Skip to content

Commit 7c9d6ad

Browse files
committed
Rework preview workflows
1 parent 263ef57 commit 7c9d6ad

File tree

4 files changed

+92
-193
lines changed

4 files changed

+92
-193
lines changed
Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: preview-build
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types:
66
- opened
77
- synchronize
@@ -28,108 +28,136 @@ on:
2828
default: '**'
2929
required: false
3030

31-
permissions:
31+
permissions:
32+
id-token: write
33+
deployments: write
3234
contents: read
3335
pull-requests: read
3436

3537
jobs:
3638
build:
3739
concurrency:
3840
group: ${{ github.workflow }}-${{ github.ref }}
39-
cancel-in-progress: true
41+
cancel-in-progress: ${{ github.event_name == 'pull_request_target' }}
4042
runs-on: ubuntu-latest
4143
steps:
4244

4345
- name: Get changed files
44-
if: github.event_name == 'pull_request'
46+
if: github.event_name == 'pull_request_target'
4547
id: check-files
4648
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
4749
with:
4850
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
4951

5052
- name: Checkout
51-
if: github.event_name != 'pull_request' || steps.check-files.outputs.any_changed == 'true'
53+
if: github.event_name == 'push' || steps.check-files.outputs.any_changed == 'true'
5254
uses: actions/checkout@v4
5355
with:
56+
ref: ${{ github.event.pull_request.head.sha || github.ref }}
5457
persist-credentials: false
55-
56-
- name: Generate path prefix
58+
59+
- name: Create Deployment
60+
if: github.event_name == 'push' || steps.check-files.outputs.any_changed == 'true'
61+
uses: actions/github-script@v7
62+
id: deployment
63+
env:
64+
PR_NUMBER: ${{ github.event.pull_request.number }}
65+
REF: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.ref || github.ref_name }}
66+
with:
67+
result-encoding: string
68+
script: |
69+
const { owner, repo } = context.repo;
70+
const prNumber = process.env.PR_NUMBER;
71+
// const environment = prNumber ? `docs-preview-${prNumber}` : 'docs-preview';
72+
const environment = 'docs-preview';
73+
const deployment = await github.rest.repos.createDeployment({
74+
owner,
75+
repo,
76+
environment,
77+
ref: process.env.REF,
78+
auto_merge: false,
79+
transient_environment: true,
80+
required_contexts: [],
81+
})
82+
await github.rest.repos.createDeploymentStatus({
83+
deployment_id: deployment.data.id,
84+
owner,
85+
repo,
86+
state: "in_progress",
87+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
88+
})
89+
return deployment.data.id
90+
91+
- name: Generate env.PATH_PREFIX
92+
if: steps.deployment.outputs.result
5793
env:
5894
PR_NUMBER: ${{ github.event.pull_request.number }}
95+
GITHUB_REF_NAME: ${{ github.ref_name }}
5996
run: |
6097
case "${GITHUB_EVENT_NAME}" in
61-
"pull_request")
98+
"pull_request_target")
6299
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" >> $GITHUB_ENV
63100
;;
64101
"push")
65102
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" >> $GITHUB_ENV
66103
if [[ ! "${GITHUB_REF_NAME}" =~ ^(main|master|16\.x)$ ]]; then
67-
echo "Unsupported ref name: ${GITHUB_REF_NAME}";
104+
echo "Unsupported ref name: '${GITHUB_REF_NAME}'";
68105
exit 1;
69106
fi
70107
;;
71108
*)
72-
echo "Unsupported event: ${GITHUB_EVENT_NAME}";
109+
echo "Unsupported event: '${GITHUB_EVENT_NAME}'";
73110
exit 1;
74111
;;
75112
esac
76-
77-
- name: Store deployment metadata
78-
id: metadata
79-
env:
80-
PR_NUMBER: ${{ github.event.pull_request.number }}
81-
REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
82-
SHOULD_DEPLOY: ${{ github.event_name == 'pull_request' && steps.check-files.outputs.any_changed || 'true' }}
83-
run: |
84-
cat << EOF > deployment_metadata.json
85-
{
86-
"pr_number": "${PR_NUMBER}",
87-
"ref": "${REF}",
88-
"should_deploy": "${SHOULD_DEPLOY}",
89-
"path_prefix": "${PATH_PREFIX}"
90-
}
91-
EOF
92-
echo "should_deploy=${SHOULD_DEPLOY}" >> "${GITHUB_OUTPUT}"
93-
94-
- name: Upload deployment metadata
95-
uses: actions/upload-artifact@v4
96-
with:
97-
name: deployment_metadata
98-
path: deployment_metadata.json
99-
if-no-files-found: error
100-
retention-days: 1
101-
compression-level: 1
102113
103114
- name: Bootstrap Action Workspace
104-
if: github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
115+
if: github.repository == 'elastic/docs-builder' && steps.deployment.outputs.result
116+
105117
uses: ./.github/actions/bootstrap
106118

107119
- name: Set REDESIGN feature flag
108-
if: contains(github.event.pull_request.labels.*.name, 'redesign')
120+
if: contains(github.event.pull_request.labels.*.name, 'redesign') && steps.deployment.outputs.result
109121
run: echo "REDESIGN=true" >> $GITHUB_ENV
110122

111123
# we run our artifact directly please use the prebuild
112124
# elastic/docs-builder@main GitHub Action for all other repositories!
113125
- name: Build documentation
114-
if: github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
126+
if: github.repository == 'elastic/docs-builder' && steps.deployment.outputs.result
115127
run: |
116128
dotnet run --project src/docs-builder -- --strict --path-prefix "${PATH_PREFIX}"
117129
118130
- name: Build documentation
119-
if: github.repository != 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
131+
if: github.repository != 'elastic/docs-builder' && steps.deployment.outputs.result
120132
uses: elastic/docs-builder@main
121133
continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
122134
with:
123135
prefix: ${{ env.PATH_PREFIX }}
124136
strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
125137

126-
- uses: actions/upload-artifact@v4
127-
if: steps.metadata.outputs.should_deploy == 'true'
138+
- uses: elastic/docs-builder/.github/actions/aws-auth@main
139+
if: steps.deployment.outputs.result
140+
141+
- name: Upload to S3
142+
if: steps.deployment.outputs.result
143+
run: |
144+
aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
145+
aws cloudfront create-invalidation \
146+
--distribution-id EKT7LT5PM8RKS \
147+
--paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
148+
149+
- name: Update deployment status
150+
uses: actions/github-script@v7
151+
if: always() && steps.deployment.outputs.result
152+
env:
153+
PR_NUMBER: ${{ github.event.pull_request.number }}
128154
with:
129-
name: docs
130-
path: .artifacts/docs/html/
131-
if-no-files-found: error
132-
retention-days: 1
133-
# The lower the compression-level, the faster the artifact will be uploaded.
134-
# But the size of the artifact will be larger.
135-
compression-level: 1
155+
script: |
156+
await github.rest.repos.createDeploymentStatus({
157+
owner: context.repo.owner,
158+
repo: context.repo.repo,
159+
deployment_id: ${{ steps.deployment.outputs.result }},
160+
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
161+
environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`,
162+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
163+
})

.github/workflows/preview-cleanup.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ jobs:
2323
const deployments = await github.rest.repos.listDeployments({
2424
owner,
2525
repo,
26-
environment: `docs-preview-${context.issue.number}`
26+
environment: 'docs-preview',
27+
ref: context.payload.pull_request.head.ref,
2728
});
2829
core.setOutput('is-empty', deployments.data.length === 0)
2930
for (const deployment of deployments.data) {

.github/workflows/preview-deploy.yml

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -14,103 +14,8 @@ permissions:
1414
actions: read
1515

1616
jobs:
17-
deployment-metadata:
18-
if: ${{ github.event.workflow_run.conclusion == 'success' }}
17+
do-nothing:
1918
runs-on: ubuntu-latest
20-
outputs:
21-
pr_number: ${{ steps.metadata.outputs.pr_number }}
22-
ref: ${{ steps.metadata.outputs.ref }}
23-
should_deploy: ${{ steps.metadata.outputs.should_deploy }}
24-
path_prefix: ${{ steps.metadata.outputs.path_prefix }}
2519
steps:
26-
- name: Download deployment metadata
27-
env:
28-
GH_TOKEN: ${{ github.token }}
29-
run: |
30-
gh run download ${{ github.event.workflow_run.id }} \
31-
--repo "${GITHUB_REPOSITORY}" \
32-
--name deployment_metadata
33-
- name: Get deployment metadata
34-
id: metadata
35-
run: |
36-
{
37-
echo "pr_number=$(jq -r '.pr_number' deployment_metadata.json)"
38-
echo "ref=$(jq -r '.ref' deployment_metadata.json)"
39-
echo "path_prefix=$(jq -r '.path_prefix' deployment_metadata.json)"
40-
echo "should_deploy=$(jq -r '.should_deploy' deployment_metadata.json)"
41-
} >> "${GITHUB_OUTPUT}"
42-
43-
deploy:
44-
needs: deployment-metadata
45-
if: needs.deployment-metadata.outputs.should_deploy == 'true'
46-
runs-on: ubuntu-latest
47-
concurrency:
48-
group: ${{ github.workflow }}-${{ needs.deployment-metadata.outputs.pr_number }}
49-
cancel-in-progress: true
50-
steps:
51-
- name: Create Deployment
52-
uses: actions/github-script@v7
53-
id: deployment
54-
env:
55-
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
56-
REF: ${{ needs.deployment-metadata.outputs.ref }}
57-
with:
58-
result-encoding: string
59-
script: |
60-
const { owner, repo } = context.repo;
61-
const prNumber = process.env.PR_NUMBER;
62-
const environment = prNumber ? `docs-preview-${prNumber}` : 'docs-preview';
63-
const deployment = await github.rest.repos.createDeployment({
64-
owner,
65-
repo,
66-
environment,
67-
ref: process.env.REF,
68-
auto_merge: false,
69-
required_contexts: [],
70-
})
71-
await github.rest.repos.createDeploymentStatus({
72-
deployment_id: deployment.data.id,
73-
owner,
74-
repo,
75-
state: "in_progress",
76-
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
77-
})
78-
return deployment.data.id
79-
80-
- name: Download docs
81-
env:
82-
GH_TOKEN: ${{ github.token }}
83-
run: |
84-
gh run download ${{ github.event.workflow_run.id }} \
85-
--repo "${GITHUB_REPOSITORY}" \
86-
--name docs \
87-
--dir html
88-
89-
- uses: elastic/docs-builder/.github/actions/aws-auth@main
90-
91-
- name: Upload to S3
92-
env:
93-
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
94-
PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }}
95-
run: |
96-
aws s3 sync ./html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
97-
aws cloudfront create-invalidation \
98-
--distribution-id EKT7LT5PM8RKS \
99-
--paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
100-
101-
- name: Update deployment status
102-
uses: actions/github-script@v7
103-
if: always() && steps.deployment.outputs.result
104-
env:
105-
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
106-
PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }}
107-
with:
108-
script: |
109-
await github.rest.repos.createDeploymentStatus({
110-
owner: context.repo.owner,
111-
repo: context.repo.repo,
112-
deployment_id: ${{ steps.deployment.outputs.result }},
113-
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
114-
environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`,
115-
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
116-
})
20+
- name: Do nothing
21+
run: echo "This is here for backwards compatibility. After validating that the preview-build workflow is working as expected, you can remove this workflow file."

docs/migration/guide/how-to-set-up-docs-previews.md

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ This guide will help you set up docs previews for your GitHub repository.
44

55
## GitHub Workflows
66

7-
The docs preview system consists of three GitHub Workflows:
7+
The docs preview system consists of two GitHub Workflows:
88
- [`docs-build.yml`](#build): **Build** the docs on a PR
9-
- [`docs-deploy.yml`](#deploy): **Deploy** the docs to our preview environment
109
- [`docs-cleanup.yml`](#cleanup): **Cleanup** the docs after a PR is merged or closed
1110

1211

@@ -22,65 +21,31 @@ This way you only build and deploy the docs when there are changes to the docs a
2221

2322
```yaml
2423
---
25-
name: docs-build <1>
24+
name: docs-build
2625

2726
on:
28-
push: <2>
27+
push:
2928
branches:
3029
- main
31-
pull_request: ~
30+
pull_request_target: ~
3231

3332
jobs:
3433
docs-preview:
35-
uses: elastic/docs-builder/.github/workflows/preview-build.yml <3>
34+
uses: elastic/docs-builder/.github/workflows/preview-build.yml <2>
3635
with:
37-
path-pattern: docs/** <4>
36+
path-pattern: docs/** <3>
3837
permissions:
38+
id-token: write
39+
deployments: write
3940
contents: read
4041
pull-requests: read
4142
```
4243
43-
1. The naming is important so that the `docs-deploy` workflow is triggered.
44-
2. You can omit the `push` event if you only want to build the docs on PRs.
45-
3. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-build.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-build.yml)
46-
4. This should be the path to your docs folder.
47-
44+
1. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-build.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-build.yml)
45+
2. This should be the path to your docs folder.
4846
4947
::::
5048
51-
### Deploy
52-
53-
This workflow is triggered when the `docs-build` workflow is completed. The underlying reusable workflow, downloads the artifact and deploys the docs to our preview environment.
54-
55-
56-
::::{dropdown} .github/workflows/docs-deploy.yml
57-
:open:
58-
59-
```yaml
60-
---
61-
name: docs-deploy
62-
63-
on:
64-
workflow_run:
65-
workflows:
66-
- docs-build <1>
67-
types:
68-
- completed
69-
70-
jobs:
71-
docs-preview:
72-
uses: elastic/docs-builder/.github/workflows/preview-deploy.yml <2>
73-
permissions:
74-
contents: none <3>
75-
id-token: write
76-
deployments: write
77-
actions: read
78-
```
79-
1. The name of the previously mentioned `docs-build` workflow.
80-
2. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-deploy.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-deploy.yml)
81-
3. No need to read the code.
82-
83-
::::
8449
8550
### Cleanup
8651

0 commit comments

Comments
 (0)