uv: uv add private GitHub repository with PAT on Actions fails

Summary

I’m Trying to setup a CI Workflow on GitHub Actions for a private repository that must pull another private repository. Using a Github PAT, I can make it work in pip but it fails with uv.

My CI.yml file is the following

...
jobs:
   ...
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }} on ${{ matrix.os }}
        uses: astral-sh/setup-uv@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Pip install packages
        run: |
          # using pip here would work, but uv fails
          uv --verbose add "git+https://x-access-token:${TOKEN}@github.com/organization/private_repo"
          uv sync
        env:
          TOKEN: ${{ secrets.GH_READONLY_PAT }}

where secrets.GH_READONLY_PAT is a PAT with read only access to private repos, which I tested and works correctly.

Running the action gives the following error

DEBUG No netrc file found
DEBUG GitHub API request failed for: https://api.github.com/repos/NeuralQXLab/netket_pro/commits/HEAD (404 Not Found)
DEBUG Fetching source distribution from Git: https://github.com/NeuralQXLab/netket_pro
DEBUG Acquired lock for `[https://github.com/neuralqxlab/netket_pro`](https://github.com/neuralqxlab/netket_pro%60)
DEBUG Updating Git source `[https://github.com/NeuralQXLab/netket_pro`](https://github.com/NeuralQXLab/netket_pro%60)
   Updating https://github.com/NeuralQXLab/netket_pro (HEAD)
DEBUG Attempting GitHub fast path for: https://api.github.com/repos/NeuralQXLab/netket_pro/commits/HEAD
DEBUG Failed to check GitHub HTTP status client error (404 Not Found) for url (https://api.github.com/repos/NeuralQXLab/netket_pro/commits/HEAD)
DEBUG Performing a Git fetch for: https://github.com/NeuralQXLab/netket_pro
DEBUG Released lock at `/home/runner/actions-runner/_work/_temp/setup-uv-cache/git-v0/locks/d25fca3599406dfa`
DEBUG Reverting changes to `pyproject.toml`
DEBUG Removing `uv.lock`
  × Failed to download and build `netket-pro @
  │ git+[https://github.com/NeuralQXLab/netket_pro`](https://github.com/NeuralQXLab/netket_pro%60)
  ├─▶ Git operation failed
  ├─▶ failed to clone into:
  │   /home/runner/actions-runner/_work/_temp/setup-uv-cache/git-v0/db/d25fca3599406dfa
  ╰─▶ process didn't exit successfully: `/usr/bin/git fetch --force
      --update-head-ok 'https://github.com/NeuralQXLab/netket_pro'
      '+HEAD:refs/remotes/origin/HEAD'` (exit status: 128)
      --- stderr
      fatal: could not read Username for 'https://github.com/': No such device
      or address

I tried the following things:

  • Execute the command above on my machine, with the same token, it works.
  • Create a .netrc file in a preceding step with the token inside, but it is not used/does not solve the problem.

Most interestingly, by ssh-ing in the runner if the step fails, and running exactly the same command, the command success, so I have the feeling that if the session is interactive something different is triggered

Platform

linux GitHub runner

Version

0.5.29

Python version

3.12

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Weird, I don’t understand what’s going on here. We can look into this.

Looking through this and related issues, I’m aware of three generic ways to get a Personal Access Token (PAT) to git invocations that happen in the guts of tools like uv. The first way is ~/.netrc. As folks have mentioned here, git doesn’t know about the NETRC env var, and this approach interacts kind of poorly with Docker. Also no one’s thrilled about writing secrets to disk.

The second way is using the gh command line utility, as mentioned here. gh is installed by default in GitHub runners. It works by configuring itself as a “credential helper” in ~/.gitconfig, and if it can’t find a system keyring it also stores your token under ~/.config/gh or similar. This way is the most convenient, and I recommend it if you’re not sure what to do, but it only works with GitHub specifically. Here’s a working example. The relevant lines of that .github/workflows/ci.yml are:

    - run: echo "${{ secrets.FOO_GITHUB_TOKEN }}" | gh auth login --with-token
    - run: gh auth setup-git
    - run: uv run main.py

The third way is to wrap the token in a trivial “credential helper” (echo) configured entirely via environment variables. This approach has the most annoying syntax, but it could be the most reliable approach if multiple layers of your stack that don’t necessarily know about each other need to configure Git. It also keeps your token off the disk, and it might be easier to get it working with container barriers like Docker (i.e. using --env). Here’s a working example. Here are the relevant lines:

    - run: |
        export "GIT_CONFIG_KEY_0=credential.https://github.com.helper"
        export "GIT_CONFIG_VALUE_0=!echo username=git ; echo password=${{ secrets.FOO_GITHUB_TOKEN }} ; #"
        export "GIT_CONFIG_COUNT=1"
        uv run main.py

In a truly generic, “who knows what other layers above and below me are doing” scenario, you’d read GIT_CONFIG_COUNT from your own environment, name the other two variables accordingly (export lets you set variables with other variables in their names), and then increment the count. There’s some very sensitive punctuation there; for a longer explanation see the giant comment in that file.


Also in case anyone finds this thread who’s working PATs for the first time, for completeness, in all of these approaches you have to start by manually adding the PAT to your CI environment using GitHub’s “Secrets and Variables -> Actions” repo settings:

Image

In this thread, we have multiple use cases:

  1. “classic” install and build in a github action
  2. “docker build” in a github action

(in our context, we do both: “classic” to run tests and “docker build” to push the deployment artifacts on our private docker registry)

classic install and build:

using https://github.com/extractions/netrc

      - name: Set up .netrc
        uses: extractions/netrc@v2
        with:
          machine: github.com
          username: api
          password: ${{ secrets.GH_REPO_PAT }}

      - name: Install dependencies for testing
        run: |
          python -m pip install --upgrade pip uv
          uv sync --all-extras --dev

      - name: Run tests
        run: |
          uv run pytest

This works. GH_REPO_PAT is a fine-grained personal access token, with Read access to code and metadata permission to the private repo of the dependency.

(to be continued)

We use the following in our Dockerfile, in combination with netrc and Docker action:

RUN --mount=type=secret,id=GIT_AUTH_TOKEN \
    --mount=type=secret,id=GIT_ACTOR \
    GIT_AUTH_TOKEN=$(cat /run/secrets/GIT_AUTH_TOKEN) && \
    GIT_ACTOR=$(cat /run/secrets/GIT_ACTOR) && \
    echo "machine github.com login ${GIT_ACTOR} password ${GIT_AUTH_TOKEN}" > ~/.netrc && \
    chmod 600 ~/.netrc

RUN uv sync
RUN rm ~/.netrc

Action:

- name: Build and push Docker image
  id: push
  uses: docker/build-push-action@v6
  with:
    context: .
    push: true
    tags: ${{ steps.meta.outputs.tags }}
    labels: ${{ steps.meta.outputs.labels }}
    secrets: |
      GIT_AUTH_TOKEN=${{ secrets.pat }}
      GIT_ACTOR=${{ secrets.username }}

For us, this works if the (classical) PAT has “Full control of private repositories” selected. In other cases, it complained about not having write access or the repository not existing. Fine grained tokens did not work.

Follow up:

This error message:

process didn't exit successfully: `/usr/bin/git fetch --force --update-head-ok 'https://gitlab.example.com/nnis/nnis-client-python' '+HEAD:refs/remotes/origin/HEAD'` (exit status: 128)

I tried to find out what’s going on here:

❯ uv venv
Using CPython 3.13.3 interpreter at: /usr/bin/python3
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate.fish

❯ source .venv/bin/activate.fish

❯ /usr/bin/git fetch --force --update-head-ok 'https://gitlab.example.com/nnis/nnis-client-python' '+HEAD:refs/remotes/origin/HEAD'
(english: no git repository available) Schwerwiegend: Kein Git-Repository (oder irgendein Elternverzeichnis bis zum Einhängepunkt /)
Stoppe bei Dateisystemgrenze (GIT_DISCOVERY_ACROSS_FILESYSTEM nicht gesetzt).

❯ git init

❯ /usr/bin/git fetch --force --update-head-ok 'https://gitlab.example.com/nnis/nnis-client-python' '+HEAD:refs/remotes/origin/HEAD'
Username for 'https://gitlab.example.com': mbeckert
Password for 'https://mbeckert@gitlab.example.com': 
Warnung: Umleitung nach https://gitlab.example.com/nnis/nnis-client-python.git/
remote: Enumerating objects: 2107, done.
remote: Counting objects: 100% (525/525), done.
remote: Compressing objects: 100% (87/87), done.
remote: Total 2107 (delta 490), reused 444 (delta 438), pack-reused 1582
Empfange Objekte: 100% (2107/2107), 509.73 KiB | 3.21 MiB/s, fertig.
Löse Unterschiede auf: 100% (1220/1220), fertig.
Von https://gitlab.example.com/nnis/nnis-client-python
 * [neue Referenz]   HEAD       -> origin/HEAD
 * [neues Tag]       0.1        -> 0.1
 * [neues Tag]       0.1.2      -> 0.1.2
 * [neues Tag]       0.2        -> 0.2
 * [neues Tag]       1          -> 1
 * [neues Tag]       2.0        -> 2.0
 * [neues Tag]       2.1        -> 2.1

When creating a git repository and entering the git fetch command manually I got a prompt for username and password.

Trying with a different directory that has been shown in the uv error message:

❯ (cd /home/mbeckert/.cache/uv/git-v0/db/5485a180eb2a444a; git status)
Auf Branch master

Noch keine Commits

nichts zu committen (erstellen/kopieren Sie Dateien und benutzen
Sie "git add" zum Versionieren)

-> a git repository 👍

Another try with --git-dir:

❯ /usr/bin/git --git-dir=/home/mbeckert/.cache/uv/git-v0/db/5485a180eb2a444a fetch --force --update-head-ok 'https://gitlab.example.com/nnis/nnis-client-python' '+HEAD:refs/remotes/origin/HEAD'
(english: no git directory) Schwerwiegend: Kein Git-Repository: '/home/mbeckert/.cache/uv/git-v0/db/5485a180eb2a444a'

-> no git repository?! 🤨

Facing similar issue with uv version 0.6.14 inside docker.

fatal: could not read Username for 'https://github.com': terminal prompts disabled

my pyproject.toml

xyz = { git = "https://github.com/abc/xyz.git", rev = "3207de62c52e02a55ecf536024069fd406b0bab0" }

I am using NETRC file for storing the PAT github token.