GitLab CI GIT STRATEGY=克隆作业不起作用

标签 git gitlab-ci

我有一个如下所示的 GitLab CI 管道:

image: mambaorg/micromamba

stages:
- test

job-name:
    stage: test
    variables:
        GIT_STRATEGY: clone
    script:
        - ls -l .git
        - id
        - micromamba install -c conda-forge git
        - git config --global --add safe.directory $CI_PROJECT_DIR
        - git branch -r

打印出来的只是origin/name_of_my_branch,我看不到例如起源/主控

第一个命令就在那里,否则我根本无法直接使用 git。

根据文档,我希望看到所有远程分支 - 有什么想法吗?

我的输出如下(仅过滤我在这里谈论的内容)

$ ls -l .git total 52
-rw-rw-rw- 1 root root   240 Aug 28 13:27 FETCH_HEAD
-rw-rw-rw- 1 root root    41 Aug 28 13:27 HEAD
-rw-rw-rw- 1 root root   358 Aug 28 13:27 config
-rw-rw-rw- 1 root root 17572 Aug 28 13:27 index drwxrwxrwx 3 root root  4096 Aug 28 13:27 lfs drwxrwxrwx 3 root root  4096 Aug 28 13:27 logs drwxrwxrwx 4 root root  4096 Aug 28 13:27 objects drwxrwxrwx 6 root root  4096 Aug 28 13:27 refs
-rw-rw-rw- 1 root root   492 Aug 28 13:27 shallow $ id uid=57439(mambauser) gid=57439(mambauser) groups=57439(mambauser)

$ git branch -r   origin/test_CI

最佳答案

确实是 GIT_STRATEGY: clone设置将从头开始为每个作业克隆存储库,确保本地工作副本始终是原始的。

但是,正如“Optimize GitLab for large repositories”中提到的,GitLab 和 GitLab Runner 执行 shallow clone by default .
通过Settings > CI/CD进行配置,展开General pipelines/Git策略Gitshallow clone:最大值为 1000。要禁用浅克隆并使 GitLab CI/CD 每次获取所有分支和标签,请将该值保留为空或设置为 0。

在您的情况下,仅获取 CI 作业正在运行的特定分支。
要获取所有远程分支,您可以手动将以下行添加到 .gitlab-ci.yml 中的 script 部分:

script:
  - git fetch --unshallow
  - git fetch origin

您将使用此修改后的 gitlab-ci.yml:

image: mambaorg/micromamba

stages:
- test

job-name:
    stage: test
    variables:
        GIT_STRATEGY: clone
    script:
        - ls -l .git
        - id
        - micromamba install -c conda-forge git
        - git config --global --add safe.directory $CI_PROJECT_DIR
        - git fetch --unshallow
        - git fetch origin
        - git branch -r

git Branch -r 现在应该列出所有远程分支。


OP Michele Peresano确认in the comments一个简单的 git fetch origin $CI_DEFAULT_BRANCH 就足以使所有远程分支可见。

这证实了 default refspec +refs/heads/*:refs/remotes/origin/* (获取所有分支),尽管最初是 clone is done only for one branch .

关于GitLab CI GIT STRATEGY=克隆作业不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76993476/

相关文章:

database - 使用 git/gerrit 迭代数据库迁移

build - Gitlab-CI:gitlab ci trigger build 仅用于合并请求

docker - 如何让 gitlab-runner 从指定文件而不是 .gitlab-ci.yml 读取?

security - 用 gitlab 卑鄙地打嗝

Python 子进程 git 提交消息只接受单个字符串

svn - 为网站的git repo设置项目目录结构

git - 在 Azure DevOps 中使用 GitFlow(是否意味着多个管道?)

android - 为什么有的模块显示蓝色名字,有的显示白色名字

docker - 如何解决 "The cypress npm package is installed, but the Cypress binary is missing."

macos - 如何在 osx 上为 gitlab-runner exec docker 修复 'Job failed: invalid volume specification'