0

I am trying to trigger a particular job in CI after either of the 2 conditions

  1. trigger by another job in the same pipeline OR
  2. changes: somefile.txt

My CI is as described

job1:
    stage: build
    script: 
    - echo "JOb1"
    - curl -X POST -F token=2342344444 -F "variables[TRIGGER_JOB]=job1" -F ref=master https://main.gitlab.myconmpanyxyz.com/api/v4/projects/1234/trigger/pipeline
    only:
     changes:
     - job1.md

job2: # This does not RUN as expected because of the TRIGGER_JOB set to job1
    stage: test
    script: 
    - echo "Job2"   
    rules:
    - if: $TRIGGER_JOB =="job2"

job3: # this RUNS as expected because of VARIABLE TRIGGER_JOB
    stage: test
    script: 
    - echo "Job3"   
    rules:
    - if: $TRIGGER_JOB =="job1"

job4: # this also RUNS, but this should not be the expected behavior 
    stage: test
    script: 
    - echo “job4“   
    rules:
    - if: $TRIGGER_JOB == "xyz"
    - changes: 
      - job4.md

After job1 finishes it also needs to call job4 and not any other jobs (job2 in this case). So I am using CURL to call the job itself. If there are any better ways of calling a specific job in the same CI, also please let me know. I have already seen this stack-overflow page, but it does not help because my job needs to be triggered by either of 2 conditions which is not allowed bit gitlab-ci.

I need job4 to be called by either of the 2 conditions - if the TRIGGER_JOB=="job1" or if there are any changes in job4.md file. Currently job4 runs if changes are made in job4.md file, however it also runs if the job1 is triggered. But afaik this should not be the expected behavior. docs. Can anyone please give me some leads how to create this kind of design.

BongTech
  • 31
  • 6

1 Answers1

0

Your solution was almost correct, but the changes keyword with only or except does only work, if the pipeline is triggered by a push or a merge_request event. This is defined in the variable CI_PIPELINE_SOURCE. When you trigger the pipeline by calling the API, the variable CI_PIPELINE_SOURCE contains the value trigger and therefore only:changes returns always true, which triggers job1 again and ends in an endless loop. You can add a simple except rule to your job1 to prevent that:

job1:
    stage: build
    script: 
    - echo "JOb1"
    - curl -X POST -F token=2342344444 -F "variables[TRIGGER_JOB]=job1" -F ref=master https://main.gitlab.myconmpanyxyz.com/api/v4/projects/1234/trigger/pipeline
    only:
     changes:
       - job1.md
    except:
      variables:
        - $CI_PIPELINE_SOURCE == "trigger"

You can find more information on only/except:changes in the documentation.

Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36