|
| 1 | +name: preview-site |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: |
| 5 | + - deploy-site |
| 6 | + types: |
| 7 | + - requested |
| 8 | + - completed |
| 9 | +jobs: |
| 10 | + deploy: |
| 11 | + if: github.repository == 'ProjectPythia/projectpythia.github.io' |
| 12 | + runs-on: ubuntu-latest |
| 13 | + defaults: |
| 14 | + run: |
| 15 | + shell: bash |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v2 |
| 18 | + - name: Set message value |
| 19 | + run: | |
| 20 | + echo "comment_message=This pull request is being automatically built with [GitHub Actions](https://github.com/features/actions) and [Netlify](https://www.netlify.com/). To see the status of your deployment, click below." >> $GITHUB_ENV |
| 21 | + - name: Find Pull Request |
| 22 | + uses: actions/github-script@v4 |
| 23 | + id: find-pull-request |
| 24 | + with: |
| 25 | + script: | |
| 26 | + let pullRequestNumber = '' |
| 27 | + let pullRequestHeadSHA = '' |
| 28 | + core.info('Finding pull request...') |
| 29 | + const pullRequests = await github.pulls.list({owner: context.repo.owner, repo: context.repo.repo}) |
| 30 | + for (let pullRequest of pullRequests.data) { |
| 31 | + if(pullRequest.head.sha === context.payload.workflow_run.head_commit.id) { |
| 32 | + pullRequestHeadSHA = pullRequest.head.sha |
| 33 | + pullRequestNumber = pullRequest.number |
| 34 | + break |
| 35 | + } |
| 36 | + } |
| 37 | + core.setOutput('number', pullRequestNumber) |
| 38 | + core.setOutput('sha', pullRequestHeadSHA) |
| 39 | + if(pullRequestNumber === '') { |
| 40 | + core.info( |
| 41 | + `No pull request associated with git commit SHA: ${context.payload.workflow_run.head_commit.id}` |
| 42 | + ) |
| 43 | + } |
| 44 | + else{ |
| 45 | + core.info(`Found pull request ${pullRequestNumber}, with head sha: ${pullRequestHeadSHA}`) |
| 46 | + } |
| 47 | + - name: Find Comment |
| 48 | + uses: peter-evans/find-comment@v1 |
| 49 | + if: steps.find-pull-request.outputs.number != '' |
| 50 | + id: fc |
| 51 | + with: |
| 52 | + issue-number: '${{ steps.find-pull-request.outputs.number }}' |
| 53 | + comment-author: 'github-actions[bot]' |
| 54 | + body-includes: '${{ env.comment_message }}' |
| 55 | + - name: Create comment |
| 56 | + if: | |
| 57 | + github.event.workflow_run.conclusion != 'success' |
| 58 | + && steps.find-pull-request.outputs.number != '' |
| 59 | + && steps.fc.outputs.comment-id == '' |
| 60 | + uses: peter-evans/create-or-update-comment@v1 |
| 61 | + with: |
| 62 | + issue-number: ${{ steps.find-pull-request.outputs.number }} |
| 63 | + body: | |
| 64 | + ${{ env.comment_message }} |
| 65 | +
|
| 66 | + 🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }} |
| 67 | + - name: Update comment |
| 68 | + if: | |
| 69 | + github.event.workflow_run.conclusion != 'success' |
| 70 | + && steps.find-pull-request.outputs.number != '' |
| 71 | + && steps.fc.outputs.comment-id != '' |
| 72 | + uses: peter-evans/create-or-update-comment@v1 |
| 73 | + with: |
| 74 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 75 | + edit-mode: replace |
| 76 | + body: | |
| 77 | + ${{ env.comment_message }} |
| 78 | +
|
| 79 | + 🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }} |
| 80 | + - name: Download Artifact site |
| 81 | + if: | |
| 82 | + github.event.workflow_run.conclusion == 'success' |
| 83 | + && steps.find-pull-request.outputs.number != '' |
| 84 | + && steps.fc.outputs.comment-id != '' |
| 85 | + |
| 86 | + with: |
| 87 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + workflow: ci.yaml |
| 89 | + run_id: ${{ github.event.workflow_run.id }} |
| 90 | + name: site-zip |
| 91 | + - name: Unzip site |
| 92 | + if: | |
| 93 | + github.event.workflow_run.conclusion == 'success' |
| 94 | + && steps.find-pull-request.outputs.number != '' |
| 95 | + && steps.fc.outputs.comment-id != '' |
| 96 | + run: | |
| 97 | + rm -rf ./portal/_build/html |
| 98 | + unzip site.zip |
| 99 | + rm -f site.zip |
| 100 | + # Push the site's HTML to Netlify and get the preview URL |
| 101 | + - name: Deploy to Netlify |
| 102 | + if: | |
| 103 | + github.event.workflow_run.conclusion == 'success' |
| 104 | + && steps.find-pull-request.outputs.number != '' |
| 105 | + && steps.fc.outputs.comment-id != '' |
| 106 | + id: netlify |
| 107 | + |
| 108 | + with: |
| 109 | + publish-dir: ./portal/_build/html |
| 110 | + production-deploy: false |
| 111 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 112 | + enable-commit-comment: false |
| 113 | + env: |
| 114 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 115 | + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
| 116 | + timeout-minutes: 5 |
| 117 | + - name: Update site Preview comment |
| 118 | + if: | |
| 119 | + github.event.workflow_run.conclusion == 'success' |
| 120 | + && steps.find-pull-request.outputs.number != '' |
| 121 | + && steps.fc.outputs.comment-id != '' |
| 122 | + uses: peter-evans/create-or-update-comment@v1 |
| 123 | + with: |
| 124 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 125 | + edit-mode: replace |
| 126 | + body: | |
| 127 | + ${{ env.comment_message }} |
| 128 | +
|
| 129 | + 🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }} |
| 130 | + ✅ Deployment Preview URL: ${{ steps.netlify.outputs.deploy-url }} |
0 commit comments