Eric Newsom
Eric Newsom

Reputation: 1

Gitlab-CI: Run deployment stage after merge with reference to source branch

I have n features branch that will MR and merge into a develop branch.

I have a pipeline with 3 stages:

stages: - feature-push - develop-mr-retag - develop-mr-rollout

feature-push runs on any push to a feature branch (not the develop branch we are merging into). It will test, build, and push an app in a docker image tagged with the name of the feature branch.

The latter two stages should run on commits to a branch develop after a merge request is approved and merged (assuming the source branch passed the feature-push stage). It needs to rollout the new image to some k8s pods, and it needs the name of the source branch to find the correct image.

I want to use ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} for this, but I don't think that variable exists for pipelines run after a merge, only on merge_requests pipelines. These seem to be triggered before an MR is approved, which I don't want as this is a deployment.

Is this possible or should I find a different approach?

**Edit: ** To clarify, I need to run my docker build before MR, to know it can build successfully. I don't want to just throw away that build if it's the one that gets merged, so that's why I want to build/push before MR, and deploy the previously built image after MR.

Upvotes: 0

Views: 1484

Answers (1)

Damien Durant
Damien Durant

Reputation: 51

I'm looking for the same.

For the moment, I parse the commit title of the mergecommit to extract the source branch, and I check that the branch found really exist. Here is the relevant code :

CI_MERGE_REQUEST_SOURCE_BRANCH_NAME=$(sed -r "s/^Merge branch '(.*)' into .*/\1/i"<<<$CI_COMMIT_TITLE)

if [ $(git ls-remote --heads ${CI_REPOSITORY_URL} $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME | wc -l ) -ne 1 ]; then echo "Can't find source branche ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})" && exit 1; fi

But if the default commit title is modified by anybody, this will not work.

Upvotes: 1

Related Questions