deploy-preview #161
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-preview | |
on: | |
workflow_run: | |
workflows: | |
- build-book | |
types: | |
- requested | |
- completed | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash -l {0} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Fetch Repo Name | |
id: repo-name | |
run: echo "::set-output name=value::$(echo '${{ github.repository }}' | awk -F '/' '{print $2}')" # just the repo name, not owner | |
- name: Set message value | |
run: | | |
echo "comment_message=π Thanks for opening this PR! The book will be automatically built with [GitHub Actions](https://github.com/features/actions). To see the status of your deployment, click below." >> $GITHUB_ENV | |
- name: Find Pull Request | |
uses: actions/github-script@v7 | |
id: find-pull-request | |
with: | |
script: | | |
let pullRequestNumber = '' | |
let pullRequestHeadSHA = '' | |
core.info('Finding pull request...') | |
const pullRequests = await github.rest.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 preview comment | |
uses: peter-evans/find-comment@v3 | |
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 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@v4 | |
with: | |
issue-number: ${{ steps.find-pull-request.outputs.number }} | |
body: | | |
${{ env.comment_message }} | |
π Git commit SHA: ${{ steps.find-pull-request.outputs.sha }} | |
β Deployment Preview URL: In Progress | |
- name: Update 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@v4 | |
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: In Progress | |
- name: Download Artifact Book | |
if: | | |
github.event.workflow_run.conclusion == 'success' | |
&& steps.find-pull-request.outputs.number != '' | |
&& steps.fc.outputs.comment-id != '' | |
uses: dawidd6/action-download-artifact@v3.1.4 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
workflow: build-book.yaml | |
run_id: ${{ github.event.workflow_run.id }} | |
name: book-zip-${{ steps.find-pull-request.outputs.number }} | |
- name: Unzip the book | |
if: | | |
github.event.workflow_run.conclusion == 'success' | |
&& steps.find-pull-request.outputs.number != '' | |
&& steps.fc.outputs.comment-id != '' | |
run: | | |
rm -rf _build/html | |
unzip book.zip | |
rm -f book.zip | |
# Push the book's HTML to github-pages | |
# This will be published at /_preview/PRnumber/ relative to the main site | |
- name: Deploy to GitHub pages | |
id: deploy | |
if: | | |
github.event.workflow_run.conclusion == 'success' | |
&& steps.find-pull-request.outputs.number != '' | |
&& steps.fc.outputs.comment-id != '' | |
uses: peaceiris/actions-gh-pages@v3.9.3 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
publish_dir: _build/html | |
enable_jekyll: false | |
destination_dir: _preview/${{ steps.find-pull-request.outputs.number }} | |
- name: Finalize preview comment | |
if: | | |
github.event.workflow_run.conclusion == 'success' | |
&& steps.find-pull-request.outputs.number != '' | |
&& steps.fc.outputs.comment-id != '' | |
&& steps.deploy.conclusion == 'success' | |
uses: peter-evans/create-or-update-comment@v4 | |
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: https://${{ github.repository_owner }}.github.io/${{ steps.repo-name.outputs.value }}/_preview/${{ steps.find-pull-request.outputs.number }} |