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

How to fetch entire repository with GitLab CI/CD?

我目前正在设置 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.

根据 this blog 的说法,当 CI-server 没有获取完整的存储库时(我们有一个开发分支和一个主分支,但我正在处理不同的分支) ).对于 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}"
}}

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

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

如果你不想克隆你的所有作业,因为它比较慢,你可以在你的 .gitlab-ci.yml 中为你的作业更改它:

my_job:
  stage: deploy
  variables:
    GIT_STRATEGY: clone
  script:
    - ./deploy

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

注意:您也可以将此变量设置为 none,这在您不需要代码但可能需要以前工作创建的工件时很有用。使用它,它不会检查任何代码,而是直接跳到您的脚本。