set git configuration in gitlab CI for default branch to prevent hint message

Viewed 894

In my gitlab CI I always get this hint messages. Yes, I see I have to set git config --global init.defaultBranch main, but everything I'm adding in my stages / jobs of the CI gitlab config is executed after fetching.

test:
  stage: test
  image:
    name: registry.domain.com/project/ci:1.0.0
  before_script:
    - git config --global init.defaultBranch main
  script:
    - echo "something"

As I said, this is not working. At the moment of fetching, the before_script is not executed.

Using Kubernetes namespace: gitlab
Using Kubernetes executor with image 
registry.domain.com/project/ci:1.0.0 ...
Using attach strategy to execute scripts...
...
Getting source from Git repository
00:01
Fetching changes with git depth set to 50...
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /builds/Cf_z92LS/0/project/repo/.git/
Created fresh repository.

Update

My custom docker image, which is used for the CI pipeline is created like this:

FROM alpine:3.14.2

RUN apk --update add bash curl git
RUN git config --global init.defaultBranch main

But I still get these message.

Running git config --list in the CI shows me, that the config value is set correctly:

init.defaultbranch=main
fetch.recursesubmodules=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
2 Answers

As far as i experienced, the only way to disable this message is to set the config globally in the .gitconfig of the user running the gitlab-runner.

This can either be done on the underlying VM if you use the shell-runner or inside the used docker-image when using the docker-runner

Update

Altough it says global, the git-setting is user based. You'll have to set it as the same user that executes the gitlab-runner. Depending on the configuration, this might be gitlab-runner or a custom user on shell-runners or root when using the docker-executor.

the order and context of running scripts can be confusing. depending on what they do, context may differ between stages as many intermediate containers can be created and removed during execution. give it a try like this:

test:
  stage: test
  image:
    name: registry.domain.com/project/ci:1.0.0
  before_script:
  script:
    - git config --global init.defaultBranch main
    - echo "something"
Related