在 gitlab CI 中为默认分支设置 git 配置以防止提示消息

use*_*695 7 gitlab gitlab-ci

在我的 gitlab CI 中,我总是收到此提示消息。是的,我发现我必须设置git config --global init.defaultBranch main,但是我在 CI gitlab 配置的阶段/作业中添加的所有内容都是在获取执行的。

test:
  stage: test
  image:
    name: registry.domain.com/project/ci:1.0.0
  before_script:
    - git config --global init.defaultBranch main
  script:
    - echo "something"
Run Code Online (Sandbox Code Playgroud)

正如我所说,这是行不通的。在获取的那一刻,before_script 并没有被执行。

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.
Run Code Online (Sandbox Code Playgroud)

更新

我用于 CI 管道的自定义 docker 映像的创建方式如下:

FROM alpine:3.14.2

RUN apk --update add bash curl git
RUN git config --global init.defaultBranch main
Run Code Online (Sandbox Code Playgroud)

但我仍然收到这些消息。

在 CI 中运行git config --list表明配置值设置正确:

init.defaultbranch=main
fetch.recursesubmodules=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
Run Code Online (Sandbox Code Playgroud)

uup*_*cal 3

据我经验,禁用此消息的唯一方法是在.gitconfig运行 gitlab-runner 的用户中全局设置配置。

如果您使用 shell-runner,则可以在底层 VM 上完成此操作;如果使用 docker-runner,则可以在使用的 docker-image 内完成此操作

更新

尽管它说global,git 设置是基于用户的。您必须将其设置为执行 gitlab-runner 的同一用户。根据配置,这可能是gitlab-runnershell 运行程序上或root使用 docker-executor 时的自定义用户。