checkout: /github/home/.gitconfig does not exist for container runs

Issue

If running a job that uses a container like the below, the file /github/home/.gitconfig will not exist, even after checkout runs a config command.

  example:
    runs-on: ubuntu-latest
    container:
      image: alpine

Here is a demo:

  1. Checkout runs, and runs /usr/bin/git config --global --add safe.directory /__w/sat/sat
  2. The global .gitconfig does not exist
  3. Any calls to git remain unsafe/dubious

The safe.directory settings set by the command git config --global --add safe.directory <path> is a no-op.

Workaround

Use the --system scope over --global:

$ git config --system --add safe.directory <path>
  example:
    runs-on: ubuntu-latest
    container:
      image: alpine

    steps:
      - uses: actions/checkout@v3
      - run: git config --system --add safe.directory <path>

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 45
  • Comments: 17

Commits related to this issue

Most upvoted comments

The following should also work, and saves you an extra run step.

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          set-safe-directory: ${{ github.workspace }}

That works for me so far

name: example
jobs:
  example:
    name: example
    runs-on: ubuntu-24.04
    container: ghcr.io/damlys/devcontainer:1.0.0
    steps:
      - name: git clone
        uses: actions/checkout@v4
      - name: git clone fix
        run: git config --system --add safe.directory $GITHUB_WORKSPACE

$GITHUB_WORKSPACE allows me to use that between repositories without changes

I’m fixing the issue in pgxn-tools by adding this line to the Dockerfile to disable the check inside the running container:

git config --system --add safe.directory '*'

Is there any reason that is likely to be a security issue?

Thanks for this. Adding a step to our job with git config --system --add safe.directory $(pwd) works around the “dubious ownership” error.

For info, did a little experiment and added a step to print the id and owner of pwd - for some reason actions/checkout@v4 checks out the repo as user 1001, even though there is no such user in the container. Output from CI

Run id
  id
  ls -ld $(pwd)
  shell: sh -e {0}
uid=0(root) gid=0(root) groups=0(root)
drwxr-xr-x 7 1001 127 4096 Aug 15 15:40 /__w/OpenAssetIO-Test-CMake/OpenAssetIO-Test-CMake

For reference, the upstream change to git that performs the “dubious ownership” check appears to be https://github.com/git/git/commit/f4aa8c8bb11dae6e769cd930565173808cbb69c8

I banged my head on what I think is this issue all day. Consider this config:

name: Test Git
on:
  push:
jobs:
  bundle:
    name: Git
    runs-on: ubuntu-latest
    container: alpine/git
    steps:
    - uses: actions/checkout@v4
    - run: git archive -v --format zip --prefix foo/ --output foo.zip HEAD

This results in this error:

fatal: detected dubious ownership in repository at '/__w/test-pgxn-tools/test-pgxn-tools'
To add an exception for this directory, call:

	git config --global --add safe.directory /__w/test-pgxn-tools/test-pgxn-tools

Shouldn’t we be able to do Git stuff with the repo in a container? Curious, I tried changing the last line to:

    - run: git config safe.directory

It simply exits with no error message, as in this run:

Run git config safe.directory
  git config safe.directory
  shell: sh -e {0}
Error: Process completed with exit code 1.

Is that because the global config file is missing? I find it simply wild that there is no error message.

Anyway, I then tried setting the global config:

    - run: git config --global --add safe.directory "$PWD"

And that works. But I admit I’m pretty confused about why this isn’t set up by default when an action starts.

So fine, I can manually put this line into my workflow YAML files, but I’m mystified as to why it’s necessary. Is it this issue or am I missing something?

Thanks for bearing with my examples.

@xjlin0, please read my previous comment. This is specific to containers

With “dubious ownership” errors, the workaround doesn’t work for me, when I switch from --global to --system, it fails with permission error

jobs:
  pytest:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code Repository
        uses: actions/checkout@v3
      - name: change --global to --system
        run: git config --system --add safe.directory /app
      - name: Build the Stack
        run:  docker-compose -f local.yml build

results:

Run git config --system --add safe.directory /app
error: could not lock config file /etc/gitconfig: Permission denied
Error: Process completed with exit code 255.

Any suggestions? thanks!