I’m trying to make a pipeline that runs as following:
- A merge request on a Develop or Staging branch gets approved and merged into said branch
- The pipeline builds the Dockerimage
- Tags with with :repo_name-branch-merge_request_id
- Pushes it to a private DockerHub repo
But I’m having a hard time to make this work as intended. Here’s the gitlab-ci.yml file:
stages:
- build & push
docker-build:
# Using the official Docker image for the entire pipeline
image: docker:latest
stage: build & push
services:
- docker:dind
variables:
# Tags with the merge request ID for archive purposes
tag: ":$CI_PROJECT_TITLE-$CI_MERGE_REQUEST_TARGET_BRANCH_NAME-$CI_MERGE_REQUEST_IID"
before_script:
# DockerHub login
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"
script:
- echo "Building $CI_REGISTRY${tag}"
- docker build -t "$CI_REGISTRY${tag}" .
- echo "Pushing $CI_REGISTRY${tag} to DockerHub"
- docker push "$CI_REGISTRY${tag}"
rules:
# Only trigger the job on approved merge requests
- if: $CI_MERGE_REQUEST_APPROVED && $CI_COMMIT_BRANCH == 'Develop' || 'Staging'
# Run this job in branches where a Dockerfile exists if above is true
exists:
- Dockerfile
What actually happens
Every time someone makes a new branch off ‘Develop’ or ‘Staging’ and commits to that (feature) branch, the pipeline is run in that branch. And whenever you make a merge request in Develop or Staging, it runs a “detached” pipeline before it’s approved. Although that properly tags the image with the variables, I don’t want it to run until the merge request is approved. But the post-merge pipeline that runs afterwards is tagged :repo_name–
I’m new to CI pipelines, is this even the right way to approach this task? I’ve found it surprisingly hard to find any threads about a similar criteria that’s not too dated.
I’m also having issues finding any examples of using $CI_MERGE_REQUEST_APPROVED since it’s relatively new, but seems to be exactly what I want.
Any ideas?