diff --git a/.github/workflows/git-ai-notes.yaml b/.github/workflows/git-ai-notes.yaml index b2f0112..77c8361 100644 --- a/.github/workflows/git-ai-notes.yaml +++ b/.github/workflows/git-ai-notes.yaml @@ -18,6 +18,24 @@ name: Git AI notes # git-ai: # uses: gooddata/github-actions-public/.github/workflows/git-ai-notes.yaml@ # +# A REPOSITORY WITH AN S3 GIT CACHE CAN RESTORE IT INSTEAD OF CLONING. +# `runner` takes a JSON `runs-on` value and defaults to "ubuntu-latest". +# `git-cache: true` restores the `.git` that the repository's own cache job +# saves under `git//-full` in the S3 bucket `gdc-github-cache`, +# then fetches only what changed since. On the self-hosted runners, which +# run in AWS, every byte fetched from GitHub is cross-cloud transfer that +# counts toward CI cost, while the restore reads S3 inside AWS. The restore +# takes a few seconds longer than a treeless checkout and is chosen for that +# cost. The bucket is reachable from those runner groups, so a caller sets +# both: +# +# with: +# runner: '{"group": "infra1-runners-arc", "labels": "runners-small"}' +# git-cache: true +# +# A cache miss or an unreachable bucket only warns, and the job fetches the +# full history as it does without the cache. +# # FORK PULL REQUESTS ARE NOT SUPPORTED, BY DESIGN. # `pull_request` runs originating from a fork get a read-only GITHUB_TOKEN, so # the push to refs/notes/ai cannot succeed. Those runs are skipped explicitly @@ -52,19 +70,25 @@ name: Git AI notes # repository itself, with the token written into the clone URL. Both paths # here run `git-ai ci local` instead (`merge` on a merge, `sync` on a push to # an open PR), which works in the checkout and takes its inputs as flags, so -# the token stays in the checkout action's credential helper. The checkout is -# of the base branch, with full history: on a closed PR the default ref is the -# PR's merge ref, which is gone once the branch is deleted, and git-ai walks -# history to find merge bases. -# The checkout is blobless (`filter: blob:none`): git-ai needs the commit -# graph, and fetches the few file contents it diffs from the promisor remote -# on demand. A full clone of a large repository costs minutes per run. +# the token stays in the checkout action's credential handling. The checkout +# is of the base branch, with every branch's history: git-ai walks it to find +# merge bases, and the PR's own commits come from refs/pull//head, which +# outlives a deleted branch. +# The checkout is treeless (`filter: tree:0`) and sparse (`.github` plus the root-level files): +# git-ai reads the commit graph and the notes ref, never files on disk, and +# fetches the few trees and file contents it diffs from the promisor remote +# on demand. A full checkout of the base branch fetches every tree and blob +# at its tip to write the tree, which costs most of a monorepo run. # -# A MERGE COMMIT IS SKIPPED BEFORE THE CHECKOUT. +# RUNS WITH NOTHING TO CARRY ARE SKIPPED BEFORE THE CHECKOUT. # A merge commit with two parents keeps the PR's commits and their notes, and -# git-ai does nothing for it. The job reads the parent count from the API and -# ends there, so repositories that allow merge commits only pay seconds, not -# a checkout, on every merge. +# git-ai does nothing for it. A push to an open PR that only adds commits +# (the compare API reports the new head `ahead` of or `identical` to the old one) rewrites +# nothing, and git-ai skips it as a fast-forward. A push event without a +# previous head has nothing to carry from. The job decides these from the +# API and ends there, so they cost seconds, not a checkout. A failed lookup +# falls through to the full run, which decides the same thing from the +# checkout. # # Supply-chain hardening vs. the stock `git-ai ci github install` output: # * Downloads the release binary from GitHub, NOT usegitai.com, which removes @@ -87,18 +111,29 @@ name: Git AI notes # briefly for its sockets, which a fresh runner can miss. The job starts the # service first and retries the run, so a slow start does not drop a merge. # -# Bump GIT_AI_VERSION and GIT_AI_SHA256 together, here, then repin every caller -# to the new commit SHA. Get the checksum of git-ai-linux-x64 from that -# release's SHA256SUMS: +# Bump GIT_AI_VERSION and both checksums together, here, then repin every caller +# to the new commit SHA. Get the checksums of git-ai-linux-x64 and +# git-ai-linux-arm64 from that release's SHA256SUMS: # curl -sSL https://github.com/git-ai-project/git-ai/releases/download//SHA256SUMS on: workflow_call: + inputs: + runner: + description: 'JSON `runs-on` value for the job.' + type: string + default: '"ubuntu-latest"' + git-cache: + description: 'Restore the repository''s S3 git cache before the checkout. Needs a runner that can reach the bucket.' + type: boolean + default: false env: GIT_AI_VERSION: v1.7.6 - # sha256 of git-ai-linux-x64 for GIT_AI_VERSION, from that release's SHA256SUMS + # sha256 of git-ai-linux-x64 and git-ai-linux-arm64 for GIT_AI_VERSION, from + # that release's SHA256SUMS. A self-hosted `runner` may be either. GIT_AI_SHA256: f6cda0cb43c1c9a3d68e7efe2d79577680ea6f382c31bf36a2162906f93f6c1c + GIT_AI_SHA256_ARM64: 1326abd5547636e6b6b297ad79afc955e57e82361efac320d4f860147b95a87a jobs: git-ai: @@ -117,42 +152,75 @@ jobs: && github.actor != 'dependabot[bot]' && (github.event.pull_request.merged == true || github.event.action == 'synchronize') - runs-on: ubuntu-latest + runs-on: ${{ fromJSON(inputs.runner) }} permissions: contents: write steps: - - name: Skip a merge commit + - name: Skip a run with nothing to carry id: kind - if: github.event.pull_request.merged == true env: GH_TOKEN: ${{ github.token }} + MERGED: ${{ github.event.pull_request.merged }} MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }} + BEFORE: ${{ github.event.before }} + AFTER: ${{ github.event.after }} run: | set -euo pipefail # A failed lookup falls through to the full run, which decides the same # thing from the checkout. - parents=$(gh api "repos/${GITHUB_REPOSITORY}/commits/${MERGE_SHA}" --jq '.parents | length') || parents=0 - if [ "${parents:-0}" -gt 1 ]; then - echo "merge ${MERGE_SHA} has ${parents} parents; the PR's commits keep their notes" - echo "skip=true" >> "$GITHUB_OUTPUT" + if [ "$MERGED" = true ]; then + parents=$(gh api "repos/${GITHUB_REPOSITORY}/commits/${MERGE_SHA}" --jq '.parents | length') || parents=0 + if [ "${parents:-0}" -gt 1 ]; then + echo "merge ${MERGE_SHA} has ${parents} parents; the PR's commits keep their notes" + echo "skip=true" >> "$GITHUB_OUTPUT" + fi + exit 0 fi + case "$BEFORE" in ""|0000000000000000000000000000000000000000) + echo "no previous head in the event; nothing to carry over" + echo "skip=true" >> "$GITHUB_OUTPUT" + exit 0 ;; + esac + status=$(gh api "repos/${GITHUB_REPOSITORY}/compare/${BEFORE}...${AFTER}" --jq '.status') || status=unknown + case "$status" in ahead|identical) + echo "${AFTER} is ${status} of ${BEFORE}; a fast-forward push has no notes to carry" + echo "skip=true" >> "$GITHUB_OUTPUT" ;; + esac - name: Install git-ai (pinned + checksum-verified) if: steps.kind.outputs.skip != 'true' run: | set -euo pipefail + case "$(uname -m)" in + x86_64) asset=git-ai-linux-x64; sum="$GIT_AI_SHA256" ;; + aarch64|arm64) asset=git-ai-linux-arm64; sum="$GIT_AI_SHA256_ARM64" ;; + *) echo "no pinned git-ai binary for $(uname -m)"; exit 1 ;; + esac bin="$(mktemp)" curl --fail --location --silent --show-error \ -o "$bin" \ - "https://github.com/git-ai-project/git-ai/releases/download/${GIT_AI_VERSION}/git-ai-linux-x64" - echo "${GIT_AI_SHA256} ${bin}" | sha256sum -c - + "https://github.com/git-ai-project/git-ai/releases/download/${GIT_AI_VERSION}/${asset}" + echo "${sum} ${bin}" | sha256sum -c - install -D -m 0755 "$bin" "$HOME/.git-ai/bin/git-ai" echo "$HOME/.git-ai/bin" >> "$GITHUB_PATH" + - name: Restore the S3 git cache + if: steps.kind.outputs.skip != 'true' && inputs.git-cache + uses: runs-on/cache/restore@88d90644011a3a9957fd141a106f5a94f9794203 # v5.0.7 + with: + path: ./.git + key: git/${{ github.repository }}-full + env: + RUNS_ON_S3_BUCKET_CACHE: gdc-github-cache + RUNS_ON_AWS_REGION: us-east-1 + # `clean: false` keeps checkout from resetting a restored `.git` into a + # full working tree before the sparse pattern applies. - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 if: steps.kind.outputs.skip != 'true' with: ref: ${{ github.event.pull_request.base.ref }} fetch-depth: 0 - filter: blob:none + filter: tree:0 + clean: false + sparse-checkout: .github - name: Run git-ai if: steps.kind.outputs.skip != 'true' env: @@ -173,11 +241,8 @@ jobs: # The branch may be gone after a merge; GitHub keeps its commits here. git fetch --quiet origin "+refs/pull/${PR_NUMBER}/head:refs/github/pr/${PR_NUMBER}" if [ "$MERGED" != true ]; then - # A push to an open PR. Without the head it replaced there is - # nothing to carry notes from, as in `git-ai ci github run`. - case "$BEFORE" in ""|0000000000000000000000000000000000000000) - echo "no previous head in the event; nothing to carry over"; exit 0 ;; - esac + # A push to an open PR that rewrote history; the kind step ended the + # run for a fast-forward or a missing previous head. for attempt in 1 2 3; do if git-ai ci local sync --previous-head-sha "$BEFORE" --head-sha "${AFTER:-$HEAD_SHA}" \ --base-ref "$BASE_REF" --base-sha "$BASE_SHA" --remote origin; then