2

I want to run a pipeline that builds a docker image from an app and deploys it to Gitlab Registry and Docker Hub.

I'd like this pipeline to run only when the main branch gets a commit and when that commit has a message that is a "version". Examples of versions: 1.0.0 3.4.0 10.1.6

I have made the following gitlab-ci.yml, however, my pipeline never triggers. What am I doing wrong? I've already checked the regex and it's ok, so I'm guessing I'm doing something wrong with the Gitlab config?

image: docker:stable

variables:
    PROJECT_NAME: "project"
    BRANCH_NAME: "main"
    IMAGE_NAME: "$PROJECT_NAME:$CI_COMMIT_MESSAGE"
    LATEST_IMAGE_NAME: "$PROJECT_NAME:latest"

services:
    - docker:19.03.12-dind

build_image:
    script:
      # Push to Gitlab registry
      - docker build -t $CI_REGISTRY/username/$PROJECT_NAME/$IMAGE_NAME .
      - docker build -t $CI_REGISTRY/username/$PROJECT_NAME/$LATEST_IMAGE_NAME .
      - docker login $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
      - docker push $CI_REGISTRY/username/$PROJECT_NAME/$IMAGE_NAME
      - docker push $CI_REGISTRY/username/$PROJECT_NAME/$LATEST_IMAGE_NAME
      # Push to Docker hub
      - docker build -t username/$IMAGE_NAME .
      - docker build -t username/$LATEST_IMAGE_NAME .
      - docker login -u username-p $DOCKER_HUB_TOKEN
      - docker push username/$IMAGE_NAME
      - docker push username/$LATEST_IMAGE_NAME
    rules:
      - if: $CI_COMMIT_MESSAGE =~ /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
      - if: $CI_PIPELINE_SOURCE == "$BRANCH_NAME"
RabidTunes
  • 775
  • 1
  • 8
  • 21
  • Try without the `$` at the end of your pattern. – sytech Aug 19 '22 at 19:50
  • 1
    God dammit you're right, I've been banging my head against the wall for 2 days with this... I've also removed the `^` at the beggining and now it works. You can add it as answer and I'll mark it as the solution, thanks pal – RabidTunes Aug 19 '22 at 20:04

2 Answers2

1

I propose to you an alternative, that is tags.

A tag is a label that you attach on git commits. Examples of tags are: 1.0.1, 1.1.2, latest.

In order to tag a commit with a version, just issue: git tag <version> (the newest commit is subtended here)

In order to trigger your pipeline for any tag, just use this:

rules:
  - if: $CI_COMMIT_TAG

In this solution, we have decoupled the version issue from the message concern.

Davide Calarco
  • 432
  • 4
  • 6
  • But would this trigger the Gitlab Pipeline when adding the tag or when commiting and pushing the commit with the tag? – RabidTunes Aug 19 '22 at 21:28
  • 1
    When you push, GitLab receives a commit with a tag. Given the above rule, since the commit tag is not empty, GitLab starts the pipeline. – Davide Calarco Aug 20 '22 at 08:42
1

The problem you're having is likely due to the $ marker. Often commit messages get formatted with additional newlines, which would make the pattern not match.

Alternatively, your pattern may work if using CI_COMMIT_TITLE which only includes the first line of the commit message.

sytech
  • 29,298
  • 3
  • 45
  • 86