Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 127 additions & 53 deletions .github/workflows/demo_deploy.yml
Original file line number Diff line number Diff line change
@@ -1,80 +1,154 @@
name: Deploy PR branches
on:
on:
pull_request:
branches-ignore: "assets-update"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TARGET_REPO: "mkdocs-demo-deploy"
TARGET_OWNER: "CallumWalley"
WORKFLOW_ID: "deploy.yml"
DEPLOY_URL: "https://callumwalley.github.io/mkdocs-demo-deploy"
HEAD: ${{ github.event.pull_request.head.ref }}
permissions: write-all
GH_REPO: ${{ github.repository }}
NO_MKDOCS_2_WARNING: "1"
permissions:
contents: read
pull-requests: write
concurrency:
group: demo-deploy-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
demo-deploy:
continue-on-error: true
name: Trigger test deployments
runs-on: ubuntu-24.04
steps:
- name: Trigger Workflow in Another Repository
continue-on-error: true
id: trigger
run: |
set -x
set -o xtrace
curl -L \
echo "dispatch_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_OUTPUT"
curl -fsSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.PAT }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${TARGET_OWNER}/${TARGET_REPO}/dispatches \
-d "{\"event_type\":\"deploy\",\"client_payload\":{\"targets\":\"${GITHUB_REPOSITORY}:${HEAD}\", \"use-cache\":\"true\"}}"

# This would be better if it worked
# - name: Run 'deploy.yml' Workflow
# uses: convictional/trigger-workflow-and-wait@v1.6.1
# with:
# owner: ${TARGET_OWNER}
# repo: ${TARGET_REPO}
# github_token: ${{ secrets.PAT }}
# workflow_file_name: deploy.yml
# client_payload: '{"targets":"${GITHUB_REPOSITORY}:${HEAD}", "use-cache":"true"}'
# - name: Wait for Workflow Action
# run: |
# curl -L \
# -X POST \
# -H "Accept: application/vnd.github+json" \
# -H "Authorization: Bearer ${{ secrets.PAT }}" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# https://api.github.com/repos/${TARGET_OWNER}/${TARGET_REPO}/actions/runs/deploy.yml
- name: Wait for Workflow Action
- name: Post initial loading comment
id: comment
run: |
# Just give a minute or so to deploy
sleep 90
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
msg="Deployment in progress...<br><br>\
<img src='https://c.tenor.com/tYyW3H6uO8oAAAAC/tenor.gif' width=20% \
alt='kakapo'><br><br>Waiting for test environment to finish building, please be patient ☺️"
gh pr comment "${HEAD}" --edit-last --create-if-none --body "${msg}"
- name: Wait for target workflow to complete
id: wait
env:
GH_TOKEN: ${{ secrets.PAT }}
TIMEOUT_SECONDS: 600
run: |
finish() {
echo "conclusion=$1" >> "$GITHUB_OUTPUT"
echo "completed_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_OUTPUT"
}

# Only consider runs created after we dispatched, so a stale
# already-completed run isn't mistaken for the one we just triggered.
echo "Looking for a run created after ${{ steps.trigger.outputs.dispatch_time }}..."
run_id=""
while [ -z "$run_id" ]; do
if [ "$SECONDS" -ge "$TIMEOUT_SECONDS" ]; then
echo "Timed out waiting for the run to appear."
finish "timed_out"
exit 0
fi
run_id=$(gh api -X GET "repos/${TARGET_OWNER}/${TARGET_REPO}/actions/workflows/${WORKFLOW_ID}/runs" \
-f "created=>${{ steps.trigger.outputs.dispatch_time }}" \
--jq '.workflow_runs[-1].id // empty')
[ -z "$run_id" ] && sleep 10
done
echo "Tracking run $run_id"

while true; do
if [ "$SECONDS" -ge "$TIMEOUT_SECONDS" ]; then
echo "Timed out waiting for the run to complete."
finish "timed_out"
exit 0
fi
run_json=$(gh api "repos/${TARGET_OWNER}/${TARGET_REPO}/actions/runs/${run_id}")
status=$(echo "$run_json" | jq -r '.status')
echo "Status: $status"
if [ "$status" = "completed" ]; then
finish "$(echo "$run_json" | jq -r '.conclusion')"
break
fi
sleep 10
done

- name: Post messages open requests
continue-on-error: true
run: |
msg="Test deployment available at <a href=\"${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}\">${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}</a>"
changed_pages="$(git diff --name-only origin/main -- '*.md')"
# Logic for truncating out long sections commented out.
# maxlines=-5
if [ -n "${changed_pages}" ]; then
msg="${msg}<br><br>Seems the following pages differ;<br><ul>"
for f in ${changed_pages};do
# maxlines=((maxlines+1))
#if [ ${maxlines} -lt 1 ]; then
g=${f#*/}; h=${g%.*}
msg="${msg}<li><a href=\"${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}/${h}\" target=_blank>${h##*/}</a></li>"
#fi
done
# if [ ${maxlines} -gt 0 ];then
# msg="${msg}<li> ... and ${maxlines} more.</li>"
# fi
msg="${msg}</ul>"
set -x
echo $0
conclusion="${{ steps.wait.outputs.conclusion }}"
finished="${{ steps.wait.outputs.completed_at }}"

if [ "$conclusion" = "success" ]; then
msg="<strong>Test deployment successful!!</strong> (${finished})<br>Preview available at \
<a href=\"${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}\">${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}</a>"
# Get diff of main branch and feature branch.
changed_pages=$(gh api repos/${GITHUB_REPOSITORY}/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }}\
--jq '.files[] | select(.filename | endswith(".md")) | .filename')
# If changes append to message.
if [ -n "${changed_pages}" ]; then
msg="${msg}<br><br>Seems the following pages differ;<br><ul>"
for f in ${changed_pages};do
g=${f#*/}; h=${g%.*}
msg="${msg}<li><a href=\"${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}/${h}\" target=_blank>${h##*/}</a></li>"
done
msg="${msg}</ul>"
echo "::info title=Deploy Succeeded::${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}"
fi
msg="${msg}<br><br><em><a href=\"${DEPLOY_URL}\">See all deployed demo sites</a></em>"
else
msg="Deployment failed (${conclusion})"
echo "::error title=Deploy failed::${conclusion}"
fi
msg="${msg}<br><br><em><a href="${DEPLOY_URL}">See all deployed demo sites</a></em>"
(gh pr comment ${HEAD} --edit-last --body "${msg}") || (gh pr comment ${HEAD} --body "${msg}")
echo "::info title=Deploy successful::${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}"
# Update message in pull request.
gh pr comment "${HEAD}" --edit-last --create-if-none --body "${msg}"

- name: Map changed pages to preview URLs
if: steps.wait.outputs.conclusion == 'success'
id: pages
run: |
changed_pages=$(gh api repos/${GITHUB_REPOSITORY}/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }} \
--jq '.files[] | select(.filename | endswith(".md")) | .filename')
urls="${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}/"$'\n'
for f in ${changed_pages}; do
g=${f#*/}; h=${g%.*}
urls="${urls}${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}/${h}/"$'\n'
done
echo "urls<<EOF" >> $GITHUB_OUTPUT
echo "$urls" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

- name: Wait for preview URLs to be live
if: steps.wait.outputs.conclusion == 'success'
run: |
# GitHub Pages CDN can lag behind the deploy; a URL that 404s serves
# GitHub's default 404 page, whose strict CSP breaks AccessLint's
# script injection. Poll each URL until it returns 200 first.
while IFS= read -r url; do
[ -z "$url" ] && continue
echo "Waiting for $url"
for i in $(seq 1 30); do
code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
if [ "$code" = "200" ]; then
echo " OK ($code)"
break
fi
echo " Got $code, retrying (${i}/30)..."
sleep 5
done
done <<< "${{ steps.pages.outputs.urls }}"
with:
header: a11y
path: ${{ steps.a11y.outputs.report-markdown-path }}
Loading