|
| 1 | +name: Amplify Preview |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + |
| 5 | +permissions: |
| 6 | + pull-requests: write |
| 7 | + id-token: write |
| 8 | + |
| 9 | +jobs: |
| 10 | + amplify-preview: |
| 11 | + name: Get and post Amplify preview URL |
| 12 | + runs-on: ubuntu-latest |
| 13 | + environment: docs-amplify |
| 14 | + steps: |
| 15 | + - name: Extract branch name |
| 16 | + shell: bash |
| 17 | + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT |
| 18 | + id: extract_branch |
| 19 | + |
| 20 | + - name: Configure AWS credentials |
| 21 | + uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4 |
| 22 | + with: |
| 23 | + aws-region: us-west-2 |
| 24 | + role-to-assume: ${{ vars.IAM_ROLE }} |
| 25 | + |
| 26 | + - name: Describe Amplify branch |
| 27 | + id: get_amplify_branch |
| 28 | + env: |
| 29 | + AMPLIFY_APP_IDS: ${{ vars.AMPLIFY_APP_IDS}} |
| 30 | + BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }} |
| 31 | + shell: bash |
| 32 | + run: | |
| 33 | + IFS=, app_id_array=($AMPLIFY_APP_IDS) |
| 34 | + for app_id in "${app_id_array[@]}"; do |
| 35 | + branch_info=$(aws amplify get-branch --app-id ${app_id} --branch-name ${BRANCH_NAME} --query 'branch') |
| 36 | + if [ $? -eq 0 ]; then |
| 37 | + echo "PREVIEW_URL=https://$(jq -r '.displayName' <<< "$branch_info").$app_id.amplifyapp.com" >> $GITHUB_OUTPUT |
| 38 | + echo "CREATE_TIME=$(jq -r '.createTime' <<< "$branch_info")" >> $GITHUB_OUTPUT |
| 39 | + echo "UPDATE_TIME=$(jq -r '.updateTime' <<< "$branch_info")" >> $GITHUB_OUTPUT |
| 40 | + echo "JOB_ID=$(jq -r '.activeJobId' <<< "$branch_info")" >> $GITHUB_OUTPUT |
| 41 | + echo "APP_ID=${app_id}" >> $GITHUB_OUTPUT |
| 42 | + break |
| 43 | + fi |
| 44 | + done |
| 45 | +
|
| 46 | + - name: Describe Amplify Deployment |
| 47 | + id: get_amplify_job |
| 48 | + env: |
| 49 | + APP_ID: ${{ steps.get_amplify_branch.outputs.APP_ID }} |
| 50 | + JOB_ID: ${{ steps.get_amplify_branch.outputs.JOB_ID }} |
| 51 | + BRANCH_NAME: ${{ steps.extract_branch.outputs.branch }} |
| 52 | + shell: bash |
| 53 | + continue-on-error: true |
| 54 | + run: | |
| 55 | + job_info=$(aws amplify get-job --app-id ${APP_ID} --branch-name ${BRANCH_NAME} --job-id ${JOB_ID} --query 'job.summary') |
| 56 | + echo "JOB_STATUS=$(jq -r '.status' <<< "$job_info")" >> $GITHUB_OUTPUT |
| 57 | + echo "COMMIT_ID=$(jq -r '.commitId' <<< "$job_info")" >> $GITHUB_OUTPUT |
| 58 | +
|
| 59 | + - uses: actions/github-script@v7 |
| 60 | + env: |
| 61 | + PREVIEW_URL: ${{ steps.get_amplify_branch.outputs.PREVIEW_URL }} |
| 62 | + UPDATE_TIME: ${{ steps.get_amplify_branch.outputs.UPDATE_TIME }} |
| 63 | + JOB_ID: ${{ steps.get_amplify_branch.outputs.JOB_ID }} |
| 64 | + JOB_STATUS: ${{ steps.get_amplify_job.outputs.JOB_STATUS }} |
| 65 | + COMMIT_ID: ${{ steps.get_amplify_job.outputs.COMMIT_ID }} |
| 66 | + with: |
| 67 | + script: | |
| 68 | + const previewUrl = process.env.PREVIEW_URL; |
| 69 | + const jobId = process.env.JOB_ID; |
| 70 | + const jobStatus = process.env.JOB_STATUS || "unknown"; |
| 71 | + const updatedAt = process.env.UPDATE_TIME; |
| 72 | + const commitId = process.env.COMMIT_ID; |
| 73 | +
|
| 74 | + const commentBody = ` Amplify preview here: ${previewUrl} |
| 75 | +
|
| 76 | + <details><summary>Preview details</summary> |
| 77 | +
|
| 78 | + - **LAST_UPDATED_AT**: ${updatedAt} |
| 79 | + - **JOB_ID**: ${jobId} |
| 80 | + - **JOB_STATUS**: ${jobStatus} |
| 81 | + - **COMMIT_ID**: ${commitId} |
| 82 | +
|
| 83 | + </details> |
| 84 | + `; |
| 85 | +
|
| 86 | + const prProps = { |
| 87 | + issue_number: context.issue.number, |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + }; |
| 91 | +
|
| 92 | + const comments = (await github.rest.issues.listComments(prProps))?.data; |
| 93 | +
|
| 94 | + const existingComment = comments?.find((comment) => |
| 95 | + comment.user.login === "github-actions[bot]" && comment.body.includes("Amplify preview here")); |
| 96 | +
|
| 97 | + if (existingComment == null) { |
| 98 | + console.log("Posting new comment ${existingComment.id}") |
| 99 | + github.rest.issues.createComment({ |
| 100 | + ...prProps, |
| 101 | + body: commentBody, |
| 102 | + }) |
| 103 | + } else { |
| 104 | + console.log("Found existing comment ${existingComment.id}") |
| 105 | + github.rest.issues.updateComment({ |
| 106 | + ...prProps, |
| 107 | + body: commentBody, |
| 108 | + comment_id: existingComment.id, |
| 109 | + }) |
| 110 | + } |
0 commit comments