Skip to content

Commit

Permalink
workflows: add PR previewing
Browse files Browse the repository at this point in the history
  • Loading branch information
ES-Alexander committed Dec 8, 2024
1 parent 7ea9e48 commit 00782ff
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/pr-build-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build PR Preview

on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{ github.event.pull_request.head.repo.full_name }}

- name: Build with Zola
uses: shalzz/zola-deploy-action@v0.19.2
env:
BUILD_ONLY: true
BUILD_FLAGS: --base-url http://br-www-docs.s3-website-us-east-1.amazonaws.com/preview/cockpit/${{ github.event.number }}

- name: Save PR number
run: |
mkdir -p ./pr
echo ${{ github.event.number }} > ./pr/NR
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: pr-preview
path: |
pr/
public/
55 changes: 55 additions & 0 deletions .github/workflows/pr-deploy-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Deploy PR Preview

on:
workflow_run:
workflows: ["Build PR Preview"]
types:
- completed

jobs:
upload:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: Download artifact
uses: actions/github-script@v7
with:
script: |
var artifacts = await github.rest.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-preview"
})[0];
var download = await github.rest.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

- name: Retrieve PR Number
run: |
echo "PR=$(cat pr/NR)" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Upload to AWS
run: |
# Delete the files from the previous iteration of this PR (if they exist)
aws s3 rm --recursive s3://br-www-docs/preview/cockpit/${{ env.PR }}
# Upload the new ones
aws s3 cp --recursive public s3://br-www-docs/preview/cockpit/${{ env.PR }}
echo "Preview at http://br-www-docs.s3-website-us-east-1.amazonaws.com/preview/cockpit/${{ env.PR }}"
20 changes: 20 additions & 0 deletions .github/workflows/pr-remove-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Remove PR Preview

on:
pull_request_target:
types: [closed]

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Remove proposal preview
run: |
aws s3 rm --recursive s3://br-www-docs/preview/cockpit/${{ github.event.pull_request.number }}

0 comments on commit 00782ff

Please sign in to comment.