diff --git a/.github/workflows/pins.yml b/.github/workflows/pins.yml index ee04579f..efb8e3bb 100644 --- a/.github/workflows/pins.yml +++ b/.github/workflows/pins.yml @@ -32,7 +32,9 @@ jobs: install: false cache: false - name: Check the pins - # gh api reads the actions' releases, and mise GitHub's for the dev tools. + # The token is for gh api, which reads the actions' releases, mise, + # which reads GitHub's for the dev tools, and bump.sh's own calls to + # GitHub's API. env: GH_TOKEN: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }} diff --git a/docs/content/docs/contributing/maintaining.md b/docs/content/docs/contributing/maintaining.md index 3a725b4a..bdacda37 100644 --- a/docs/content/docs/contributing/maintaining.md +++ b/docs/content/docs/contributing/maintaining.md @@ -20,7 +20,8 @@ scripts/bump.sh docker pele # only these tools, with a named images or e2e t - With no tools named, `bump.sh` applies every patch and minor bump. It never applies a major, a held pin or a tool that it applies only when named. Naming an images or e2e tool also applies its major. `e2e-tools` always applies one, because users get the shared tools' new majors as well, and `assets/shared-tools/mise.toml` holds what stays. - It writes a value only if it has the shape of a pin (a version, a 64-hex checksum, a dated release, a commit, an image digest), since some of the edited files run on the host, and it edits nothing unless every value is good. Then it runs the commands that finish a bump: `mise lock` for Go and the dev tools, `go get` and `go mod tidy` for the modules, `go get` and `hugo mod tidy` in `docs/` for Hextra, `npm install --ignore-scripts` in `docs/` and then `mise run docs:diagrams` for pele, and actionlint and zizmor for the workflows. If one of those fails, the pins are already edited, and `bump.sh` says so. - It needs `curl` and `jq`, `mise` for Go, the dev tools, the end-to-end lock and pele, `go` to apply the modules and Hextra, and `gh` for the actions. -- Every Monday the Pins workflow (`.github/workflows/pins.yml`) runs `scripts/bump.sh --check` and opens or updates one issue, "Pinned versions to bump", which lists the outdated pins under their groups, and closes it once none is left. It never bumps anything, so you run `scripts/bump.sh`, then the `--from` tier of the [end-to-end tests](tests.md#end-to-end-tests) when an images or e2e pin moved, because those bumps change images. +- `bump.sh` sends `GH_TOKEN`, or else `GITHUB_TOKEN`, when one is set, with its calls to GitHub's API and to nothing else, through a header file rather than curl's command line. Without a token, GitHub allows 60 calls an hour from one address, which a CI runner shares with other jobs, and once they're spent it refuses the rest with a `403`. A fetch that fails names its URL. +- Every Monday the Pins workflow (`.github/workflows/pins.yml`) runs `scripts/bump.sh --check` with the workflow's token and opens or updates one issue, "Pinned versions to bump", which lists the outdated pins under their groups, and closes it once none is left. It never bumps anything, so you run `scripts/bump.sh`, then the `--from` tier of the [end-to-end tests](tests.md#end-to-end-tests) when an images or e2e pin moved, because those bumps change images. The tools, by group, as `--check` prints them: diff --git a/scripts/bump.sh b/scripts/bump.sh index 97302893..3b325ace 100755 --- a/scripts/bump.sh +++ b/scripts/bump.sh @@ -132,9 +132,20 @@ summary=() notes=() tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT -fetch() { curl -fsSL --retry 3 "$@"; } -gh_latest() { fetch "https://api.github.com/repos/$1/releases/latest" | jq -r .tag_name; } -gh_head() { fetch "https://api.github.com/repos/$1/commits/${2:-master}" | jq -r .sha; } +# fetch [CURL_ARG…] URL: the URL's body, or a failure that names it. +fetch() { curl -fsSL --retry 3 "$@" || { local rc=$?; echo "bump: couldn't fetch ${!#}" >&2; return "$rc"; }; } +# gh_api PATH: GitHub's API, with a token when there is one, GH_TOKEN as gh reads it or else +# GITHUB_TOKEN, as the Pins workflow sets: without one, a runner shares its address's 60 requests an +# hour with every other job on it, and GitHub answers 403 once they're spent. The token goes to curl +# in a header file, never on its command line, and only to api.github.com. +gh_auth=() +if [[ -n ${GH_TOKEN:-${GITHUB_TOKEN:-}} ]]; then + (umask 077; printf 'Authorization: Bearer %s\n' "${GH_TOKEN:-$GITHUB_TOKEN}" > "$tmp/gh-auth") + gh_auth=(-H @"$tmp/gh-auth") +fi +gh_api() { fetch ${gh_auth[@]+"${gh_auth[@]}"} "https://api.github.com/$1"; } +gh_latest() { gh_api "repos/$1/releases/latest" | jq -r .tag_name; } +gh_head() { gh_api "repos/$1/commits/${2:-master}" | jq -r .sha; } # sha256 FILE: its SHA-256, with sha256sum (coreutils) or shasum (macOS's; Perl's, which Fedora # lacks), whichever there is. sha256() { if command -v sha256sum >/dev/null; then sha256sum "$1"; else shasum -a 256 "$1"; fi | cut -c1-64; } @@ -317,7 +328,7 @@ bump_e2e-ubuntu() { # SHA256SUMS's own SHA-256, so the file is checked against them before a line of it is used. bump_e2e-lima() { local f=.github/workflows/e2e.yml new want got - fetch https://api.github.com/repos/lima-vm/lima/releases/latest > "$tmp/lima-release" + gh_api repos/lima-vm/lima/releases/latest > "$tmp/lima-release" new=$(jq -r .tag_name "$tmp/lima-release"); new=${new#v} report e2e-lima "$(get_env $f LIMA_VERSION)" "$new" || return 0 valid lima_version "$new" diff --git a/testdata/script/bump.txtar b/testdata/script/bump.txtar index 11e913f9..0008b83d 100644 --- a/testdata/script/bump.txtar +++ b/testdata/script/bump.txtar @@ -136,6 +136,37 @@ stdout '^ crane +0\.22\.1 +newer than upstream''s latest, 0\.1\.0$' ! stdout ' -> ' cmp repo/assets/oci/build.sh before +# GitHub's API gets a token when there is one, GH_TOKEN before +# GITHUB_TOKEN, from a header file and never on curl's command line, and +# nothing but GitHub's API gets it; without one, no call has a header. +cp latest-good up/crane-latest +rm curl.calls +rm curl.headers +env GH_TOKEN= +env GITHUB_TOKEN=gh-actions-token +exec repo/scripts/bump.sh --check crane e2e-lima +grep -count=1 '^https://api\.github\.com/repos/google/go-containerregistry/releases/latest Authorization: Bearer gh-actions-token$' curl.headers +grep -count=1 '^https://api\.github\.com/repos/lima-vm/lima/releases/latest Authorization: Bearer gh-actions-token$' curl.headers +! exec grep -v '^https://api\.github\.com/' curl.headers +! grep 'gh-actions-token' curl.calls +rm curl.headers +env GH_TOKEN=gh-cli-token +exec repo/scripts/bump.sh --check crane +grep -count=1 '^https://api\.github\.com/repos/google/go-containerregistry/releases/latest Authorization: Bearer gh-cli-token$' curl.headers +! exec grep -v '^https://api\.github\.com/' curl.headers +! grep 'gh-cli-token' curl.calls +rm curl.headers +env GH_TOKEN= +env GITHUB_TOKEN= +exec repo/scripts/bump.sh --check crane +! exists curl.headers + +# A failed fetch names its URL. +rm up/crane-latest +! exec repo/scripts/bump.sh --check crane +stderr '^bump: couldn''t fetch https://api\.github\.com/repos/google/go-containerregistry/releases/latest$' +cp latest-good up/crane-latest + # An unknown tool: refused. ! exec repo/scripts/bump.sh nope stderr '^bump: unknown tool ''nope''' @@ -291,8 +322,10 @@ cp release-index up/release-index # Answers crane's, mise's and Lima's releases, Ubuntu's list of cloud # image releases and Docker Hub's index of the release container, by its # tag, from $WORK/up/, to stdout or -o's file; anything else fails. -# Every call goes in $WORK/curl.calls. +# Every call goes in $WORK/curl.calls, and each header file it reads +# (-H @FILE) in $WORK/curl.headers, after its URL. echo "curl $*" >> "$WORK/curl.calls" +for a in "$@"; do [[ $a == @* ]] && printf '%s %s\n' "${!#}" "$(cat "${a#@}")" >> "$WORK/curl.headers"; done out=/dev/stdout for a in "$@"; do [[ ${o:-} == next ]] && { out=$a; o=; }; [[ $a == -o ]] && o=next; done # The module proxy's @latest, as -w's status with -o's body: up/proxy/'s