I want to create a deploy job that updates the prod testing server when a merge is make from a delivery branch. We do not deliver super often which is why we have a dev testing server and a prod testing server. The prod testing server is the latest delivered version while the dev testing server is the most up to date code in main.
When a delivery is made, we make a branch in the format meta-delivery-x.x
or meta-delivery-x.x.x
. After all documentation is updated, we merge that branch into main. It is at that point that I want to deploy to the prod testing server.
I have tried various combinations of rules
but cannot seem to get the right mix. I do not want the job to run for every commit made to the delivery branch, only when it gets merged. I will put some of the examples I have tried.
- if: '$CI_COMMIT_REF_NAME == "meta-delivery-x.x" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
- if: '$CI_MERGE_REQUEST_STATE == "merged" && $CI_COMMIT_REF_NAME == "meta-delivery-x.x" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"
I already have a job to deploy to the dev testing server when any merge is made into main. I just can't figure out how to get a job to only run when that merge is from a specific branch. Here is what I am using for any merge into main:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_REF_NAME == "main"'
I am going to use regex for handling the x.x.x but all I need is the right Gitlab variable combination.
There is no clear solution for a MR that has been already merged. When it is merged, the variables like $CI_PIPELINE_SOURCE
or $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
are not available. There are some tricks to resolve it:
Using Commit Message
If the merge request creates a commit, the default template for merge commit messages is:
Merge branch '%{source_branch}' into '%{target_branch}'...
Therefore, you can have a rule in the form of
- 'if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main" && $CI_COMMIT_MESSAGE =~ /^Merge branch.*delivery.* into main/'
Using Environments
The other solution is using Environments. First we detect the MR, and then when it is merged (or closed), it is applied in deploy
job. Unfortunately if it is closed also, the pipeline will run.
pre_deploy:
stage: deploy
script:
- echo "Only detect the MR to create the env"
environment:
name: review/$CI_COMMIT_REF_SLUG
on_stop: deploy
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /delivery/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
deploy:
stage: deploy
script:
- echo "The logic for deploy"
environment:
name: review/$CI_COMMIT_REF_SLUG
action: stop
rules:
- if: $CI_MERGE_REQUEST_ID