AlejoDiaz49
AlejoDiaz49

Reputation: 95

Not able to execute GitLab runner job only if ref or variable conditions

I want that the CI only execute this job if I am on master, merge request, scheduled pipeline OR if the variable COVERAGE_TEST is equal to ON:

c++TestCoverage:
  stage: analysis
  script: "./ciScripts/testCoverageScript.sh"
  tags:
    - framework
  dependencies:
    - c++Build
  variables:
    GIT_STRATEGY: fetch
  artifacts:
    paths:
      - ./build/test_coverage/
    expire_in: 1 week
    when: on_success

I tried adding the next lines:

only:
  refs:
    - master
    - merge_requests
    - schedule
  variables:
    - $COVERAGE_TEST == "ON"

But the result is actully -> If ((Master || MergeRequest || ScheduledPipeline) && COVERAGE_TEST == ON)


I also tried with:

only:
  variables:
    - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
    - $CI_PIPELINE_SOURCE == 'merge_request_event'
    - $CI_PIPELINE_SOURCE == 'schedule'
    - $COVERAGE_TEST == "TRUE" 

but CI_PIPELINE_SOURCE == 'merge_request_event' does not work as I want because If I have the branch in merge request and I push some changes the value of CI_PIPELINE_SOURCE is push


Is there a way to do it?

Upvotes: 5

Views: 6306

Answers (1)

Nicolas Pepinster
Nicolas Pepinster

Reputation: 6239

You need to use rules and if keywords to test all variables values and put OR between the tests.

Correct configuration in your case should be :

  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" || $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "schedule" || $COVERAGE_TEST == "TRUE"'
      when: always

Upvotes: 2

Related Questions