Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a specific job in gitlab

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

like image 619
Kiblawi_Rabee Avatar asked Jun 21 '19 08:06

Kiblawi_Rabee


1 Answers

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

like image 102
GuilleW Avatar answered Oct 12 '22 14:10

GuilleW