Run CI/CD only if label exists in MR

Replace this template with your information

Describe your question in as much detail as possible:

I have setup CI/CD pipeline and I am working on running few steps only if label is present in merge request.

  • What are you seeing, and how does that differ from what you expect to see?
    I see a full build not respecting the conditional

  • Consider including screenshots, error messages, and/or other helpful visuals

create-grant:
  stage: create-grant
  extends: .setup_required
  script:
    - bash ./scripts/myscript.sh
  rules: &Setting
    - if: '$CI_MERGE_REQUEST_LABELS =~ $image_label_regex && ($CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "trigger" || $CI_PIPELINE_SOURCE == "schedule" || $CI_PIPELINE_SOURCE == "web")'
      changes:
        - .gitlab-ci.yml
        - gitlab_templates/*.yml
  • What version are you on? Are you using self-managed or GitLab.com?
    Self-managed
    v13.10

I would setup some tests to verify this is doing what you expect. I would test it first locally, to confirm the shellscript does what you want, and then Iā€™d run tests in gitlab ci confirming the output when run in shell (that would use the actual values from CI for those variables as opposed to the made-up values when run locally).

Looks like there is an issue with variable expansion. Had to replace $image_regex with exact value /Example/ and it works now.

1 Like