Kib*_*bee 11 tags post gitlab gitlab-ci gitlab-ci-runner
我想在管道中运行特定作业,我认为为作业分配一个标签,然后在 post 方法中再次指定此标签将满足我的需求。问题是当我使用 api(post) 触发时,所有作业在管道中触发事件虽然只有一个标记为 .
gitlab-ci.yml :
job1: 脚本: - echo "helloworld!" 标签: [我的标签]
job2: 脚本: - echo "hello gitlab!"
api 调用: curl -X POST -F token="xxx" -F ref="myTag" https://gitlab.com/api/v4/projects/12345678/trigger/pipeline
Nao*_*dgi 11
向您的触发器 api 调用添加一个变量,如下所示:
https://docs.gitlab.com/ee/ci/triggers/#making-use-of-trigger-variables
然后only
在您的gitlab.yml
文件中使用prperty ,如下所示:
https://docs.gitlab.com/ee/ci/variables/#environment-variables-expressions
那么只有当变量存在时,作业才会被执行
例如
job1:
script: echo "HELLO"
only:
variables:
- $variables[API_CALL]=true
Run Code Online (Sandbox Code Playgroud)
GitLab 中的更改可能会使上述答案不起作用。这
only:
variables:
- $variables[....]
Run Code Online (Sandbox Code Playgroud)
语法触发 CI Lint。
对于像我一样来到这里的其他人,这是我触发特定工作的方式:
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"
Run Code Online (Sandbox Code Playgroud)
except - pipelines
作业 1 中的,否则,您将进入无限子管道循环!通过使用变量,您可以:
使用此 curl 命令通过变量触发管道
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"
Run Code Online (Sandbox Code Playgroud)
当然,您必须相应地设置变量。
使用适当的变量定义您的工作:
job1:
script: echo "HELLO for job1"
only:
variables:
- $variables[TRIGERRED_JOB] == "JOB1"
job2:
script: echo "HELLO for job2"
only:
variables:
- $variables[TRIGERRED_JOB] == "JOB2"
Run Code Online (Sandbox Code Playgroud)
如果您从另一个/相同的工作中运行 curl,您可以使用 ${CI_JOB_TOKEN} 而不是 $TOKEN 和
https://docs.gitlab.com/ee/ci/triggers/#making-use-of-trigger-variables