GitLab Rules for `master` & 'tags' only

Viewed 462

i'm looking for a solution to execute a CI/CD job only for master and tags but at the same time I would like to check if the Dockerfile exists, which requires the use of rules

What do I want to achieve

I would like to build Docker images only for master and tags without restriction to a specific tag notation. I used to be able to achieve something like this through only

gitlab-ci.yml

only:
 - master
 - tags

Unfortunately, however, with only I would have to give up my check for the "Dockerfile", so i switched to rules.

Problem with rules

However, the problem with rules is that so far the only way to work out a solution here is by using regex. This limits the convention for tags considerably.

In my example 0-9.0-9.0-9

Other notations would be prohibited, 1.0.0.1 or 1.0.0-alpha, that would have to be defined and implemented separately for each project.

Is there no option to handle this more elegantly?

Build Images:
  stage: Build
  <<: *docker
  <<: *registry
  rules: 
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH 
    - if: $CI_COMMIT_TAG =~ "/[0-9].[0-9].[0-9]/g"
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag="latest"
        echo "Running on default branch '$CI_DEFAULT_BRANCH'"
      else
        tag="$CI_COMMIT_TAG"
        echo "Running on branch 'tag/$CI_COMMIT_TAG'"
      fi   
    - docker build --pull -t "$CI_REGISTRY_IMAGE/testing:${tag}" .
    - docker push "$CI_REGISTRY_IMAGE/testing:${tag}"

Edit:

The if: $CI_COMMIT_TAG =~ "/[0-9].[0-9].[0-9]/g" does not work properly

0 Answers
Related