From 16aced6e8eac8b43ebf19e5c72ce6a7b6a491571 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Thu, 23 Sep 2021 15:40:31 -0600 Subject: [PATCH 1/2] Preview PRs via Netlify + GitHub Actions --- .github/workflows/ci.yaml | 28 ++++++-- .github/workflows/preview.yaml | 127 +++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/preview.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9f3ee68b..dec0a1e0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,17 +1,20 @@ name: deploy-site - on: push: - branches: - - main - + pull_request: + workflow_dispatch: jobs: - deploy-site: + build: runs-on: ubuntu-latest defaults: run: shell: bash -l {0} + if: github.repository == 'ProjectPythia/projectpythia.github.io' steps: + - name: Cancel previous runs + uses: styfle/cancel-workflow-action@0.9.1 + with: + access_token: ${{ github.token }} - uses: actions/checkout@v2 - uses: conda-incubator/setup-miniconda@v2 @@ -30,9 +33,22 @@ jobs: conda env list cd portal make -j4 html + - name: Zip the site + run: | + set -x + set -e + if [ -f site.zip ]; then + rm -rf site.zip + fi + zip -r site.zip ./portal/_build/html + - uses: actions/upload-artifact@v2 + with: + name: site-zip + path: ./site.zip - name: Deploy - uses: peaceiris/actions-gh-pages@v3.7.3 + if: github.ref == 'refs/heads/main' + uses: peaceiris/actions-gh-pages@v3.8.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./portal/_build/html diff --git a/.github/workflows/preview.yaml b/.github/workflows/preview.yaml new file mode 100644 index 00000000..74c138ee --- /dev/null +++ b/.github/workflows/preview.yaml @@ -0,0 +1,127 @@ +name: preview-site +on: + workflow_run: + workflows: + - deploy-site + types: + - requested + - completed +jobs: + deploy: + if: github.repository == 'ProjectPythia/projectpythia.github.io' + runs-on: ubuntu-latest + defaults: + run: + shell: bash + steps: + - uses: actions/checkout@v2 + - name: Set message value + run: | + 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 + - name: Find Pull Request + uses: actions/github-script@v4 + id: find-pull-request + with: + script: | + let pullRequestNumber = '' + let pullRequestHeadSHA = '' + core.info('Finding pull request...') + const pullRequests = await github.pulls.list({owner: context.repo.owner, repo: context.repo.repo}) + for (let pullRequest of pullRequests.data) { + if(pullRequest.head.sha === context.payload.workflow_run.head_commit.id) { + pullRequestHeadSHA = pullRequest.head.sha + pullRequestNumber = pullRequest.number + break + } + } + core.setOutput('number', pullRequestNumber) + core.setOutput('sha', pullRequestHeadSHA) + if(pullRequestNumber === '') { + core.info( + `No pull request associated with git commit SHA: ${context.payload.workflow_run.head_commit.id}` + ) + } + else{ + core.info(`Found pull request ${pullRequestNumber}, with head sha: ${pullRequestHeadSHA}`) + } + - name: Find Comment + uses: peter-evans/find-comment@v1 + if: steps.find-pull-request.outputs.number != '' + id: fc + with: + issue-number: '${{ steps.find-pull-request.outputs.number }}' + comment-author: 'github-actions[bot]' + body-includes: '${{ env.comment_message }}' + - name: Create comment + if: | + github.event.workflow_run.conclusion != 'success' + && steps.find-pull-request.outputs.number != '' + && steps.fc.outputs.comment-id == '' + uses: peter-evans/create-or-update-comment@v1 + with: + issue-number: ${{ steps.find-pull-request.outputs.number }} + body: | + ${{ env.comment_message }} + 🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }} + - name: Update comment + if: | + github.event.workflow_run.conclusion != 'success' + && steps.find-pull-request.outputs.number != '' + && steps.fc.outputs.comment-id != '' + uses: peter-evans/create-or-update-comment@v1 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + edit-mode: replace + body: | + ${{ env.comment_message }} + 🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }} + - name: Download Artifact site + if: | + github.event.workflow_run.conclusion == 'success' + && steps.find-pull-request.outputs.number != '' + && steps.fc.outputs.comment-id != '' + uses: dawidd6/action-download-artifact@v2.14.1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + workflow: ci.yaml + run_id: ${{ github.event.workflow_run.id }} + name: site-zip + - name: Unzip site + if: | + github.event.workflow_run.conclusion == 'success' + && steps.find-pull-request.outputs.number != '' + && steps.fc.outputs.comment-id != '' + run: | + rm -rf ./portal/_build/html + unzip site.zip + rm -f site.zip + # Push the site's HTML to Netlify and get the preview URL + - name: Deploy to Netlify + if: | + github.event.workflow_run.conclusion == 'success' + && steps.find-pull-request.outputs.number != '' + && steps.fc.outputs.comment-id != '' + id: netlify + uses: nwtgck/actions-netlify@v1.2 + with: + publish-dir: ./portal/_build/html + production-deploy: false + github-token: ${{ secrets.GITHUB_TOKEN }} + enable-commit-comment: false + env: + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} + timeout-minutes: 5 + - name: Update site Preview comment + if: | + github.event.workflow_run.conclusion == 'success' + && steps.find-pull-request.outputs.number != '' + && steps.fc.outputs.comment-id != '' + uses: peter-evans/create-or-update-comment@v1 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + edit-mode: replace + body: | + ${{ env.comment_message }} + 🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }} + ✅ Deployment Preview URL: ${{ steps.netlify.outputs.deploy-url }} From e7011c5ad35941f8f19d2f2f22a8a18ab92a65b6 Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Thu, 23 Sep 2021 16:35:42 -0600 Subject: [PATCH 2/2] Fix spaces --- .github/workflows/preview.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/preview.yaml b/.github/workflows/preview.yaml index 74c138ee..af7d54e3 100644 --- a/.github/workflows/preview.yaml +++ b/.github/workflows/preview.yaml @@ -62,6 +62,7 @@ jobs: issue-number: ${{ steps.find-pull-request.outputs.number }} body: | ${{ env.comment_message }} + 🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }} - name: Update comment if: | @@ -74,6 +75,7 @@ jobs: edit-mode: replace body: | ${{ env.comment_message }} + 🚧 Deployment in progress for git commit SHA: ${{ steps.find-pull-request.outputs.sha }} - name: Download Artifact site if: | @@ -123,5 +125,6 @@ jobs: edit-mode: replace body: | ${{ env.comment_message }} + 🔍 Git commit SHA: ${{ steps.find-pull-request.outputs.sha }} ✅ Deployment Preview URL: ${{ steps.netlify.outputs.deploy-url }}