I’m using GitLab and I’m trying to leverage a feature where a Jira ticket can be automatically moved to different progress states like "done," "close," or "resolve" based on specific trigger words in commit messages, such as "Resolves," "Closes," or "Fixes." I’ve followed a commit convention, and my initial commit message looks like this:
git commit -m "JIRA-653-feat: add new feature"
My goal is to ensure that when a merge request (MR) is merged into the main branch, it always displays the commit message like "JIRA-1625-feat: add new feature." To achieve this, I decided to add an empty commit when the MR is merging into the main branch. I use the following command:
git commit --allow-empty -m "Fixes JIRA-653"
The idea is that this empty commit should progress the Jira ticket’s status to either "Resolves" or "Fixes" when it gets merged into the main branch.
However, I’m encountering two issues:
-
I don’t know how to obtain the name of the current MR branch in my GitLab CI/CD pipeline.
-
I’m unsure how to trigger the addition of the empty commit when the MR is merged into the main branch.
Here’s the job I’ve defined in my GitLab CI/CD pipeline:
add-empty-commit:
stage: empty-commit
script:
- git config user.name "john.doe"
- git config user.email "john.doe@gmail.fi"
- git fetch && git rebase origin/main
- echo $CI_COMMIT_BRANCH ## does not work in MR
- git commit --allow-empty -m "Fixes Jira-653"
- git push "https://project_access_token_name:$GITLAB_API_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" 'HEAD:mc' ## added hardcode branch name(mc).
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
I’ve already created a GitLab access token with write access to the repository from my CI/CD pipeline.
Could someone help me with these two issues?
>Solution :
Source branch name can be acquired from variable CI_MERGE_REQUEST_SOURCE_BRANCH_NAME in merge request events, see https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
Merged result pipelines need to be enabled from Settings – Merge requests – Merge options, and it’s a premium feature. See https://docs.gitlab.com/ee/ci/pipelines/merged_results_pipelines.html for more details.
Instead of doing all this, you can just modify the default merge message in GitLab Merge request settings to comply with your needs. The default is Merge branch '%{source_branch}' into '%{target_branch}', if you branch name always matches a JIRA id, change it to something like Fixes %{source_branch}.