Gitlab:通过API触发手动操作

时间:2020-11-05 10:26:10

标签: gitlab-ci gitlab-api gitlab-pipelines

我有一个GitLab-CI管道阶段,如下所示:

test:
  stage: test
  script:
    - echo test
  when: manual

我需要通过GitLab API请求触发此操作。我已经尝试过这些解决方案,但是它们似乎没有用。

curl --request POST \
  --form token=<trigger-token> \
  --form ref=<branch-name> \
https://gitlab.example.com/api/v4/projects/1/trigger/pipeline

curl --request POST \
  --headert <private-token> \
https://gitlab.example.com/api/v4/projects/1/jobs/1/play

我没有收到任何错误消息。但是,如果将curl-request输出通过管道传递到jq,则会得到以下输出:

[...]
      "started_at": null,
      "finished_at": null,
      "committed_at": null,
      "duration": null,
      "coverage": null,
      "detailed_status": {
        "icon": "status_manual",
        "text": "blocked",
        "label": "waiting for manual action",
        "group": "manual",
        "tooltip": "manual action"
[...]

这些是管理日志,但是即使管道已授权,也不会触发作业。

{"severity":"INFO","time":"2020-11-05T15:57:51.989Z","correlation_id":"z7ATZBEHCB2","message":"Pipeline authorized","project_id":148,"user_id":12}

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您使用的 API 调用是为了 trigger 该步骤,而不是手动启动它。如果您更改作业定义以接受触发器或手动操作,则您当前的 API 调用将起作用。我们可以使用 rules 关键字来做到这一点:

test:
  stage: test
  script:
    - echo test
  when: manual
  rules:
    - if: “$CI_PIPELINE_SOURCE == ‘trigger’”
       when: always

这样做是将默认值设置为 manual,这样它仍然可以在 UI 中启动,但还要检查 $CI_PIPELINE_SOURCE 预定义变量以查看管道是否被触发。如果是这样,它将始终运行。

您可以在此处查看 rules 关键字的文档:https://docs.gitlab.com/ee/ci/yaml/#rules 以及此处的所有预定义变量:https://docs.gitlab.com/ee/ci/variables/predefined_variables.html