How to get source branch name, when source branch merge to destination branch

Hello Everyone,
I am Mayank , I am trying to setup a pipeline of 2 stage and I want to get source branch name, when merge branch gets merge into master branch. I tried $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME variable but it’s only available during merge_request-event (not when MR gets merged to master branch). Please help how can get source_branch name when code get’s merged to master branch. For details please see .gitlab-ci.yml

.gitlab-ci.yml

stages:
  - deploy_on_dev
  - deploy

ET_dev:
  image: 
    name: /magoyal/gitlab-ci
  tags:
    - docker
  stage: deploy_on_dev
  script:
    - echo $CI_COMMIT_BRANCH       **#output test**
    - echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME **#output test**
    - env
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'


deploy:
  image: 
    name: /magoyal/gitlab-ci
  tags:
    - docker
  stage: deploy
  script:
    - echo $CI_COMMIT_BRANCH   **#output master**
    - echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME **#no output**
    - env
  rules:
    - if: >-
          $CI_PIPELINE_SOURCE == "push"
          && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

So in the deploy job how can get the source_branch name i.e test.

I do not think you can get the name of the branch that was merged from varaibles GitLab set.

What do you need the name for? There may be another method to do what you want.

1 Like

Hi @DeveloperC,
Thanks for your reply.

What do you need the name for? There may be another method to do what you want.

Actually, I want to run a ansible playbook at both the stages i.e deploy_on_dev and deploy, and the playbook name will be same as the source branch name and that’s the reason we want to use source branch name in the deploy job Please see .gitlab-ci.yml below. If we can’t do this way can you please suggest some other method to solve this.

stages:
  - deploy_on_dev
  - deploy

ET_dev:
  image: 
    name: /magoyal/gitlab-ci
  tags:
    - docker
  stage: deploy_on_dev
  script:
    - ansible-playbook $CI_COMMIT_BRANCH.yml  # ansible-playbook test.yml
  rules:
     - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'


deploy:
  image: 
    name: /magoyal/gitlab-ci
  tags:
    - docker
  stage: deploy
  script:
    - ansible-playbook $CI_COMMIT_BRANCH.yml #ansible-playbook master.yml which is wrong, we need ansible-playbook test.yml
  rules:
    - if: >-
          $CI_PIPELINE_SOURCE == "push"
          && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH