diff --git a/.github/workflows/demo_deploy.yml b/.github/workflows/demo_deploy.yml index 4efb5f3..5b14326 100644 --- a/.github/workflows/demo_deploy.yml +++ b/.github/workflows/demo_deploy.yml @@ -1,7 +1,6 @@ name: Deploy PR branches -on: +on: pull_request: - branches-ignore: "assets-update" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} TARGET_REPO: "mkdocs-demo-deploy" @@ -9,72 +8,147 @@ env: 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...

\ + kakapo

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 ${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}" - 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}

Seems the following pages differ;
" + set -x + echo $0 + conclusion="${{ steps.wait.outputs.conclusion }}" + finished="${{ steps.wait.outputs.completed_at }}" + + if [ "$conclusion" = "success" ]; then + msg="Test deployment successful!! (${finished})
Preview available at \ + ${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}" + # 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}

Seems the following pages differ;
" + echo "::info title=Deploy Succeeded::${DEPLOY_URL}/${GITHUB_REPOSITORY}/${HEAD}" + fi + msg="${msg}

See all deployed demo sites" + else + msg="Deployment failed (${conclusion})" + echo "::error title=Deploy failed::${conclusion}" fi - msg="${msg}

See all deployed demo sites" - (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<> $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 }}