如何使用 GitLab CI/CD 获取整个存储库?

Tig*_*120 16 git continuous-integration gitlab gitlab-ci

我目前正在设置 GitLab CI/CD。我们在项目中使用GitVersion,会抛出以下错误:

/root/.nuget/packages/gitversiontask/5.3.7/build/GitVersionTask.targets(46,9): error : InvalidOperationException: Could not find a 'develop' or 'master' branch, neither locally nor remotely.

根据这篇博客,当 CI 服务器没有获取完整的存储库时(我们有一个开发分支和一个主分支,但我正在开发另一个分支),就会发生这种情况。对于 Jenkins,我们通过扩展结账阶段解决了这个问题:

stage("Checkout") { gitlabCommitStatus(name: "Checkout") {
    
    // These are the normal checkout instructions
    cleanWs()
    checkout scm
    
    // This is the additional checkout to get all branches
    checkout([
      $class: 'GitSCM',
      branches: [[name: 'refs/heads/'+env.BRANCH_NAME]],
      extensions: [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']],
      userRemoteConfigs: scm.userRemoteConfigs,
    ])

    sh "git checkout ${env.BRANCH_NAME}"
    sh "git reset --hard origin/${env.BRANCH_NAME}"
}}
Run Code Online (Sandbox Code Playgroud)

我本质上是在为该.gitlab-ci.yml文件寻找与此等效的内容。

Ada*_*all 21

默认情况下,出于速度考虑,运行者会使用“获取”而不是“克隆”来下载代码,但可以通过多种方式对其进行配置。如果您希望克隆而不是提取项目管道中的所有作业,您可以更改 CI 设置中的默认设置:

设置栏 在此输入图像描述

如果您不希望克隆所有作业,因为它速度较慢,您可以在您的作业的 .gitlab-ci.yml 中更改它:

my_job:
  stage: deploy
  variables:
    GIT_STRATEGY: clone
  script:
    - ./deploy
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关 GIT_STRATEGY 变量的更多信息:https ://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-strategy

注意:您还可以将此变量设置为none,如果您不需要代码但可能需要先前作业创建的工件,则这很有用。使用此功能,它不会签出任何代码,而是直接跳到您的脚本。

  • 在此答案中添加“GIT_DEPTH: 0”+1。 (3认同)