Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read labels in Gitlab CI script

I have a few use cases in my Gitlab setup I would like to be able to support:

  1. If a certain label (let's call it “skip_build”) is set, the deployment steps should not be run when I merge an MR to a main branch. This would be useful when we have multiple MRs being merged right after another and only need the last one built.
  2. If another label (we'll call it “skip_tests”) is set, I should be able to read it as an env var from within the script and alter the flow within the script accordingly (using normal bash syntax), e.g. to alter the package command parameters used a bit. This is useful for small changes where it might not make sense to run a lengthy test suite.

Is this possible with Gitlab, and if so, how?

I’ve tried experimenting with CI_MERGE_REQUEST_LABELS, but it doesn’t seem to be able to read that as an env var from within the script.

like image 550
avlund Avatar asked Jun 07 '26 20:06

avlund


1 Answers

You have to use merge request pipelines for the CI_MERGE_REQUEST_LABELS variable (and other MR-related variables) to be present as documented in predefined variables.

You could use a rules: clause to skip jobs. Something like

build:
  rules:  # only run this job if the regex pattern does not match
    - if: $CI_MERGE_REQUEST_LABELS !~ /skip_build/

You can also do this on any other kind of predefined (or user-defined) variable, like branch name, commit messages, MR titles, etc. Whatever works for you.

For example, a built in feature of GitLab is that if your commit message contains [ci skip] it will prevent the pipeline from running. You could implement similar functionality for your jobs and/or pipelines through rules: or workflow:rules:.

like image 200
sytech Avatar answered Jun 10 '26 11:06

sytech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!