From 5b714773714c440ea3003d4c056c6d052611cdb9 Mon Sep 17 00:00:00 2001 From: Asmir Avdicevic Date: Thu, 26 Nov 2026 08:51:25 +0100 Subject: [PATCH 1/3] minimal-crates: accept a token for a private git dependency Callers that patch in a private GitHub repository cannot run this workflow: it takes no secrets, and the caller's env does not reach a called workflow, so cargo fetches the repository without credentials. Add an optional GIT_TOKEN secret and a git-token-repo input. With both set, git rewrites https://github.com/ to carry the token and cargo fetches with the git CLI. Without them, nothing changes. Document the pair in the README as the convention for every shared workflow that runs cargo. --- .github/workflows/minimal-crates.yaml | 20 ++++++++++++++++++++ README.md | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/.github/workflows/minimal-crates.yaml b/.github/workflows/minimal-crates.yaml index b905bd9..f67adce 100644 --- a/.github/workflows/minimal-crates.yaml +++ b/.github/workflows/minimal-crates.yaml @@ -19,6 +19,19 @@ on: crypto backend of rustls. type: "boolean" default: false + git-token-repo: + description: | + The private GitHub repository, as `owner/name`, that a git + dependency comes from. Cargo fetches it with `GIT_TOKEN`. + type: "string" + default: "" + secrets: + GIT_TOKEN: + description: | + Token that can read `git-token-repo`, for example a fine-grained + token with read-only Contents access. It is used only when + `git-token-repo` is set. + required: false workflow_dispatch: concurrency: @@ -26,6 +39,13 @@ concurrency: cancel-in-progress: true env: + # With GIT_TOKEN and git-token-repo, git rewrites + # https://github.com/ to carry the token. Cargo uses the + # git CLI so that the rewrite applies. Other URLs stay unchanged. + CARGO_NET_GIT_FETCH_WITH_CLI: ${{ secrets.GIT_TOKEN != '' && inputs.git-token-repo != '' }} + GIT_CONFIG_COUNT: ${{ secrets.GIT_TOKEN != '' && inputs.git-token-repo != '' && '1' || '0' }} + GIT_CONFIG_KEY_0: ${{ format('url.https://x-access-token:{0}@github.com/{1}.insteadOf', secrets.GIT_TOKEN, inputs.git-token-repo) }} + GIT_CONFIG_VALUE_0: ${{ format('https://github.com/{0}', inputs.git-token-repo) }} RUST_BACKTRACE: 1 RUSTC_WRAPPER: "sccache" SCCACHE_GHA_ENABLED: "true" diff --git a/README.md b/README.md index 269fb6a..c477d86 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,33 @@ jobs: uses: "n0-computer/workflows/.github/workflows/minimal-crates.yaml@main" ``` +### Private git dependencies + +A caller can depend on a private GitHub repository, for example +through `[patch.crates-io]`. To fetch it, pass a token that can read +that repository and name the repository: + +```yaml +jobs: + min-crates: + name: Min Crates + uses: "n0-computer/workflows/.github/workflows/minimal-crates.yaml@v2" + secrets: + GIT_TOKEN: ${{ secrets.MY_PRIVATE_REPO_TOKEN }} + with: + git-token-repo: "n0-computer/my-private-repo" +``` + +A fine-grained token with read-only Contents access to that repository +is enough. Git uses the token only for URLs that start with +`https://github.com/`. Without both values, nothing +changes. + +Every shared workflow that runs cargo accepts the optional +`GIT_TOKEN` secret and the `git-token-repo` input. A new shared +workflow must accept them too: copy the `secrets`, `inputs` and `env` +entries from `minimal-crates.yaml`. + ### Editing a shared workflow - Start editing a shared workflow in a branch, e.g. `username/branch_name`. From e6206d30c905a9063e5db13b617e21355f8a5b02 Mon Sep 17 00:00:00 2001 From: Asmir Avdicevic Date: Thu, 26 Nov 2026 09:02:25 +0100 Subject: [PATCH 2/3] minimal-crates: use GIT_TOKEN for all of github.com Drop the git-token-repo input. With GIT_TOKEN, every job rewrites https://github.com/ to carry the token. Public fetches and checkout's own auth header keep working with the token in the URL. --- .github/workflows/minimal-crates.yaml | 22 ++++++---------------- README.md | 24 ++++++------------------ 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/.github/workflows/minimal-crates.yaml b/.github/workflows/minimal-crates.yaml index f67adce..636431a 100644 --- a/.github/workflows/minimal-crates.yaml +++ b/.github/workflows/minimal-crates.yaml @@ -19,18 +19,10 @@ on: crypto backend of rustls. type: "boolean" default: false - git-token-repo: - description: | - The private GitHub repository, as `owner/name`, that a git - dependency comes from. Cargo fetches it with `GIT_TOKEN`. - type: "string" - default: "" secrets: GIT_TOKEN: description: | - Token that can read `git-token-repo`, for example a fine-grained - token with read-only Contents access. It is used only when - `git-token-repo` is set. + Token for private git dependencies on github.com. required: false workflow_dispatch: @@ -39,13 +31,11 @@ concurrency: cancel-in-progress: true env: - # With GIT_TOKEN and git-token-repo, git rewrites - # https://github.com/ to carry the token. Cargo uses the - # git CLI so that the rewrite applies. Other URLs stay unchanged. - CARGO_NET_GIT_FETCH_WITH_CLI: ${{ secrets.GIT_TOKEN != '' && inputs.git-token-repo != '' }} - GIT_CONFIG_COUNT: ${{ secrets.GIT_TOKEN != '' && inputs.git-token-repo != '' && '1' || '0' }} - GIT_CONFIG_KEY_0: ${{ format('url.https://x-access-token:{0}@github.com/{1}.insteadOf', secrets.GIT_TOKEN, inputs.git-token-repo) }} - GIT_CONFIG_VALUE_0: ${{ format('https://github.com/{0}', inputs.git-token-repo) }} + # Every job fetches github.com git dependencies with GIT_TOKEN, if given. + CARGO_NET_GIT_FETCH_WITH_CLI: "true" + GIT_CONFIG_COUNT: ${{ secrets.GIT_TOKEN != '' && '1' || '0' }} + GIT_CONFIG_KEY_0: url.https://x-access-token:${{ secrets.GIT_TOKEN }}@github.com/.insteadOf + GIT_CONFIG_VALUE_0: https://github.com/ RUST_BACKTRACE: 1 RUSTC_WRAPPER: "sccache" SCCACHE_GHA_ENABLED: "true" diff --git a/README.md b/README.md index c477d86..60d2d68 100644 --- a/README.md +++ b/README.md @@ -15,30 +15,18 @@ jobs: ### Private git dependencies -A caller can depend on a private GitHub repository, for example -through `[patch.crates-io]`. To fetch it, pass a token that can read -that repository and name the repository: +Every shared workflow takes an optional `GIT_TOKEN` secret. All its +jobs fetch git dependencies from github.com with it, so a caller can +depend on a private repository: ```yaml -jobs: - min-crates: - name: Min Crates - uses: "n0-computer/workflows/.github/workflows/minimal-crates.yaml@v2" secrets: GIT_TOKEN: ${{ secrets.MY_PRIVATE_REPO_TOKEN }} - with: - git-token-repo: "n0-computer/my-private-repo" ``` -A fine-grained token with read-only Contents access to that repository -is enough. Git uses the token only for URLs that start with -`https://github.com/`. Without both values, nothing -changes. - -Every shared workflow that runs cargo accepts the optional -`GIT_TOKEN` secret and the `git-token-repo` input. A new shared -workflow must accept them too: copy the `secrets`, `inputs` and `env` -entries from `minimal-crates.yaml`. +A fine-grained token with read-only Contents access to the private +repository is enough. A new shared workflow must take the secret too: +copy its `secrets` and `env` entries from `minimal-crates.yaml`. ### Editing a shared workflow From 507db95808e03a16eb80b06542dd5260744566d3 Mon Sep 17 00:00:00 2001 From: Asmir Avdicevic Date: Thu, 26 Nov 2026 09:35:00 +0100 Subject: [PATCH 3/3] minimal-crates: stop git from storing GIT_TOKEN With the token in the URL, git hands it to the credential helpers after each successful fetch. On macOS, Homebrew git's osxkeychain helper then blocks during actions/checkout, which runs with a temporary HOME, and the job hangs. Reset credential.helper to empty. --- .github/workflows/minimal-crates.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/minimal-crates.yaml b/.github/workflows/minimal-crates.yaml index 636431a..96f75ce 100644 --- a/.github/workflows/minimal-crates.yaml +++ b/.github/workflows/minimal-crates.yaml @@ -32,10 +32,14 @@ concurrency: env: # Every job fetches github.com git dependencies with GIT_TOKEN, if given. + # The empty credential.helper stops git from storing the token after a + # fetch: macOS osxkeychain blocks during actions/checkout. CARGO_NET_GIT_FETCH_WITH_CLI: "true" - GIT_CONFIG_COUNT: ${{ secrets.GIT_TOKEN != '' && '1' || '0' }} + GIT_CONFIG_COUNT: ${{ secrets.GIT_TOKEN != '' && '2' || '0' }} GIT_CONFIG_KEY_0: url.https://x-access-token:${{ secrets.GIT_TOKEN }}@github.com/.insteadOf GIT_CONFIG_VALUE_0: https://github.com/ + GIT_CONFIG_KEY_1: credential.helper + GIT_CONFIG_VALUE_1: "" RUST_BACKTRACE: 1 RUSTC_WRAPPER: "sccache" SCCACHE_GHA_ENABLED: "true"