2

After merge there is a new commit with new SHA in gitlab.

enter image description here

In CI/CD variables there is a CI_MERGE_REQUEST_SOURCE_BRANCH_NAME variable. Also there are a few ways to know some other commit data in command line like git rev-parse --verify HEAD and etc.

So, how to get source commit SHA for special merge commit?

To understand the question clearly I'll try to describe the case.

branches: master, dev

commits: dev_1, dev_2, dev_3, merge_to_master_dev_123, dev_4, dev_5, dev_6, merge_to_master_dev_456

When I run (or rerun) the pipeline for merge_to_master_dev_123 I need to get dev_3 commit SHA.

When I run (or rerun) the pipeline for merge_to_master_dev_456 I need to get dev_6 commit SHA.

I'm going to use it in GitLab Runner in gitlab-ci.yml scripts (shell scripts especially).

And get latest request of source branch is not what I'm looking for, because I can rerun merge_to_master_dev_123 pipeline after merge_to_master_dev_456, merge_to_master_dev_... pipelines.

So how to do it?

Andrew
  • 671
  • 2
  • 8
  • 21
  • I'm not quite sure what you want to do, like is it an actual merge commit? Was master merged to dev or the other way around? Either way this will help you: https://stackoverflow.com/a/12527561/2115135 – Jakub Kania Feb 23 '19 at 18:27

1 Answers1

0

You could save the SHA in your commit message when you merge to master and then retrieve the SHA from the CI_COMMIT_MESSAGE variable.

Or better still you could tag the commit on dev when you merge and then use the tag to reference the commit.. How about tagging the commit on the dev branch with a beta pre-release tag e.g. 1.0.0-beta and then tag the merge commit without the pre-release tag 1.0.0? When you re-run the merge commit you can use the CI_COMMIT_TAG variable to get 1.0.0, add on the -beta pre-release tag and you have a git reference to the commit that you merged from.

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65