How to trigger a specific job in gitlab

那年仲夏 提交于 2020-05-15 08:38:06

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/56699829/how-to-trigger-a-specific-job-in-gitlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!