-
Notifications
You must be signed in to change notification settings - Fork 1
103 lines (89 loc) · 3.71 KB
/
amplify-preview.yaml
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
on:
pull_request:
permissions:
pull-requests: write
id-token: write
jobs:
amplify-preview:
name: Amplify Preview
runs-on: ubuntu-latest
environment: docs-amplify
steps:
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4
with:
aws-region: us-west-2
role-to-assume: ${{ vars.IAM_ROLE }}
- name: Describe Amplify branch
id: get_amplify_branch
env:
AMPLIFY_APP_IDS: ${{ vars.AMPLIFY_APP_IDS}}
BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }}
shell: bash
run: |
IFS=, app_id_array=($AMPLIFY_APP_IDS)
for app_id in "${app_id_array[@]}"; do
branch_info=$(aws amplify get-branch --app-id ${app_id} --branch-name ${BRANCH_NAME} --query 'branch')
if [ $? -eq 0 ]; then
echo "PREVIEW_URL=https://$(jq -r '.displayName' <<< "$branch_info").$app_id.amplifyapp.com" >> $GITHUB_OUTPUT
echo "CREATE_TIME=$(jq -r '.createTime' <<< "$branch_info")" >> $GITHUB_OUTPUT
echo "UPDATE_TIME=$(jq -r '.updateTime' <<< "$branch_info")" >> $GITHUB_OUTPUT
echo "JOB_ID=$(jq -r '.activeJobId' <<< "$branch_info")" >> $GITHUB_OUTPUT
echo "APP_ID=${app_id}" >> $GITHUB_OUTPUT
break
fi
done
- name: Describe Amplify Deployment
id: get_amplify_job
env:
APP_ID: ${{ steps.get_amplify_branch.outputs.APP_ID }}
JOB_ID: ${{ steps.get_amplify_branch.outputs.JOB_ID }}
BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }}
shell: bash
continue-on-error: true
run: |
job_info=$(aws amplify get-job --app-id ${APP_ID} --branch-name ${BRANCH_NAME} --job-id ${JOB_ID} --query 'job.summary')
echo "JOB_STATUS=$(jq -r '.status' <<< "$branch_info")" >> $GITHUB_OUTPUT
- uses: actions/github-script@v7
env:
PREVIEW_URL: ${{ steps.get_amplify_branch.outputs.PREVIEW_URL }}
UPDATE_TIME: ${{ steps.get_amplify_branch.outputs.UPDATE_TIME }}
JOB_ID: ${{ steps.get_amplify_branch.outputs.JOB_ID }}
JOB_STATUS: ${{ steps.get_amplify_job.outputs.JOB_STATUS }}
with:
script: |
const previewUrl = process.env.PREVIEW_URL;
const jobId = process.env.JOB_ID;
const jobStatus = process.env.JOB_STATUS || "unknown";
const updatedAt = process.env.UPDATE_TIME;
const commentBody = `![🤖](https://a0.awsstatic.com/libra-css/images/site/fav/favicon.ico) Amplify preview here: ${previewUrl}
<details><summary>Preview details</summary>
- **LAST_UPDATED_AT**: ${updatedAt}
- **JOB_ID**: ${jobId}
- **JOB_STATUS**: ${jobStatus}
</details>
`;
const prProps = {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
};
const comments = (await github.rest.issues.listComments(prProps))?.data;
const existingComment = comments?.find((comment) =>
comment.user.login === "github-actions[bot]" && comment.body.includes("Amplify preview here"));
if (existingComment != null) {
github.rest.issues.createComment({
...prProps,
body: commentBody,
})
} else {
github.rest.issues.updateComment({
...prProps,
body: commentBody,
comment_id: existingComment.id,
})
}