Deploy pull request preview #2242
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
name: Deploy pull request preview | |
# Setup and build files with hugo | |
# https://github.com/peaceiris/actions-hugo | |
# Deploy to GitHub pages | |
# https://github.com/peaceiris/actions-gh-pages | |
# Triggered by the build pull request preview | |
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | |
on: | |
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_run | |
workflow_run: | |
workflows: ["Build pull request preview"] | |
types: [completed] | |
jobs: | |
deploy_pr_preview: | |
# Only run if PR preview build was successful | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
name: Deploy pull request preview | |
runs-on: ubuntu-latest | |
steps: | |
- name: "Download artifact" | |
uses: actions/github-script@v6 | |
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" | |
})[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)); | |
- name: Extract pull request preview | |
id: extract | |
run: | | |
unzip pr.zip | |
PR_NUMBER="$(cat ./PR)" | |
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | |
- name: Deploy Pull Request preview | |
uses: peaceiris/actions-gh-pages@v3 | |
with: | |
# Use GITHUB_TOKEN allowing to write to local repository | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
# Purge older files from a given PR | |
keep_files: false | |
# Accessible at http://docs.egi.eu/documentation/ | |
# XXX use a different domain | |
# cname: docs.egi.eu | |
# Branch to push to | |
publish_branch: pr_previews | |
# Source directory | |
publish_dir: ./${{ steps.extract.outputs.pr_number }}/ | |
# Destitnation directory | |
destination_dir: ${{ steps.extract.outputs.pr_number }} | |
# Reuse PR commit message | |
commit_message: ${{ github.event.head_commit.message }} | |
- name: Update Pull Request | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
var fs = require('fs'); | |
var issue_number = Number('${{steps.extract.outputs.pr_number}}'); | |
const output = `#### Documentation preview deployed! | |
Available at https://docs.egi.eu/documentation/${issue_number}`; | |
github.rest.issues.createComment({ | |
issue_number: issue_number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) |