1

In script section of gitlab job I want to determine if commit that triggered the job belongs to some branch.

$ git branch -a --contains $CI_COMMIT_SHORT_SHA
* (HEAD detached at 460a1f74)

I just see that head is detached on this commit, but I don't see name of branch I just pushed (which was containing this commit)

Then I run this command but with specified commit (same as above) in my local terminal.

 git branch -a --contains 460a1f74
* (HEAD detached at 460a1f74)
  deploy-job
  remotes/origin/deploy-job

It outputs branches properly.

And then I've tried to run it with older specific commit from the same branch in script on gitlab's job:

$ git branch -a --contains 460a2f55
* (HEAD detached at 460a2f55)
  remotes/origin/deploy-job

It outputs branch properly.

Why I don't see my branch in a first case?

Daniel
  • 309
  • 1
  • 3
  • 10
  • Because `$CI_COMMIT_SHORT_SHA` is not the right syntax? Because its value is not a correct SHA? – matt Oct 04 '20 at 16:00
  • Unfortunately, no. When I echo `$CI_COMMIT_SHORT_SHA` in gitlab I'm getting hash of the last commit which correctly works on local instance. Plus if syntax would be wrong I would get something like `error: malformed object name 1q1q1q1q1q` when executing `git branch -a --contains`. – Daniel Oct 04 '20 at 16:17

1 Answers1

1

I added:

git fetch origin

before

git branch -a --contains $CI_COMMIT_SHORT_SHA

It solved the problem.

Daniel
  • 309
  • 1
  • 3
  • 10
  • Well that was the second part of my comment, in a way; the SHA you were using was not correct (because there was no such SHA on that machine). – matt Oct 04 '20 at 16:30