Skip to content

Commit

Permalink
PR preview not building for members without write access
Browse files Browse the repository at this point in the history
Fixes #121
  • Loading branch information
ccarouge committed Oct 7, 2022
1 parent 73cb30b commit 1671275
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -45,7 +31,20 @@ jobs:
run: |
mkdocs build -f mkdocs.yml -d site
- name: Deploy preview
uses: access-nri/[email protected]
- 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
name: pr_site
path: pr_site/
51 changes: 51 additions & 0 deletions .github/workflows/clean_preview.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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 }}
19 changes: 19 additions & 0 deletions .github/workflows/close_pr.yml
Original file line number Diff line number Diff line change
@@ -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/
75 changes: 75 additions & 0 deletions .github/workflows/deploy_preview.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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/[email protected]
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 }}

0 comments on commit 1671275

Please sign in to comment.