Creating Empty Commits During GitLab MR Merges to Update Jira Issue Status Using Keywords

I have a GitLab setup where I want to automatically update Jira issue statuses when merge requests get merged. The system works by scanning commit messages for keywords like “Resolves”, “Closes”, or “Fixes” to change the ticket status.

My regular commits look like this:

git commit -m "TICKET-789-feature: implement user login"

I want to add an empty commit during the merge process that contains the trigger word:

git commit --allow-empty -m "Closes TICKET-789"

This should automatically move the Jira ticket to the completed state when the merge happens.

I’m stuck on two main problems:

  1. How can I get the current merge request branch name in my CI pipeline?
  2. How do I make this empty commit creation happen automatically when merging to main?

Here’s my current pipeline configuration:

create-empty-commit:
  stage: commit-stage
  script:
    - git config user.name "ci.bot"
    - git config user.email "[email protected]"
    - git fetch && git rebase origin/main
    - echo $CI_COMMIT_BRANCH ## this doesn't work in MR context
    - git commit --allow-empty -m "Closes TICKET-789"
    - git push "https://token_user:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" 'HEAD:feature-branch'
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

I already have the proper access tokens configured for repository write permissions. Any suggestions on how to solve these issues?

Your approach won’t work reliably - empty commits in merge pipelines don’t guarantee they’ll make it to the final merge commit. I’ve run into this exact issue with Jira integrations before.

Here’s what actually works: set up your Jira integration to scan merge request descriptions instead of commit messages. When you create MRs, just drop the closure keywords in the description template. The integration triggers when the MR merges, not during random pipeline runs.

If you’re stuck with the commit approach, try a post-merge pipeline that runs on your target branch after merges. Pull the ticket number from the source branch name with $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME and create the empty commit directly on main. This way Jira gets triggered at the right time without messing up your dev workflow.

Honestly, just add the Jira keywords straight to your merge commit message. Skip the empty commit stuff - when you merge, edit the message to include “Closes TICKET-789” and Jira picks it up automatically. Way simpler than pipeline automation.

I’ve hit this same GitLab-Jira integration issue before. The problem is $CI_COMMIT_BRANCH doesn’t work in merge request pipelines. Use $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME instead.

But there’s a bigger issue with your approach - you’re trying to commit to the source branch during a merge request pipeline, but the actual merge happens on the target branch. The empty commit needs to be part of the final merge commit going into main.

Easier solution: use GitLab’s merge commit message templates. Go to Project Settings > Merge requests and set a custom template like “Closes {source_branch}” (assuming your branch name has the ticket number).

If you’re stuck with the CI approach, switch to merge_result pipeline and target the merge commit directly instead of trying to modify the source branch.