gha(amplify-preview): add job to post amplify preview URL on PRs #8
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' <<< "$job_info")" >> $GITHUB_OUTPUT | |
echo "COMMIT_ID=$(jq -r '.commitId' <<< "$job_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 }} | |
COMMIT_ID: ${{ steps.get_amplify_job.outputs.COMMIT_ID }} | |
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 commitId = process.env.COMMIT_ID; | |
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} | |
- **COMMIT_ID**: ${commitId} | |
</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) { | |
console.log("Posting new comment ${existingComment.id}") | |
github.rest.issues.createComment({ | |
...prProps, | |
body: commentBody, | |
}) | |
} else { | |
console.log("Found existing comment ${existingComment.id}") | |
github.rest.issues.updateComment({ | |
...prProps, | |
body: commentBody, | |
comment_id: existingComment.id, | |
}) | |
} |