简体   繁体   English

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

[英]How to fetch entire repository with GitLab CI/CD?

I'm currently setting up GitLab CI/CD.我目前正在设置 GitLab CI/CD。 We use GitVersion in our project, which throws the following error:我们在项目中使用了 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.

According to this blog this happens, when the CI-server does not fetch the full repository (we have both a develop and a master branch, but I'm working on a different one).根据这个博客,当 CI 服务器没有获取完整的存储库时会发生这种情况(我们既有开发分支,也有主分支,但我正在处理不同的分支)。 For Jenkins we solved this problem by expanding the checkout stage:对于 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}"
}}

I'm essentially looking for something equivalent to this for the .gitlab-ci.yml file.我本质上是在为.gitlab-ci.yml文件寻找与此等效的东西。

By default, runners download your code with a 'fetch' rather than a 'clone' for speed's sake, but it can be configured a number of ways.默认情况下,为了速度,跑步者使用“获取”而不是“克隆”下载您的代码,但可以通过多种方式进行配置。 If you want all jobs in your project's pipeline to be cloned rather than fetched, you can change the default in your CI Settings:如果您希望项目管道中的所有作业都被克隆而不是获取,您可以在 CI 设置中更改默认值:

设置栏 在此处输入图像描述

If you don't want all your jobs to clone since it's slower, you can change it in your.gitlab-ci.yml for your job:如果您不希望克隆所有作业,因为它较慢,您可以在 your.gitlab-ci.yml 中为您的作业更改它:

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

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

Note: You can also set this variable to none , which is useful if you don't need the code but maybe an artifact created by a previous job.注意:您也可以将此变量设置为none ,如果您不需要代码但可能需要以前作业创建的工件,这很有用。 Using this, it won't checkout any code, but skip straight to your script.使用它,它不会检查任何代码,而是直接跳到您的脚本。

You should do it in script manually:您应该手动在script中执行此操作:

script:
    - git fetch origin master
    - git diff master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM