【问题标题】:Gitlab: trigger manual action via APIGitlab:通过 API 触发手动操作
【发布时间】:2020-11-05 10:26:10
【问题描述】:

我有一个如下所示的 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"
[...]

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

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

感谢您的帮助!

【问题讨论】:

  • 您遇到的错误是什么?
  • 请发布错误。我们不知道这是身份验证问题还是 ID 错误
  • 我没有收到错误。它似乎不起作用。无论如何,我用 curl 请求的输出和管理员 gitlab 日志更新了信息

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


【解决方案1】:

您使用的 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

【讨论】:

  • 非常感谢亚当的回答!这是我正在寻找的解释:)
猜你喜欢
  • 2021-05-09
  • 2022-12-18
  • 1970-01-01
  • 2019-07-05
  • 2020-05-17
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多