How to run Manual Gitlab job if variable value is correct?

74 Views Asked by At

So I have the following job I created. I want the job to run only if the Value of VERSION is "TEST-INPUT". All other cases the job fails. How would I do this? So far, If I enter any value for VERSION in GITLAB, the job still runs.

test-manual-job:
  stage: test-stage
  when: manual
  variables:
    VERSION: "${VERSION}"
  rules:
  - if: '$VERSION == "TEST-INPUT"'
    allow_failure: true
  script:
    - echo "${VERSION} is the hard-coded input"
1

There are 1 best solutions below

0
sytech On

rules: are only evaluated when the pipeline is created. When you are starting a manual job, specifying any variables will not affect whether the job runs or not because the rules: were evaluated at pipeline creation time.

I want the job to run only if the Value of VERSION is "TEST-INPUT". All other cases the job fails.

Since you want to evaluate this condition after pipeline creation time, you can't use rules:. Instead, you can just add this logic to the job itself:

myjob:
  when: manual
  before_script:
    - |
      if [[ "$VERSION" != "TEST-INPUT" ]]; then
          exit 1
      fi
  script:
    - echo "Version is ${VERSION}"