-
Notifications
You must be signed in to change notification settings - Fork 14
202 lines (183 loc) · 8.84 KB
/
preview-build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
name: preview-build
on:
pull_request_target:
types:
- opened
- synchronize
- reopened
push:
branches:
- main
- master
workflow_call:
inputs:
strict:
description: 'Treat warnings as errors'
type: string
default: 'true'
metadata-only:
description: 'Only generate documentation metadata files'
type: string
required: false
default: 'false'
continue-on-error:
description: 'Do not fail to publish if build fails'
type: string
required: false
default: 'false'
path-pattern:
description: 'Path pattern to filter files. Only if changed files match the pattern, the workflow will continue.'
type: string
default: '**'
required: false
free-disk-space:
description: 'Free disk space before running the build'
type: string
default: 'false'
required: false
permissions:
id-token: write
deployments: write
contents: read
pull-requests: read
jobs:
build:
if: github.event.repository.fork == false # Skip running the job on the fork itself (It still runs on PRs on the upstream from forks)
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.head.ref || github.ref }}
cancel-in-progress: ${{ startsWith(github.event_name, 'pull_request') }}
runs-on: ubuntu-latest
env:
GITHUB_PR_REF_NAME: ${{ github.event.pull_request.head.ref }}
steps:
- name: Checkout
if: contains(fromJSON('["push", "merge_group", "workflow_dispatch"]'), github.event_name)
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha || github.ref }}
- name: Get changed files
if: contains(fromJSON('["merge_group", "pull_request", "pull_request_target"]'), github.event_name)
id: check-files
uses: tj-actions/changed-files@2f7c5bfce28377bc069a65ba478de0a74aa0ca32 # v46.0.1
with:
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
- name: Checkout
if: startsWith(github.event_name, 'pull_request') && steps.check-files.outputs.any_modified == 'true'
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha || github.ref }}
- name: Free Disk Space
if: contains(fromJSON('["elastic/security-docs"]'), github.repository)
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
with:
tool-cache: false
- name: Create Deployment
if: contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) || (steps.check-files.outputs.any_modified == 'true' && startsWith(github.event_name, 'pull_request'))
uses: actions/github-script@v7
id: deployment
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
REF: ${{ startsWith(github.event_name, 'pull_request') && github.event.pull_request.head.sha || github.ref_name }}
with:
result-encoding: string
script: |
const { owner, repo } = context.repo;
const prNumber = process.env.PR_NUMBER;
const environment = 'docs-preview';
const task = prNumber ? `docs-preview-${prNumber}` : undefined;
const deployment = await github.rest.repos.createDeployment({
owner,
repo,
environment,
task,
ref: process.env.REF,
auto_merge: false,
transient_environment: true,
required_contexts: [],
})
await github.rest.repos.createDeploymentStatus({
deployment_id: deployment.data.id,
owner,
repo,
state: "in_progress",
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
})
return deployment.data.id
- name: Generate env.PATH_PREFIX
if: steps.deployment.outputs.result
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REF_NAME: ${{ github.ref_name }}
run: |
case "${GITHUB_EVENT_NAME}" in
"merge_group" | "pull_request" | "pull_request_target")
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" >> $GITHUB_ENV
;;
"push" | "workflow_dispatch")
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" >> $GITHUB_ENV
if [[ ! "${GITHUB_REF_NAME}" =~ ^(main|master|16\.x)$ ]]; then
echo "Unsupported ref name: '${GITHUB_REF_NAME}'";
exit 1;
fi
;;
*)
echo "Unsupported event: '${GITHUB_EVENT_NAME}'";
exit 1;
;;
esac
- name: Bootstrap Action Workspace
if: github.repository == 'elastic/docs-builder' && steps.deployment.outputs.result
uses: ./.github/actions/bootstrap
# we run our artifact directly please use the prebuild
# elastic/docs-builder@main GitHub Action for all other repositories!
- name: Build documentation
if: github.repository == 'elastic/docs-builder' && steps.deployment.outputs.result
run: |
dotnet run --project src/docs-builder -- --strict --path-prefix "${PATH_PREFIX}"
- name: Build documentation
if: github.repository != 'elastic/docs-builder' && (steps.deployment.outputs.result || (steps.check-files.outputs.any_modified == 'true' && github.event_name == 'merge_group'))
uses: elastic/docs-builder@main
id: docs-build
continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
with:
prefix: ${{ env.PATH_PREFIX }}
strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
metadata-only: ${{ fromJSON(inputs.metadata-only != '' && inputs.metadata-only || 'true') }}
- name: 'Validate inbound links'
if: ${{ !cancelled() && steps.docs-build.outputs.skip != 'true' && (steps.deployment.outputs.result || (steps.check-files.outputs.any_modified == 'true' && github.event_name == 'merge_group')) }}
uses: elastic/docs-builder/actions/validate-inbound-local@main
- name: 'Validate local path prefixes against those claimed by global navigation.yml'
if: ${{ !cancelled() && steps.docs-build.outputs.skip != 'true' && (steps.deployment.outputs.result || (steps.check-files.outputs.any_modified == 'true' && github.event_name == 'merge_group')) }}
uses: elastic/docs-builder/actions/validate-path-prefixes-local@main
- uses: elastic/docs-builder/.github/actions/aws-auth@main
if: ${{ !cancelled() && steps.docs-build.outputs.skip != 'true' && steps.deployment.outputs.result }}
- name: Upload to S3
id: s3-upload
if: ${{ !cancelled() && steps.docs-build.outputs.skip != 'true' && steps.deployment.outputs.result }}
run: |
aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
aws cloudfront create-invalidation \
--distribution-id EKT7LT5PM8RKS \
--paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
- name: Update Link Index
if: contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') && steps.s3-upload.outcome == 'success'
uses: elastic/docs-builder/actions/update-link-index@main
- name: Update Reference Index
if: contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') && steps.s3-upload.outcome == 'success'
uses: elastic/docs-builder/actions/update-reference-index@main
- name: Update deployment status
uses: actions/github-script@v7
if: always() && steps.deployment.outputs.result
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
LANDING_PAGE_PATH: ${{ steps.docs-build.outputs.landing-page-path || env.PATH_PREFIX }}
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.deployment.outputs.result }},
state: "${{ steps.docs-build.outputs.skip == 'true' && 'inactive' || (steps.s3-upload.outcome == 'success' && 'success' || 'failure') }}",
environment_url: `https://docs-v3-preview.elastic.dev${process.env.LANDING_PAGE_PATH}`,
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
})