Ben Farmer
Ben Farmer

Reputation: 2854

Gitlab CI: conditionally ignore 'needs' based on rules?

So I have a pipeline that builds docker images to be used later by other parts of the pipeline. I have set things up so this image-building part of the pipeline only triggers if special tags are included in the commit message, to avoid rebuilding them unless necessary. But there are quite a few images and now I'm trying to figure out how to make this even more fine-grained so that I can rebuild just some images when required.

Here's the idea. In my gitlab-ci.yml there is:

build-base:
  stage: build-images
  extends: .template_build_image
  variables:
    IMAGE_NAME: base
  rules:
    - if: '$CI_COMMIT_MESSAGE =~ /^.*\[build-images\].*$/s'

build-environment:
  stage: build-images
  needs: [build-base]
  extends: .template_build_image
  variables:
    IMAGE_NAME: environment
  rules:
    - !reference [build-base, rules]

...
<and others>

I can add more rules for more tags easily enough, like make a rule for build-environment to add it to the pipeline if [build-environment] is in the commit message. My problem is that build-environment needs build-base. I.e. gitlab will complain if I try to run build-environment without build-base in the pipeline. And I don't want to remove these needs relationships because when I am running the whole pipeline those relationships are important. I just want to selectively ignore them sometimes when I "manually" want to skip some initial steps in the pipeline (and just use the e.g. base image already in my image registry).

Is there some way to achieve this in gitlab CI?

Upvotes: 1

Views: 3408

Answers (1)

sytech
sytech

Reputation: 40901

For this situation, you can use needs:optional:. When the needed job is present in the pipeline, it will have the DAG behavior as normal, but if the needed job is excluded due to different rules:, it will work as normal without the needs: for that job.

myjob:
  rules:
    - if: $MAYBE
  # ...

anotherjob:
  needs:
    - job: myjob
      optional: true

Upvotes: 1

Related Questions