Can we trigger a pipeline by merge rquests labels?

Hi there,

Can we trigger a pipeline by merge rquests labels?

I only see pipeline can be triggered by webhook - merge requets created/updated/merged,

but I wanted to enable similar functions for labels, like adding a new label to the MR and triggers a pipeline.

Hi @xuanliu-aa welcome to the forum

You need rules for this, and also some predefined variables. It’s also worth knowing about workflows which define whether MR pipelines are run.

Having said all that, I think you need something vaguely like this:

stages:
    - build

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_IID

build:
    stage: build
    rules:
        - if: '$CI_MERGE_REQUEST_LABELS =~ /bug/'
          when: always
        - when: never
    script:
        - echo $CI_MERGE_REQUEST_LABELS
        - echo "The bug label is defined."
2 Likes

Hi @snim2, really appreciate!

I tried your code, and I found one question:
If I created the MR with the bug label, it will trigger the label sscript;

If I created the MR without the label, it will not trigger; even if I add the label later on the MR view, the CI will not trigger the script as well. Is it possible to do so?

So, the pipeline is triggered when you push to your MR branch. By default, all pipelines are triggered on a push event.

However, if you want to trigger the pipeline on an event, you need to write a little service that will use the API to trigger the pipeline directly. You can read about triggers in these docs.

You’ll still need something in your .gitlab-ci.yml, and you can use $CI_PIPELINE_SOURCE == "trigger" to check whether the pipeline has been triggered by your web server, rather than a git push.

2 Likes

it seems that, if I add the label later, though the test is not triggered, I could go to pipeline tab and choose to rerun, and it will show the missing test. Works for me so far.

anyway, thank you!

2 Likes