18

I want to run a specific job in a pipeline , I thought assigning a tag for the job and then specifying this tag again in the post method will fulfill my needs .The problem is when I trigger using the api(post) , all the jobs in the pipeline are triggered event though only one of this tagged .

gitlab-ci.yml :

job1: script: - echo "helloworld!" tags : [myTag]

job2: script: - echo "hello gitlab!"


the api call : curl -X POST -F token="xxx" -F ref="myTag" https://gitlab.com/api/v4/projects/12345678/trigger/pipeline

Kiblawi_Rabee
  • 264
  • 1
  • 3
  • 5

3 Answers3

15

add a variable to your trigger api call as shown here:

https://docs.gitlab.com/ee/ci/triggers/#making-use-of-trigger-variables

then use the only prperty inside your gitlab.yml file as shown here :

https://docs.gitlab.com/ee/ci/variables/#environment-variables-expressions

then only if the variable exists the job will be execute

for example

job1:
  script: echo "HELLO"
  only:
    variables:
      - $variables[API_CALL]=true
Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
  • Thank you very much , but what exactly am looking for is choosing the job from the api (passing it as a parameter). – Kiblawi_Rabee Jun 21 '19 at 10:41
  • This is as far as I know not possible. – Janusz Jun 21 '19 at 18:31
  • 3
    Should it be ```only: variables: - API_CALL == "true" ``` as per https://docs.gitlab.com/ee/ci/variables/#environment-variables-expressions – Pedro Oct 02 '20 at 10:21
  • Depend on your gitlab version the answer is almost one year old – Naor Tedgi Oct 02 '20 at 10:43
  • Hey how can i define mutable variables for the curl request? – Zeropointer Nov 19 '21 at 11:36
  • The only problem with this approach is that all other jobs in your CI script will still be triggered. You'll need to add `rules` for every job in your CI script to handle this if you want those jobs to run only on things like `push` events. – Ari M. Jun 30 '22 at 04:21
14

Probably changes in GitLab makes answers above not working. The

only:
  variables:
    - $variables[....]

syntax trigger CI Lint.

enter image description here

For others that come here like me, here's how I trigger a specific job:

job1:
  script: 
    - echo "HELLO for job1"
    - "curl 
      --request POST
      --form token=$CI_JOB_TOKEN
      --form ref=master
      --form variables[TRIGGER_JOB]=job2
      https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/trigger/pipeline"
  except:
    - pipelines

job2:
  script: echo "HELLO for job2"
  only:
    variables:
      - $TRIGGER_JOB == "job2"

⚠️ Note the except - pipelines in job1, else, you go in infinite Child pipeline loop!

trigger a specific job

GuilleW
  • 417
  • 4
  • 10
7

By using variables you can do:

Use this curl command to trigger the pipeline with a variable

curl --request POST --form token=${TOKEN} --form ref=master --form "variables[TRIGERRED_JOB]=job1" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/trigger/pipeline"

Ofcourse you have to set the variable accordingly.

Define your jobs with the appropriate variable:

job1:
  script: echo "HELLO for job1"
  only:
    variables:
      - $variables[TRIGERRED_JOB] == "JOB1"

job2:
  script: echo "HELLO for job2"
  only:
    variables:
      - $variables[TRIGERRED_JOB] == "JOB2"

if you are running the curl from inside another/same job you can use ${CI_JOB_TOKEN} instead of $TOKEN and

https://docs.gitlab.com/ee/ci/triggers/#making-use-of-trigger-variables

Arash
  • 400
  • 4
  • 11