TigersEye120
TigersEye120

Reputation: 877

How to fetch entire repository with GitLab CI/CD?

I'm currently setting up GitLab CI/CD. We use GitVersion in our project, which throws the following error:

/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). For Jenkins we solved this problem by expanding the checkout stage:

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.

Upvotes: 21

Views: 35867

Answers (2)

dingyaguang117
dingyaguang117

Reputation: 76

You should do it in script manually:

script:
    - git fetch origin master
    - git diff master

Upvotes: 1

Adam Marshall
Adam Marshall

Reputation: 7705

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:

settings bar enter image description here

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:

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

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. Using this, it won't checkout any code, but skip straight to your script.

Upvotes: 25

Related Questions