| 1 | name: Import campaign |
| 2 | |
| 3 | # Imports a varied set of public GitHub repositories into Diffinery as private test |
| 4 | # repositories (<owner>/campaign-*), optionally runs their push workflows there, and reports what |
| 5 | # came across and what ran (scripts/import-campaign.sh). Started by hand. |
| 6 | # |
| 7 | # Dispatched on GitHub, it starts the same workflow on Diffinery and prints its report; |
| 8 | # on Diffinery it does the work. Imports run token-free (Diffinery keeps no GitHub |
| 9 | # credentials), so GitHub's anonymous rate limit applies and large repositories take hours. |
| 10 | on: |
| 11 | workflow_dispatch: |
| 12 | inputs: |
| 13 | repos: |
| 14 | description: GitHub repositories to import, separated by spaces |
| 15 | type: string |
| 16 | default: >- |
| 17 | octocat/git-consortium sindresorhus/slugify expressjs/cors spf13/pflag |
| 18 | pallets/markupsafe BurntSushi/walkdir actions/hello-world-javascript-action |
| 19 | actions/hello-world-docker-action jekyll/minima |
| 20 | minutes: |
| 21 | description: How long to wait for imports and workflow runs, in minutes |
| 22 | type: string |
| 23 | default: "300" |
| 24 | run_workflows: |
| 25 | description: Also run the imported repositories' push workflows (third-party code) |
| 26 | type: boolean |
| 27 | default: false |
| 28 | follow_run: |
| 29 | description: Instead of starting a campaign, print the report and logs of this Diffinery run (its id) |
| 30 | type: string |
| 31 | default: "" |
| 32 | peek: |
| 33 | description: Instead of starting a campaign, print how the running one is doing (imports, steps, runs) and stop |
| 34 | type: boolean |
| 35 | default: false |
| 36 | rerun_failed: |
| 37 | description: Instead of starting a campaign, re-run the failed jobs of this Diffinery run of an imported repository ("<repository> <run id>"), or only the job whose name contains the rest ("<repository> <run id> <job name part>"), and print how they end |
| 38 | type: string |
| 39 | default: "" |
| 40 | push_test: |
| 41 | description: Instead of starting a campaign, push this ref of a GitHub repository ("<repository> <ref>") to a new private Diffinery repository in one push, and clone it back |
| 42 | type: string |
| 43 | default: "" |
| 44 | inspect_run: |
| 45 | description: Instead of starting a campaign, print the jobs of this Diffinery run of an imported repository ("<repository> [<run id>]", the newest run without an id), with each failed job's runner, steps and log |
| 46 | type: string |
| 47 | default: "" |
| 48 | |
| 49 | permissions: |
| 50 | contents: read |
| 51 | |
| 52 | # A peek or an inspection runs next to the campaign it looks at; campaigns queue behind each other. |
| 53 | concurrency: |
| 54 | group: ${{ (inputs.peek || inputs.inspect_run != '' || inputs.push_test != '') && 'import-campaign-peek' || 'import-campaign' }} |
| 55 | cancel-in-progress: false |
| 56 | |
| 57 | jobs: |
| 58 | relay: |
| 59 | name: Start the campaign on Diffinery and print its report |
| 60 | if: ${{ github.server_url == 'https://github.com' }} |
| 61 | runs-on: ubuntu-latest |
| 62 | timeout-minutes: 360 |
| 63 | environment: production |
| 64 | env: |
| 65 | DIFFINERY: https://diffinery.com |
| 66 | TOKEN: ${{ secrets.DIFFINERY_LIVE_TOKEN }} |
| 67 | R: codician/diffinery |
| 68 | steps: |
| 69 | - name: Dispatch and follow |
| 70 | env: |
| 71 | REPOS: ${{ inputs.repos }} |
| 72 | MINUTES: ${{ inputs.minutes }} |
| 73 | RUN_WORKFLOWS: ${{ inputs.run_workflows }} |
| 74 | FOLLOW_RUN: ${{ inputs.follow_run }} |
| 75 | PEEK: ${{ inputs.peek }} |
| 76 | RERUN_FAILED: ${{ inputs.rerun_failed }} |
| 77 | INSPECT_RUN: ${{ inputs.inspect_run }} |
| 78 | PUSH_TEST: ${{ inputs.push_test }} |
| 79 | run: | |
| 80 | set -euo pipefail |
| 81 | api() { curl -fsS --retry 3 -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 82 | if [ -n "$PUSH_TEST" ]; then |
| 83 | # One large push, as `git push` sends it: no import, no chunks. |
| 84 | read -r src ref <<<"$PUSH_TEST" |
| 85 | [[ "$src" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ && "$ref" =~ ^refs/[A-Za-z0-9_./-]+$ ]] || { echo "::error::push_test takes \"<owner>/<repository> <ref>\""; exit 1; } |
| 86 | owner=$(api GET /user | jq -r .login) |
| 87 | name="campaign-push-$GITHUB_RUN_ID" |
| 88 | api POST /user/repos "$(jq -nc --arg n "$name" '{name: $n, private: true}')" >/dev/null |
| 89 | git init -q --bare one && cd one |
| 90 | git fetch -q "https://github.com/$src" "+$ref:refs/heads/one-push" |
| 91 | echo "Pack for $src $ref: $(git pack-objects --revs --stdout -q <<<refs/heads/one-push | wc -c) bytes, $(git rev-list --objects refs/heads/one-push | wc -l) objects" |
| 92 | auth="Authorization: Basic $(printf 'x-token:%s' "$TOKEN" | base64 -w0)" |
| 93 | started=$(date +%s) |
| 94 | if git -c "http.extraHeader=$auth" -c http.postBuffer=1048576 push "$DIFFINERY/$owner/$name.git" refs/heads/one-push:refs/heads/main 2>&1 | tail -20; then :; fi |
| 95 | echo "Push took $(( $(date +%s) - started )) s" |
| 96 | want=$(git rev-parse refs/heads/one-push) |
| 97 | got=$(git -c "http.extraHeader=$auth" ls-remote "$DIFFINERY/$owner/$name.git" refs/heads/main | cut -f1) |
| 98 | echo "Diffinery has main at ${got:-nothing}; pushed $want" | tee -a "$GITHUB_STEP_SUMMARY" |
| 99 | if [ "$got" != "$want" ]; then |
| 100 | # What the server recorded for a 5xx (redacted; needs a site admin's token). |
| 101 | api GET "/diffinery/admin/errors?per_page=5" | jq -r --arg since "$(date -u -d "@$started" +%FT%TZ)" '.[] | select(.at >= $since) | "\(.at) \(.status) \(.method) \(.route): \(.message)\n\((.stack // "") | split("\n")[0:12] | join("\n"))"' || echo "(no server errors readable)" |
| 102 | # A deploy mid-push resets the repository's Durable Object; show whether one ran. |
| 103 | echo "Recent deploys on Diffinery:" |
| 104 | api GET "/repos/$R/actions/workflows/deploy.yml/runs?per_page=5" | jq -r '.workflow_runs[] | "\(.id) \(.head_branch) \(.head_sha[0:7]) \(.status) \(.conclusion) \(.created_at) \(.updated_at)"' || true |
| 105 | echo "::error::the push did not land" |
| 106 | exit 1 |
| 107 | fi |
| 108 | cd .. && git -c "http.extraHeader=$auth" clone -q --bare "$DIFFINERY/$owner/$name.git" back |
| 109 | git -C back fsck --full --strict && echo "Cloned back and checked: $DIFFINERY/$owner/$name" | tee -a "$GITHUB_STEP_SUMMARY" |
| 110 | exit 0 |
| 111 | fi |
| 112 | if [ -n "$RERUN_FAILED$INSPECT_RUN" ]; then |
| 113 | read -r repo run only <<<"${RERUN_FAILED:-$INSPECT_RUN}" |
| 114 | if [ -n "$INSPECT_RUN" ] && [ -z "$run" ]; then |
| 115 | # No run id: list the repository's latest runs and inspect the newest. |
| 116 | runs=$(api GET "/repos/$repo/actions/runs?per_page=10") |
| 117 | jq -r '.workflow_runs[] | "\(.id) \(.name) \(.created_at) \(.status) \(.conclusion // "")"' <<<"$runs" |
| 118 | run=$(jq -r '.workflow_runs[0].id // ""' <<<"$runs") |
| 119 | fi |
| 120 | [[ "$repo" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ && "$run" =~ ^[A-Za-z0-9_-]+$ ]] || { echo "::error::rerun_failed takes \"<owner>/<repository> <run id>\", inspect_run \"<owner>/<repository> [<run id>]\""; exit 1; } |
| 121 | if [ -n "$RERUN_FAILED" ] && [ -n "$only" ]; then |
| 122 | # One job: the first whose name contains the rest of the input (e.g. "Node.js 16.x"). |
| 123 | job=$(api GET "/repos/$repo/actions/runs/$run/jobs?per_page=100" | jq -r --arg n "$only" '[.jobs[] | select(.name | contains($n))][0].id // ""') |
| 124 | [ -n "$job" ] || { echo "::error::no job of run $run has \"$only\" in its name"; exit 1; } |
| 125 | run=$(api POST "/repos/$repo/actions/jobs/$job/rerun" | jq -r --arg run "$run" '.run_id // $run') |
| 126 | echo "Re-running job $job as $(api GET "/repos/$repo/actions/runs/$run" | jq -r .html_url)" |
| 127 | sleep 30 |
| 128 | while [ "$(api GET "/repos/$repo/actions/runs/$run" | jq -r .status)" != completed ]; do sleep 30; done |
| 129 | elif [ -n "$RERUN_FAILED" ]; then |
| 130 | # Diffinery answers with the run that re-runs it ({run_id}); GitHub re-runs in place. |
| 131 | run=$(api POST "/repos/$repo/actions/runs/$run/rerun-failed-jobs" | jq -r --arg run "$run" '.run_id // $run') |
| 132 | echo "Re-running the failed jobs as $(api GET "/repos/$repo/actions/runs/$run" | jq -r .html_url)" |
| 133 | sleep 30 |
| 134 | while [ "$(api GET "/repos/$repo/actions/runs/$run" | jq -r .status)" != completed ]; do sleep 30; done |
| 135 | else |
| 136 | echo "Jobs of $(api GET "/repos/$repo/actions/runs/$run" | jq -r '"\(.html_url) (\(.status) \(.conclusion // ""), attempt \(.run_attempt // 1))"')" |
| 137 | fi |
| 138 | jobs=$(api GET "/repos/$repo/actions/runs/$run/jobs?per_page=100") |
| 139 | jq -r '.jobs[] | "\(.name): \(.conclusion // .status)\(if .failure then " (\(.failure.message? // .failure))" else "" end)"' <<<"$jobs" | tee -a "$GITHUB_STEP_SUMMARY" |
| 140 | for job in $(jq -r '.jobs[] | select(.conclusion == "failure") | .id' <<<"$jobs"); do |
| 141 | echo "::group::$(jq -r --arg id "$job" '.jobs[] | select((.id | tostring) == $id) | .name' <<<"$jobs")" |
| 142 | # Where it ran and how far it got, for a job that stopped without a log. |
| 143 | jq -r --arg id "$job" '.jobs[] | select((.id | tostring) == $id) | "runner: \(.runner // .runner_name // "none")", "started \(.started_at // "-"), completed \(.completed_at // "-")", (.steps[]? | " step \(.number) \(.name): \(.status) \(.conclusion // "")")' <<<"$jobs" |
| 144 | { api GET "/repos/$repo/actions/jobs/$job/logs" || echo "(no log: the request failed)"; } | sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z //' | tail -80 |
| 145 | echo "::endgroup::" |
| 146 | done |
| 147 | exit 0 |
| 148 | fi |
| 149 | if [ "$PEEK" = true ]; then |
| 150 | for id in $(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=5" | jq -r '.workflow_runs[] | select(.status != "completed") | .id'); do |
| 151 | echo "Campaign run $id on Diffinery:" |
| 152 | api GET "/repos/$R/actions/runs/$id/jobs" | jq -r '.jobs[] | " job \(.name): \(.status) \(.conclusion // "")"' |
| 153 | done |
| 154 | owner=$(api GET /user | jq -r .login) |
| 155 | since=$(date -u -d '-12 hours' +%Y-%m-%dT%H:%M:%S) |
| 156 | imports=$(api GET /diffinery/imports | jq -c --arg since "$since" '[.[] | select((.target.name // "") | startswith("campaign-")) | select(.created_at >= $since)] | sort_by(.created_at)') |
| 157 | jq -r '.[] | "\(.target.name): \(.status) (attempt \(.attempt))\(if .error then ": \(.error)" else "" end)", |
| 158 | ([.steps[] | select(.status == "running" or .status == "failed")][] | " step \(.title): \(.status) \(.message // "") \(.count // "")"), |
| 159 | (if .git then " git \(.git.phase // "") \(.git.message // "") (\(.git.bytes) bytes, \(.git.chunks) chunks, \(.git.tries) tries)" else empty end), |
| 160 | (.warnings[]? | " warning: \(.)")' <<<"$imports" |
| 161 | # Runs on the imported repositories, with each failed job's reason and the end of its log. |
| 162 | for name in $(jq -r '.[] | select(.status == "succeeded") | .target.name' <<<"$imports"); do |
| 163 | runs=$(api GET "/repos/$owner/$name/actions/runs?per_page=30") || continue |
| 164 | jq -r --arg n "$name" '.workflow_runs[] | "\($n) run \(.name): \(.status) \(.conclusion // "")"' <<<"$runs" |
| 165 | for run in $(jq -r '.workflow_runs[] | select(.conclusion == "failure") | .id' <<<"$runs"); do |
| 166 | jobs=$(api GET "/repos/$owner/$name/actions/runs/$run/jobs?per_page=100") || continue |
| 167 | for job in $(jq -r '.jobs[] | select(.conclusion == "failure") | .id' <<<"$jobs"); do |
| 168 | echo "::group::$name: $(jq -r --arg id "$job" '.jobs[] | select((.id | tostring) == $id) | "\(.name)\(if .failure then " (\(.failure.message? // .failure))" else "" end)"' <<<"$jobs")" |
| 169 | { api GET "/repos/$owner/$name/actions/jobs/$job/logs" || true; } | sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z //' | tail -60 |
| 170 | echo "::endgroup::" |
| 171 | done |
| 172 | done |
| 173 | done |
| 174 | exit 0 |
| 175 | fi |
| 176 | if [ -n "$FOLLOW_RUN" ]; then |
| 177 | id=$FOLLOW_RUN |
| 178 | else |
| 179 | # A new campaign replaces one still running on Diffinery (its imports are cancelled by the new one). |
| 180 | for old in $(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=20" | jq -r '.workflow_runs[] | select(.status != "completed") | .id'); do |
| 181 | echo "cancelling the campaign still running on Diffinery: $old" |
| 182 | api POST "/repos/$R/actions/runs/$old/cancel" >/dev/null || true |
| 183 | done |
| 184 | before=$(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=1" | jq -r '.workflow_runs[0].id // 0') |
| 185 | api POST "/repos/$R/actions/workflows/import-campaign.yml/dispatches" "$(jq -nc --arg r "$REPOS" --arg m "$MINUTES" --arg w "$RUN_WORKFLOWS" '{ref: "main", inputs: {repos: $r, minutes: $m, run_workflows: $w}}')" >/dev/null |
| 186 | for i in $(seq 1 60); do |
| 187 | run=$(api GET "/repos/$R/actions/workflows/import-campaign.yml/runs?per_page=1" | jq -c '.workflow_runs[0] // empty') |
| 188 | [ -n "$run" ] && [ "$(jq -r .id <<<"$run")" != "$before" ] && break |
| 189 | sleep 5 |
| 190 | done |
| 191 | id=$(jq -r .id <<<"$run") |
| 192 | fi |
| 193 | echo "Diffinery run: $(api GET "/repos/$R/actions/runs/$id" | jq -r .html_url)" |
| 194 | while [ "$(api GET "/repos/$R/actions/runs/$id" | jq -r .status)" != completed ]; do sleep 30; done |
| 195 | run=$(api GET "/repos/$R/actions/runs/$id") |
| 196 | jobs=$(api GET "/repos/$R/actions/runs/$id/jobs") |
| 197 | for job in $(jq -r '.jobs[].id' <<<"$jobs"); do |
| 198 | jq -r --arg id "$job" '.jobs[] | select((.id | tostring) == $id) | "Job \(.name): \(.conclusion // .status)\(if .failure then " (\(.failure.message? // .failure))" else "" end)"' <<<"$jobs" |
| 199 | log=$(curl -fsS "$DIFFINERY/api/v3/repos/$R/actions/jobs/$job/logs" -H "Authorization: Bearer $TOKEN" || echo "(no log)") |
| 200 | # Diffinery's job logs start each line with a timestamp. |
| 201 | log=$(sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z //' <<<"$log") |
| 202 | if grep -q '^# Import campaign' <<<"$log"; then |
| 203 | sed -n '/^# Import campaign/,$p' <<<"$log" | tee -a "$GITHUB_STEP_SUMMARY" |
| 204 | else |
| 205 | # No report: the job stopped before writing one, so show how it ended. |
| 206 | echo "::group::Last 150 log lines of job $job" |
| 207 | tail -150 <<<"$log" |
| 208 | echo "::endgroup::" |
| 209 | fi |
| 210 | done |
| 211 | [ "$(jq -r .conclusion <<<"$run")" = success ] || { echo "::error::the campaign run on Diffinery ended $(jq -r .conclusion <<<"$run")"; exit 1; } |
| 212 | |
| 213 | campaign: |
| 214 | name: Import, run and report |
| 215 | if: ${{ github.server_url != 'https://github.com' }} |
| 216 | runs-on: ubuntu-latest |
| 217 | timeout-minutes: 360 |
| 218 | # DIFFINERY_LIVE_TOKEN is the production environment's. |
| 219 | environment: production |
| 220 | env: |
| 221 | DIFFINERY: ${{ github.server_url }} |
| 222 | TOKEN: ${{ secrets.DIFFINERY_LIVE_TOKEN }} |
| 223 | steps: |
| 224 | - uses: actions/checkout@v4 |
| 225 | - name: Import and run |
| 226 | env: |
| 227 | REPOS: ${{ inputs.repos }} |
| 228 | MINUTES: ${{ inputs.minutes }} |
| 229 | RUN_WORKFLOWS: ${{ inputs.run_workflows }} |
| 230 | run: | |
| 231 | echo "::notice::Mode: token-free, so imports use GitHub's anonymous rate limit" |
| 232 | # shellcheck disable=SC2086 |
| 233 | REPORT=report.md scripts/import-campaign.sh $REPOS |
| 234 | cat report.md >> "$GITHUB_STEP_SUMMARY" |
| 235 | - uses: actions/upload-artifact@v4 |
| 236 | if: always() |
| 237 | with: |
| 238 | name: import-campaign-report |
| 239 | path: report.md |