Skip to content

Commit a6fd161

Browse files
committed
Refactor auth into action
1 parent 7f5948a commit a6fd161

File tree

3 files changed

+89
-30
lines changed

3 files changed

+89
-30
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
steps:
15+
- uses: ./actions/aws-auth
16+
- name: Delete s3 objects
17+
env:
18+
PR_NUMBER: ${{ github.event.pull_request.number }}
19+
run: |
20+
aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
21+
22+
- name: Delete preview deployments
23+
uses: actions/github-script@v7
24+
with:
25+
script: |
26+
const deployments = await github.rest.repos.listDeployments({
27+
owner: context.repo.owner,
28+
repo: context.repo.repo,
29+
environment: `preview-${context.issue.number}`
30+
});
31+
for (const deployment of deployments.data) {
32+
await github.rest.repos.createDeploymentStatus({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
deployment_id: deployment.id,
36+
state: 'inactive',
37+
description: 'Marking deployment as inactive'
38+
});
39+
await github.rest.repos.deleteDeployment({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
deployment_id: deployment.id
43+
});
44+
}
45+

.github/workflows/preview.yml

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,24 @@ jobs:
1919
with:
2020
result-encoding: string
2121
script: |
22-
const response = await github.rest.repos.createDeployment({
22+
const deployment = await github.rest.repos.createDeployment({
2323
issue_number: context.issue.number,
2424
owner: context.repo.owner,
2525
repo: context.repo.repo,
26-
ref: "${{ github.event.pull_request.head.ref }}",
26+
ref: context.payload.pull_request.head.ref,
2727
environment: `preview-${context.issue.number}`,
2828
description: `Preview deployment for PR ${context.issue.number}`,
2929
auto_merge: false,
3030
required_contexts: [],
3131
})
32-
3332
await github.rest.repos.createDeploymentStatus({
34-
deployment_id: response.data.id,
33+
deployment_id: deployment.data.id,
3534
owner: context.repo.owner,
3635
repo: context.repo.repo,
3736
state: "in_progress",
3837
description: "Deployment created",
3938
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
4039
})
41-
4240
return response.data.id
4341
4442
- uses: actions/checkout@v4
@@ -54,29 +52,8 @@ jobs:
5452
env:
5553
PR_NUMBER: ${{ github.event.pull_request.number }}
5654
run: .artifacts/publish/docs-builder/release/docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
57-
- name: Generate ARNs
58-
id: generate_arns
59-
shell: python
60-
env:
61-
AWS_ACCOUNT_ID: 197730964718
62-
run: |
63-
import hashlib
64-
import os
65-
66-
prefix = "elastic-docs-v3-preview-"
67-
aws_account_id = os.environ["AWS_ACCOUNT_ID"]
68-
69-
m = hashlib.sha256()
70-
m.update(os.environ["GITHUB_REPOSITORY"].encode('utf-8'))
71-
hash = m.hexdigest()[:64-len(prefix)]
72-
name = f"{prefix}{hash}"
73-
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
74-
print(f"role_arn=arn:aws:iam::{aws_account_id}:role/{name}", file=f)
75-
- name: Configure AWS Credentials
76-
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
77-
with:
78-
role-to-assume: ${{ steps.generate_arns.outputs.role_arn }}
79-
aws-region: us-east-1
55+
56+
- uses: ./actions/aws-auth
8057

8158
- name: Upload to S3
8259
env:
@@ -85,7 +62,6 @@ jobs:
8562
aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
8663
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
8764
88-
8965
- name: Update deployment status
9066
uses: actions/github-script@v7
9167
if: steps.deployment.outputs.result
@@ -97,7 +73,7 @@ jobs:
9773
deployment_id: ${{ steps.deployment.outputs.result }},
9874
state: "success",
9975
description: "Deployment completed",
100-
environment_url: `https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number}}`,
76+
environment_url: `https://d2euvt1bxklciq.cloudfront.net/${context.repo.owner}/${context.repo.repo}/pull/${context.issue.number}`,
10177
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
10278
})
10379

actions/aws-auth/action.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
17+
runs:
18+
using: composite
19+
steps:
20+
- name: Generate AWS Role ARN
21+
id: role_arn
22+
shell: python
23+
env:
24+
AWS_ACCOUNT_ID: ${{ inputs.aws_account_id }}
25+
run: |
26+
import hashlib
27+
import os
28+
prefix = "elastic-docs-link-index-uploader-"
29+
m = hashlib.sha256()
30+
m.update(os.environ["GITHUB_REPOSITORY"].encode('utf-8'))
31+
hash = m.hexdigest()[:64-len(prefix)]
32+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
33+
f.write(f"result=arn:aws:iam::{os.environ["AWS_ACCOUNT_ID"]}:role/{prefix}{hash}")
34+
- name: Configure AWS Credentials
35+
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
36+
with:
37+
role-to-assume: ${{ steps.role_arn.outputs.result }}
38+
aws-region: ${{ inputs.aws_region }}

0 commit comments

Comments
 (0)