Skip to content

Commit 9322e16

Browse files
authored
Add safe preview deployment workflows (#406)
1 parent 29118e1 commit 9322e16

File tree

4 files changed

+137
-9
lines changed

4 files changed

+137
-9
lines changed

.github/workflows/pr.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,3 @@ jobs:
4242
path: .artifacts/publish/docs-builder/release/docs-builder
4343
if-no-files-found: error
4444
retention-days: 1
45-
46-
preview:
47-
needs: build
48-
uses: ./.github/workflows/preview.yml

.github/workflows/preview-build.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: preview-build
2+
3+
on:
4+
pull_request: ~
5+
workflow_call:
6+
inputs:
7+
strict:
8+
description: 'Treat warnings as errors'
9+
type: boolean
10+
default: true
11+
continue-on-error:
12+
description: 'Do not fail to publish if build fails'
13+
type: boolean
14+
required: false
15+
default: true
16+
17+
permissions:
18+
contents: read
19+
20+
jobs:
21+
build:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
27+
- name: Bootstrap Action Workspace
28+
if: github.repository == 'elastic/docs-builder'
29+
uses: ./.github/actions/bootstrap
30+
31+
# we run our artifact directly please use the prebuild
32+
# elastic/docs-builder@main GitHub Action for all other repositories!
33+
- name: Build documentation
34+
if: github.repository == 'elastic/docs-builder'
35+
env:
36+
PR_NUMBER: ${{ github.event.pull_request.number }}
37+
run: |
38+
dotnet run --project src/docs-builder -- --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
39+
40+
- name: Build documentation
41+
if: github.repository != 'elastic/docs-builder'
42+
uses: elastic/docs-builder@main
43+
continue-on-error: true
44+
with:
45+
prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
46+
strict: true
47+
- name: Add pull request number to build
48+
env:
49+
PR_NUMBER: ${{ github.event.pull_request.number }}
50+
PR_REF: ${{ github.event.pull_request.head.sha }}
51+
run: |
52+
echo "${PR_NUMBER}" >> .artifacts/docs/html/pull_request_number.txt
53+
echo "${PR_REF}" >> .artifacts/docs/html/pull_request_ref.txt
54+
- uses: actions/upload-artifact@v4
55+
with:
56+
name: docs
57+
path: .artifacts/docs/html/

.github/workflows/preview-cleanup.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ on:
66
types: [closed]
77

88
permissions:
9+
contents: none
910
deployments: write
1011
id-token: write
11-
contents: read
1212

1313
jobs:
1414
destroy:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v4
1817
- uses: elastic/docs-builder/.github/actions/aws-auth@main
1918
- name: Delete s3 objects
2019
env:
@@ -46,6 +45,3 @@ jobs:
4645
deployment_id: deployment.id
4746
});
4847
}
49-
50-
51-

.github/workflows/preview-deploy.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: preview-deploy
2+
3+
on:
4+
workflow_call: ~
5+
workflow_run:
6+
workflows: [preview-build]
7+
types:
8+
- completed
9+
10+
permissions:
11+
contents: none
12+
id-token: write
13+
deployments: write
14+
actions: read
15+
16+
jobs:
17+
docs-deploy:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Download docs
21+
env:
22+
GH_TOKEN: ${{ github.token }}
23+
run: |
24+
gh run download ${{ github.event.workflow_run.id }} --name docs --repo "${GITHUB_REPOSITORY}"
25+
- name: Get PR data
26+
id: pull_request
27+
run: |
28+
echo "number=$(cat pull_request_number.txt)" >> "${GITHUB_OUTPUT}"
29+
echo "ref=$(cat pull_request_ref.txt)" >> "${GITHUB_OUTPUT}"
30+
31+
- name: Create Deployment
32+
uses: actions/github-script@v7
33+
id: deployment
34+
env:
35+
PR_NUMBER: ${{ steps.pull_request.outputs.number }}
36+
PR_REF: ${{ steps.pull_request.outputs.ref }}
37+
with:
38+
result-encoding: string
39+
script: |
40+
const { owner, repo } = context.repo;
41+
const deployment = await github.rest.repos.createDeployment({
42+
owner,
43+
repo,
44+
ref: process.env.PR_REF,
45+
environment: `docs-preview-${process.env.PR_NUMBER}`,
46+
auto_merge: false,
47+
required_contexts: [],
48+
})
49+
await github.rest.repos.createDeploymentStatus({
50+
deployment_id: deployment.data.id,
51+
owner,
52+
repo,
53+
state: "in_progress",
54+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
55+
})
56+
return deployment.data.id
57+
58+
- uses: elastic/docs-builder/.github/actions/aws-auth@main
59+
60+
- name: Upload to S3
61+
env:
62+
PR_NUMBER: ${{ steps.pull_request.outputs.number }}
63+
run: |
64+
aws s3 sync . "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete --exclude "pull_request_number.txt"
65+
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
66+
67+
- name: Update deployment status
68+
uses: actions/github-script@v7
69+
if: always() && steps.deployment.outputs.result
70+
with:
71+
script: |
72+
await github.rest.repos.createDeploymentStatus({
73+
owner: context.repo.owner,
74+
repo: context.repo.repo,
75+
deployment_id: ${{ steps.deployment.outputs.result }},
76+
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
77+
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${{ steps.pull_request.outputs.number}}`,
78+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
79+
})

0 commit comments

Comments
 (0)