From 1671275cef4bc5d561cb978bc5437f540883bb18 Mon Sep 17 00:00:00 2001 From: Claire Carouge Date: Fri, 7 Oct 2022 10:59:37 +1100 Subject: [PATCH] PR preview not building for members without write access Fixes #121 --- .../{preview.yml => build_preview.yml} | 41 +++++----- .github/workflows/clean_preview.yml | 51 +++++++++++++ .github/workflows/close_pr.yml | 19 +++++ .github/workflows/deploy_preview.yml | 75 +++++++++++++++++++ 4 files changed, 165 insertions(+), 21 deletions(-) rename .github/workflows/{preview.yml => build_preview.yml} (56%) create mode 100644 .github/workflows/clean_preview.yml create mode 100644 .github/workflows/close_pr.yml create mode 100644 .github/workflows/deploy_preview.yml diff --git a/.github/workflows/preview.yml b/.github/workflows/build_preview.yml similarity index 56% rename from .github/workflows/preview.yml rename to .github/workflows/build_preview.yml index e2e974e2a..e3ddca6cb 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/build_preview.yml @@ -1,33 +1,19 @@ -name: Deploy PR preview +name: Build PR preview on: pull_request: types: - - labeled + - opened + - reopened - synchronize - - closed concurrency: group: preview-${{ github.ref }} cancel-in-progress: true -permissions: - actions: write - checks: write - contents: write - deployments: write - issues: write - packages: write - pull-requests: write - pages: write - repository-projects: write - security-events: write - statuses: write - jobs: - deploy-preview: + build-preview: runs-on: ubuntu-latest - if: contains(github.event.pull_request.labels.*.name, 'safe to test') steps: - name: Checkout uses: actions/checkout@v3 @@ -45,7 +31,20 @@ jobs: run: | mkdocs build -f mkdocs.yml -d site - - name: Deploy preview - uses: access-nri/pr-preview-action@v1.1 + - name: Save PR number + run: | + mkdir -p ./pr + echo ${{ github.event.number }} > ./pr/NB + - uses: actions/upload-artifact@v2 + with: + name: pr + path: pr/ + + - name: Save built site + run: | + mkdir -p ./pr_site + mv site ./pr_site/. + - uses: actions/upload-artifact@v2 with: - source-dir: site \ No newline at end of file + name: pr_site + path: pr_site/ \ No newline at end of file diff --git a/.github/workflows/clean_preview.yml b/.github/workflows/clean_preview.yml new file mode 100644 index 000000000..42fa35a31 --- /dev/null +++ b/.github/workflows/clean_preview.yml @@ -0,0 +1,51 @@ +name: Clean PR preview + +on: + workflow_run: + workflows: ["Close PR"] + types: + - completed + +jobs: + close-preview: + runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' + steps: + - name: "Download PR number" + uses: actions/github-script@v3.1.0 + with: + script: | + var artifacts = await github.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: ${{github.event.workflow_run.id }}, + }); + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "prnumber" + })[0]; + var download = await github.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + var fs = require('fs'); + fs.writeFileSync('${{github.workspace}}/prnumber.zip', Buffer.from(download.data)); + - run: | + unzip prnumber.zip + echo "pr-number=$(cat prnumber.txt)" >> "$GITHUB_ENV" + shell: bash + + - name: Checkout + uses: actions/checkout@v3 + - uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: Clean preview + uses: access-nri/pr-preview-action@v2 + with: + action: remove + pr-number: ${{ env.pr-number }} \ No newline at end of file diff --git a/.github/workflows/close_pr.yml b/.github/workflows/close_pr.yml new file mode 100644 index 000000000..001b35365 --- /dev/null +++ b/.github/workflows/close_pr.yml @@ -0,0 +1,19 @@ +name: Close PR + +on: + pull_request: + types: + - closed + +jobs: + send-number: + runs-on: ubuntu-latest + steps: + - name: Save PR number + run: | + mkdir -p ./prnumber + echo ${{ github.event.number }} > ./prnumber/prnumber.txt + - uses: actions/upload-artifact@v2 + with: + name: prnumber + path: prnumber/ \ No newline at end of file diff --git a/.github/workflows/deploy_preview.yml b/.github/workflows/deploy_preview.yml new file mode 100644 index 000000000..a15fb6a0c --- /dev/null +++ b/.github/workflows/deploy_preview.yml @@ -0,0 +1,75 @@ +name: Deploy PR preview + +on: + workflow_run: + workflows: ["Build PR preview"] + types: + - completed + +jobs: + deploy-preview: + runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' + steps: + - name: Checkout + uses: actions/checkout@v3 + + - uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: "Download PR number" + uses: actions/github-script@v3.1.0 + with: + script: | + var artifacts = await github.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: ${{github.event.workflow_run.id }}, + }); + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "pr" + })[0]; + var download = await github.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + var fs = require('fs'); + fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); + - run: | + unzip pr.zip + echo "pr-number=$(cat NB)" >> "$GITHUB_ENV" + shell: bash + + - name: "Download built site" + uses: actions/github-script@v3.1.0 + with: + script: | + var artifacts = await github.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: ${{github.event.workflow_run.id }}, + }); + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { + return artifact.name == "pr_site" + })[0]; + var download = await github.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + var fs = require('fs'); + fs.writeFileSync('${{github.workspace}}/pr_site.zip', Buffer.from(download.data)); + - run: unzip pr_site.zip + + - name: Deploy preview + uses: access-nri/pr-preview-action@v2 + with: + source-dir: site + action: deploy + pr-number: ${{ env.pr-number }} \ No newline at end of file