How to fetch entire repository with GitLab CI/CD?

Issue

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.

Solution

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/README.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.

Answered By – Adam Marshall

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published