1

I have a GitLab CI pipeline that looks like this:

image: mambaorg/micromamba

stages:
- test

job-name:
    stage: test
    variables:
        GIT_STRATEGY: clone
    script:
        - ls -l .git
        - id
        - micromamba install -c conda-forge git
        - git config --global --add safe.directory $CI_PROJECT_DIR
        - git branch -r

What gets printed out is only origin/name_of_my_branch and I cannot see e.g. origin/master.

The first command is there because otherwise, I cannot use git at all directly.

According to the documentation, I would expect to see all remote branches, though - any idea?

My output is the following (filtering only what I am talking about here)

$ ls -l .git total 52
-rw-rw-rw- 1 root root   240 Aug 28 13:27 FETCH_HEAD
-rw-rw-rw- 1 root root    41 Aug 28 13:27 HEAD
-rw-rw-rw- 1 root root   358 Aug 28 13:27 config
-rw-rw-rw- 1 root root 17572 Aug 28 13:27 index drwxrwxrwx 3 root root  4096 Aug 28 13:27 lfs drwxrwxrwx 3 root root  4096 Aug 28 13:27 logs drwxrwxrwx 4 root root  4096 Aug 28 13:27 objects drwxrwxrwx 6 root root  4096 Aug 28 13:27 refs
-rw-rw-rw- 1 root root   492 Aug 28 13:27 shallow $ id uid=57439(mambauser) gid=57439(mambauser) groups=57439(mambauser)

$ git branch -r   origin/test_CI
Michele Peresano
  • 154
  • 1
  • 11

1 Answers1

1

It is true that a GIT_STRATEGY: clone setting would clone the repository from scratch for every job, ensuring that the local working copy is always pristine.

But, as mentioned in "Optimize GitLab for large repositories", GitLab and GitLab Runner perform a shallow clone by default.
That is configured by Settings > CI/CD, expand General pipelines / Git strategy, Git shallow clone: The maximum value is 1000. To disable shallow clone and make GitLab CI/CD fetch all branches and tags each time, keep the value empty or set to 0.

In your case, only the specific branch that the CI job is running for is fetched.
To fetch all remote branches, you can manually add the following lines to your script section in your .gitlab-ci.yml:

script:
  - git fetch --unshallow
  - git fetch origin

You would use this modified gitlab-ci.yml:

image: mambaorg/micromamba

stages:
- test

job-name:
    stage: test
    variables:
        GIT_STRATEGY: clone
    script:
        - ls -l .git
        - id
        - micromamba install -c conda-forge git
        - git config --global --add safe.directory $CI_PROJECT_DIR
        - git fetch --unshallow
        - git fetch origin
        - git branch -r

git branch -r should now list all the remote branches.


The OP Michele Peresano confirms in the comments that a simple git fetch origin $CI_DEFAULT_BRANCH is enough to make all remote branches visible.

That confirms the default refspec +refs/heads/*:refs/remotes/origin/* (which fetches all the branches), even though, initially, the clone is done only for one branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Indeed, I solved the problem actually without changing `GIT_STRATEGY`, only adding `git fetch origin $CI_DEFAULT_BRANCH` – Michele Peresano Aug 29 '23 at 09:28
  • @MichelePeresano Interestting, no need for unshallowing the repository, then? `git fetch origin $CI_DEFAULT_BRANCH` should only fetch *one* remote branch though... – VonC Aug 29 '23 at 12:10
  • by doing this I actually see all of them, not only the default branch – Michele Peresano Aug 29 '23 at 16:13