Running build and test when an MR is raised on a branch and running deploy when the MR is approved

We have a repo with three main branches - dev, staging, production. This is for a legacy style site and I have implemented automation of the build, test and deploy phases. On the dev branch, the pipeline runs on commit just fine and we are happy with this.

The build phase manually pulls the committed changes and runs the build steps.
The test phase deploys the built site to a kubernetes pod that is used for running the tests.
The deploy phase switches the test deployment to the live deployment (if the build and tests succeed).

On staging and production, we are using a merge results pipeline and would like to run the build and test stages when an MR is raised and the deployment stage when the MR passes the tests and is approved.

I noted that the pipeline runs when an MR is raised, but it does not appear to run when the “Merge” button is pressed in the Gitlab UI. I have tried using if conditions to test if the commit is to the target branch, but either my condition is wrong, or the pipeline is not being triggered.

I have read a number of different posts but have not been able to find a suitable guide, which makes me wonder if what we are trying to do is an acceptable practice.

For the time being, I have set the deployment stage to be manual, but ideally I would like to trigger it when the approver clicks the “Merge” button. Is this possible?

Hi @mdspencer ,

Clicking “Merge” button per se does not trigger a pipeline, but creation of a new commit as a result of the merge does. You can use the CI_COMMIT_BRANCH variable to check whether this is indeed a commit to the MR’s target branch.

Hi @alexk and thanks for taking time to respond. When the developers create the MR, the pipeline is triggering. When merge is clicked, does the resulting commit also trigger the pipeline? Since merge result commits are not to the staging or production branch, I would assign build and test to commits where the target isn’t staging or production and deploy when the target is staging or production?

For some reason, when I authorised the MR that I was testing (clicked the “merge” button), it did not appear that the pipeline was triggered. Would it not have created a commit on the target branch (staging or production), thus triggering the pipeline? Would that then be a problem with my rules?

Hello @mdspencer ,

Yes, each commit triggers a pipeline (see Get started with GitLab CI/CD | GitLab). It is up to your rules in .gitlab-ci.yml to check what event triggered it.

Thanks for confirming :slight_smile: