Skip to content

Commit 88a4c2d

Browse files
authored
Add preview workflow (#279)
* Create action to authenticate with AWS using the generated role name * Create preview reusable workflow * Trigger reusable workflow in PR workflow * Add cleanup workflow * Upload binary instead of built documentation * Remove empty newline * fix
1 parent 44e4627 commit 88a4c2d

File tree

4 files changed

+205
-5
lines changed

4 files changed

+205
-5
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: AWS Auth
2+
3+
description: |
4+
This is an opinionated action to authenticate with AWS.
5+
It will generate a role ARN based on the repository name and the AWS account ID.
6+
7+
inputs:
8+
aws_account_id:
9+
description: 'The AWS account ID to generate the role ARN for'
10+
required: true
11+
default: '197730964718' # elastic-web
12+
aws_region:
13+
description: 'The AWS region to use'
14+
required: false
15+
default: 'us-east-1'
16+
aws_role_name_prefix:
17+
description: 'The prefix for the role name'
18+
required: false
19+
default: 'elastic-docs-v3-preview-'
20+
21+
runs:
22+
using: composite
23+
steps:
24+
- name: Generate AWS Role ARN
25+
id: role_arn
26+
shell: python
27+
env:
28+
AWS_ACCOUNT_ID: ${{ inputs.aws_account_id }}
29+
ROLE_NAME_PREFIX: ${{ inputs.aws_role_name_prefix }}
30+
run: |
31+
import hashlib
32+
import os
33+
prefix = os.environ["ROLE_NAME_PREFIX"]
34+
m = hashlib.sha256()
35+
m.update(os.environ["GITHUB_REPOSITORY"].encode('utf-8'))
36+
hash = m.hexdigest()[:64-len(prefix)]
37+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
38+
f.write(f"result=arn:aws:iam::{os.environ["AWS_ACCOUNT_ID"]}:role/{prefix}{hash}")
39+
- name: Configure AWS Credentials
40+
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
41+
with:
42+
role-to-assume: ${{ steps.role_arn.outputs.result }}
43+
aws-region: ${{ inputs.aws_region }}

.github/workflows/pr.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
permissions:
77
contents: read
88
packages: read
9+
id-token: write
10+
pull-requests: write
11+
deployments: write
912

1013
concurrency:
1114
group: ${{ github.workflow }}-${{ github.ref }}
@@ -32,8 +35,14 @@ jobs:
3235

3336
- name: Publish AOT
3437
run: ./build.sh publishbinaries
35-
36-
# we run our artifact directly please use the prebuild
37-
# elastic/docs-builder@main GitHub Action for all other repositories!
38-
- name: Build documentation
39-
run: .artifacts/publish/docs-builder/release/docs-builder --strict
38+
39+
- uses: actions/upload-artifact@v4
40+
with:
41+
name: docs-builder-binary
42+
path: .artifacts/publish/docs-builder/release/docs-builder
43+
if-no-files-found: error
44+
retention-days: 1
45+
46+
preview:
47+
needs: build
48+
uses: ./.github/workflows/preview.yml
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: preview-cleanup
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
7+
permissions:
8+
deployments: write
9+
id-token: write
10+
11+
jobs:
12+
cleanup:
13+
runs-on: ubuntu-latest
14+
environment: preview-${{ github.event.pull_request.number }}
15+
steps:
16+
- uses: ./.github/actions/aws-auth
17+
- name: Delete s3 objects
18+
env:
19+
PR_NUMBER: ${{ github.event.pull_request.number }}
20+
run: |
21+
aws s3 rm "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --recursive
22+
23+
- name: Delete GitHub environment
24+
uses: actions/github-script@v7
25+
with:
26+
script: |
27+
const { owner, repo } = context.repo;
28+
const deployments = await github.rest.repos.listDeployments({
29+
owner,
30+
repo,
31+
environment: `preview-${context.issue.number}`
32+
});
33+
for (const deployment of deployments.data) {
34+
await github.rest.repos.createDeploymentStatus({
35+
owner,
36+
repo,
37+
deployment_id: deployment.id,
38+
state: 'inactive',
39+
description: 'Marking deployment as inactive'
40+
});
41+
await github.rest.repos.deleteDeployment({
42+
owner,
43+
repo,
44+
deployment_id: deployment.id
45+
});
46+
}
47+
48+
octokit.rest.repos.deleteAnEnvironment({
49+
owner,
50+
repo,
51+
environment_name: `preview-${context.issue.number}`,
52+
});
53+
54+
55+

.github/workflows/preview.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: preview
2+
3+
on:
4+
workflow_call: ~
5+
6+
permissions:
7+
id-token: write
8+
pull-requests: write
9+
deployments: write
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Create Deployment
16+
uses: actions/github-script@v7
17+
id: deployment
18+
with:
19+
result-encoding: string
20+
script: |
21+
const { owner, repo } = context.repo;
22+
const deployment = await github.rest.repos.createDeployment({
23+
issue_number: context.issue.number,
24+
owner,
25+
repo,
26+
ref: context.payload.pull_request.head.ref,
27+
environment: `preview-${context.issue.number}`,
28+
description: `Preview deployment for PR ${context.issue.number}`,
29+
auto_merge: false,
30+
required_contexts: [],
31+
})
32+
await github.rest.repos.createDeploymentStatus({
33+
deployment_id: deployment.data.id,
34+
owner,
35+
repo,
36+
state: "in_progress",
37+
description: "Deployment created",
38+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
39+
})
40+
return deployment.data.id
41+
42+
- uses: actions/checkout@v4
43+
44+
- uses: actions/download-artifact@v4
45+
with:
46+
name: docs-builder-binary
47+
48+
# we run our artifact directly please use the prebuild
49+
# elastic/docs-builder@main GitHub Action for all other repositories!
50+
- name: Build documentation
51+
env:
52+
PR_NUMBER: ${{ github.event.pull_request.number }}
53+
run: |
54+
chmod +x ./docs-builder
55+
./docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
56+
57+
- uses: ./.github/actions/aws-auth
58+
59+
- name: Upload to S3
60+
env:
61+
PR_NUMBER: ${{ github.event.pull_request.number }}
62+
run: |
63+
aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
64+
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
65+
66+
- name: Update deployment status
67+
uses: actions/github-script@v7
68+
if: steps.deployment.outputs.result
69+
with:
70+
script: |
71+
await github.rest.repos.createDeploymentStatus({
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
deployment_id: ${{ steps.deployment.outputs.result }},
75+
state: "success",
76+
description: "Deployment completed",
77+
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${context.issue.number}`,
78+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
79+
})
80+
81+
- name: Update Deployment Status on Failure
82+
if: failure() && steps.deployment.outputs.result
83+
uses: actions/github-script@v7
84+
with:
85+
script: |
86+
await github.rest.repos.createDeploymentStatus({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
deployment_id: ${{ steps.deployment.outputs.result }},
90+
state: "failure",
91+
description: "Deployment failed",
92+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
93+
})

0 commit comments

Comments
 (0)