Regenerate GitHub Pages Site #55
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: Regenerate GitHub Pages Site | |
on: | |
workflow_run: | |
workflows: ["Nix Checks"] | |
types: | |
- completed | |
jobs: | |
pr-context: | |
name: 'Acquire PR Context' | |
runs-on: ubuntu-24.04 | |
outputs: | |
PR_HEADSHA: ${{ steps.set-pr-context.outputs.head-sha }} | |
PR_NUMBER: ${{ steps.set-pr-context.outputs.number }} | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
steps: | |
- name: 'Get PR context' | |
id: set-pr-context | |
if: ${{ github.event.workflow_run.event == 'pull_request' }} | |
env: | |
GH_TOKEN: ${{ github.token }} | |
PR_TARGET_REPO: ${{ github.repository }} | |
PR_BRANCH: |- | |
${{ | |
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login) | |
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) | |
|| github.event.workflow_run.head_branch | |
}} | |
run: | | |
gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \ | |
--json 'number,headRefOid' \ | |
--jq '"number=\(.number)\nhead-sha=\(.headRefOid)"' \ | |
>> $GITHUB_OUTPUT | |
- name: 'Fake PR context' | |
if: ${{ github.event.workflow_run.event != 'pull_request' }} | |
run: | | |
echo 'number=no-number' >> $GITHUB_OUTPUT | |
echo 'head-sha=no-head-sha' >> $GITHUB_OUTPUT | |
regenerate: | |
name: 'Regenerate Site' | |
runs-on: ubuntu-latest | |
needs: [pr-context] | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
env: | |
PR_HEADSHA: ${{ needs.pr-context.outputs.PR_HEADSHA }} | |
PR_NUMBER: ${{ needs.pr-context.outputs.PR_NUMBER }} | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
repository: jerith666/elbum-demo | |
submodules: true | |
token: ${{ secrets.ELBUM_DEMO_CONTENTS_WRITE }} | |
- uses: cachix/install-nix-action@v18 | |
with: | |
nix_path: nixpkgs=channel:nixos-unstable | |
- uses: DeterminateSystems/magic-nix-cache-action@main | |
- name: Download elbum closure artifact | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
workflow: ${{ github.event.workflow_run.workflow_id }} | |
run_id: ${{ github.event.workflow_run.id }} | |
name: elbum.nar | |
path: ./ | |
- name: Import elbum closure into nix store | |
run: | | |
ln -s $(nix-store --import < elbum.nar | tail -1) elbum | |
rm elbum.nar | |
- name: Dump Event for Debugging | |
run: | | |
cat <<EOF | |
${{ toJSON(github.event) }} | |
EOF | |
- name: Regenerate site | |
run: | | |
# decide how to behave based on whether triggered by | |
# a PR or a push to the main branch: | |
if [[ "${{ github.event.workflow_run.event }}" == "pull_request" ]]; then | |
PR_NUMBER="${{ env.PR_NUMBER }}" | |
if [ -n "$PR_NUMBER" ]; then | |
TARGET="pr/${PR_NUMBER}" | |
HOME="https://github.com/jerith666/elbum/pull/${PR_NUMBER}" | |
else | |
echo "Error: Could not extract PR number from event data." | |
exit 1 | |
fi | |
else | |
TARGET=. | |
HOME="https://github.com/jerith666/elbum" | |
fi | |
# generate album | |
echo "generating album in ${TARGET}" | |
./elbum/bin/elbum "test-albums/gh-pages-src/Elbum Demo Album/" "${TARGET}" | |
# resolve symlinks into the nix store | |
( | |
cd "${TARGET}"; | |
rm -v elbum.js; cp -v elbum/elbum.js .; | |
rm -v index.html; cp -v elbum/index.html .; | |
rm -v .htaccess; cp -v elbum/.htaccess .; | |
echo "${HOME}" > home; | |
rm -v elbum; | |
) | |
# commit generated album | |
git config user.name "Elbum Site Updater Bot" | |
git config user.email "[email protected]" | |
git add "${TARGET}" | |
git commit -m "regenerate album in ${TARGET} using code from ${{ github.event.workflow_run.head_sha }}" || echo "No changes to commit" | |
# push generated album | |
git push | |
- name: Link from PR to generated site | |
if: github.event.workflow_run.event == 'pull_request' | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
PR_NUMBER="${{ env.PR_NUMBER }}" | |
if [ -n "$PR_NUMBER" ]; then | |
gh repo clone jerith666/elbum elbum-repo | |
cd elbum-repo | |
gh pr comment "${PR_NUMBER}" --body "[Elbum Demo Site using this PR's code](https://jerith666.github.io/elbum-demo/pr/${PR_NUMBER})" | |
else | |
echo "Error: Could not extract PR number from event data." | |
fi |