|
| 1 | +name: Deploy Bundle Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - '*' |
| 7 | + |
| 8 | +jobs: |
| 9 | + build-and-deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Configure Git Credentials |
| 14 | + uses: de-vri-es/setup-git-credentials@v2 |
| 15 | + with: |
| 16 | + credentials: ${{ secrets.GIT_CREDENTIALS }} |
| 17 | + |
| 18 | + - name: Checkout Repository |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Setup Node.js |
| 22 | + uses: actions/setup-node@v3 |
| 23 | + with: |
| 24 | + node-version: 16.20.2 |
| 25 | + |
| 26 | + - name: Install Dependencies |
| 27 | + run: npm ci |
| 28 | + |
| 29 | + - name: Extract Branch Names |
| 30 | + shell: bash |
| 31 | + run: | |
| 32 | + # transform branch names in form of `refs/heads/main` to `main` |
| 33 | + draft_branch=$(basename ${{ github.event.pull_request.head.ref }}) |
| 34 | + echo "draft_branch=$draft_branch" >> $GITHUB_OUTPUT |
| 35 | + id: extract_branch |
| 36 | + |
| 37 | + - name: Build UI Bundle Preview |
| 38 | + run: | |
| 39 | + set -o pipefail |
| 40 | + gulp lint |& tee $GITHUB_WORKSPACE/build.log |
| 41 | + UI_ROOT_PATH_PREFIX=${{ github.event.repository.name }}/${{ steps.extract_branch.outputs.draft_branch }} \ |
| 42 | + gulp preview:build |& tee $GITHUB_WORKSPACE/build.log |
| 43 | +
|
| 44 | + - name: Check Build Result |
| 45 | + id: logFail |
| 46 | + if: failure() |
| 47 | + run: | |
| 48 | + MULTILINE_LOG=$(cat $GITHUB_WORKSPACE/build.log) |
| 49 | + echo "BUILD_FAILURE<<EOF" >> $GITHUB_ENV |
| 50 | + echo $MULTILINE_LOG >> $GITHUB_ENV |
| 51 | + echo "EOF" >> $GITHUB_ENV |
| 52 | +
|
| 53 | + - name: Create Build Success Comment |
| 54 | + if: ${{ success() && github.event.pull_request.number }} |
| 55 | + uses: peter-evans/create-or-update-comment@v3 |
| 56 | + with: |
| 57 | + token: ${{ secrets.COMMENT_GITHUB_TOKEN }} |
| 58 | + issue-number: ${{ github.event.pull_request.number }} |
| 59 | + body: | |
| 60 | + UI bundle preview build successful! :white_check_mark: |
| 61 | + Deploying preview to GitHub Pages. |
| 62 | + reactions: rocket |
| 63 | + |
| 64 | + - name: Create Build Failure Comment |
| 65 | + if: ${{ failure() && github.event.pull_request.number }} |
| 66 | + uses: peter-evans/create-or-update-comment@v3 |
| 67 | + with: |
| 68 | + token: ${{ secrets.COMMENT_GITHUB_TOKEN }} |
| 69 | + issue-number: ${{ github.event.pull_request.number }} |
| 70 | + body: | |
| 71 | + UI bundle preview build failure! :x: |
| 72 | + > ${{ env.BUILD_FAILURE }} |
| 73 | + reactions: confused |
| 74 | + |
| 75 | + - name: Find Comment |
| 76 | + if: ${{ success() && github.event.pull_request.number }} |
| 77 | + uses: peter-evans/find-comment@v2 |
| 78 | + id: fc |
| 79 | + with: |
| 80 | + token: ${{ secrets.COMMENT_GITHUB_TOKEN }} |
| 81 | + issue-number: ${{ github.event.pull_request.number }} |
| 82 | + comment-author: 'mlr' |
| 83 | + body-includes: UI bundle preview build successful! |
| 84 | + direction: last |
| 85 | + |
| 86 | + - name: Deploy to GitHub Pages |
| 87 | + if: success() |
| 88 | + run: | |
| 89 | + git clone https://github.com/$GITHUB_REPOSITORY.git pages |
| 90 | + cd pages |
| 91 | + git checkout gh-pages |
| 92 | +
|
| 93 | + # If there was previously a build for the preview, then remove it |
| 94 | + # so we get a clean build. This is needed in case a follow up |
| 95 | + # build of the same pull request contains content deletions. |
| 96 | + rm -rf ${{ steps.extract_branch.outputs.draft_branch }} |
| 97 | +
|
| 98 | + mkdir -p ${{ steps.extract_branch.outputs.draft_branch }} |
| 99 | + cp -r ../public/* ${{ steps.extract_branch.outputs.draft_branch }}/. |
| 100 | +
|
| 101 | + # Records the repository that originally triggered the build so we can post back |
| 102 | + # comments upon clean up of a stale draft if it still has an open pull request. |
| 103 | + echo "${{ github.event.repository.full_name }}" > ${{ steps.extract_branch.outputs.draft_branch }}/.github_source_repository |
| 104 | +
|
| 105 | + git add . |
| 106 | + git config user.name 'github-actions[bot]' |
| 107 | + git config user.email 'github-actions[bot]@users.noreply.github.com' |
| 108 | + git commit --allow-empty -m "Auto-deployed from GitHub Actions" |
| 109 | + git push -u origin gh-pages |
| 110 | +
|
| 111 | + - name: Obtain GitHub Pages build URL |
| 112 | + if: success() |
| 113 | + run: | |
| 114 | + sleep 5 # Allow time for build to initiate |
| 115 | + build_url=$(curl -s -L \ |
| 116 | + -H "Accept: application/vnd.github+json" \ |
| 117 | + -H "Authorization: Bearer ${{ secrets.COMMENT_GITHUB_TOKEN }}" \ |
| 118 | + -H "X-GitHub-Api-Version: 2022-11-28" 'https://api.github.com/repos/${{ github.event.repository.full_name }}/pages/builds/latest' \ |
| 119 | + | jq -r .url) |
| 120 | + echo "url=$build_url" >> $GITHUB_OUTPUT |
| 121 | + id: ghpages_build |
| 122 | + |
| 123 | + - name: Wait for Github Pages deployment |
| 124 | + if: success() |
| 125 | + run: | |
| 126 | + for i in {1..60}; do |
| 127 | + build_status=$(curl -s -L \ |
| 128 | + -H "Accept: application/vnd.github+json" \ |
| 129 | + -H "Authorization: Bearer ${{ secrets.COMMENT_GITHUB_TOKEN }}" \ |
| 130 | + -H "X-GitHub-Api-Version: 2022-11-28" '${{ steps.ghpages_build.outputs.url }}' \ |
| 131 | + | jq -r .status) |
| 132 | +
|
| 133 | + if [ "$build_status" == "built" ]; then echo "Deploy is complete." |
| 134 | + exit 0 |
| 135 | + else |
| 136 | + echo "Deploy is not complete. Status: $build_status. Retrying in 10 seconds..." |
| 137 | + sleep 10 |
| 138 | + fi |
| 139 | + done |
| 140 | + echo "Deploy is still not complete after approximately 10 minutes." |
| 141 | + exit 1 |
| 142 | +
|
| 143 | + - name: Get GitHub Pages Preview URL |
| 144 | + if: success() |
| 145 | + shell: bash |
| 146 | + run: | |
| 147 | + echo "url=https://riptano.github.io/${{ github.event.repository.name }}/${{ steps.extract_branch.outputs.draft_branch }}/" >> $GITHUB_OUTPUT |
| 148 | + id: draft_url |
| 149 | + |
| 150 | + - name: Update comment |
| 151 | + if: ${{ steps.fc.outputs.comment-id != '' }} |
| 152 | + uses: peter-evans/create-or-update-comment@v3 |
| 153 | + with: |
| 154 | + token: ${{ secrets.COMMENT_GITHUB_TOKEN }} |
| 155 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 156 | + body: | |
| 157 | + Deployment successful! [View preview](${{ steps.draft_url.outputs.url }}) |
| 158 | + reactions: hooray |
0 commit comments