GitLab CI 手动启动作业(部署)
GitLab CI Start job manually (deployment)
是否可以将 gitlab ci 作业标记为手动启动?
我需要它来部署应用程序,但我想 decide 如果要部署它
afaik 不直接支持手动批准的构建步骤。但是应该可以通过使用 ci triggers.
来实现类似的行为
build_package:
stage: build
script:
- make build
upload_package:
stage: package
script:
- if [ -n "${UPLOAD_TO_S3}" ]; then make upload; fi
然后您可以通过发出 POST 请求并传递配置的变量来触发重建。
curl -X POST \
-F token=TOKEN \
-F ref=master \
-F "variables[UPLOAD_TO_S3]=true" \
https://gitlab.example.com/api/v3/projects/9/trigger/builds
如果你有自己的 gitlab 实例,应该可以在每个合并请求上注入 javascript 按钮,这将进行 curl 调用。
自第一个答案发布以来,这已经发生了变化。这是原始 Gitlab Issue 的 link。现在支持做类似
的事情
production:
stage: deploy
script: run-deployment $OMNIBUS_GITLAB_PACKAGE
environment: production
when: manual
注意 when: manual
属性。 UI 自我更新,为用户提供一种触发作业的方式。
GitLab 13.5 (October 2020) adds more to the when: manual
feature, to support trigger
:
Trigger downstream or child pipelines with manual jobs
Previously, it was not possible to configure a trigger job to wait on a manual action. This made it challenging to configure either downstream or child pipeline triggers to wait for a user to click on them before running.
In this release, we’ve added the ability to add when: manual
to trigger jobs. Use this keyword to make trigger jobs wait until you click on the play button. This gives you more control of your downstream and child pipelines, and they will only run when you want them to.
See Documentation and Issue.
GitLab 14.8(2022 年 2 月)添加另一个 option/approach:
Deployment approval API
We are excited to introduce deployment approval via API.
Prior to this feature, teams had the ability to protect an environment from any changes by requiring a manual job to be executed in a pipeline as a workaround.
< 现在,部署批准是我们平台中的一个 first-class 概念。
团队可以为特定环境配置多个批准者,并使用新的 API 端点来批准或拒绝对该环境的部署。
This capability enables teams to create workflows to obtain the proper approvals before deploying software to production or other protected environments.
See Documentation and Issue.
所以:
curl --header 'Content-Type: application/json' --request POST \
--data '{"name": "production", "deploy_access_levels": [{"group_id": 9899826}], "required_approval_count": 1}' \
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/projects/22034114/protected_environments"
是否可以将 gitlab ci 作业标记为手动启动?
我需要它来部署应用程序,但我想 decide 如果要部署它
afaik 不直接支持手动批准的构建步骤。但是应该可以通过使用 ci triggers.
来实现类似的行为build_package:
stage: build
script:
- make build
upload_package:
stage: package
script:
- if [ -n "${UPLOAD_TO_S3}" ]; then make upload; fi
然后您可以通过发出 POST 请求并传递配置的变量来触发重建。
curl -X POST \
-F token=TOKEN \
-F ref=master \
-F "variables[UPLOAD_TO_S3]=true" \
https://gitlab.example.com/api/v3/projects/9/trigger/builds
如果你有自己的 gitlab 实例,应该可以在每个合并请求上注入 javascript 按钮,这将进行 curl 调用。
自第一个答案发布以来,这已经发生了变化。这是原始 Gitlab Issue 的 link。现在支持做类似
的事情production:
stage: deploy
script: run-deployment $OMNIBUS_GITLAB_PACKAGE
environment: production
when: manual
注意 when: manual
属性。 UI 自我更新,为用户提供一种触发作业的方式。
GitLab 13.5 (October 2020) adds more to the when: manual
feature, to support trigger
:
Trigger downstream or child pipelines with manual jobs
Previously, it was not possible to configure a trigger job to wait on a manual action. This made it challenging to configure either downstream or child pipeline triggers to wait for a user to click on them before running.
In this release, we’ve added the ability to add
when: manual
to trigger jobs. Use this keyword to make trigger jobs wait until you click on the play button. This gives you more control of your downstream and child pipelines, and they will only run when you want them to.
See Documentation and Issue.
GitLab 14.8(2022 年 2 月)添加另一个 option/approach:
Deployment approval API
We are excited to introduce deployment approval via API.
Prior to this feature, teams had the ability to protect an environment from any changes by requiring a manual job to be executed in a pipeline as a workaround.
< 现在,部署批准是我们平台中的一个 first-class 概念。
团队可以为特定环境配置多个批准者,并使用新的 API 端点来批准或拒绝对该环境的部署。
This capability enables teams to create workflows to obtain the proper approvals before deploying software to production or other protected environments.
See Documentation and Issue.
所以:
curl --header 'Content-Type: application/json' --request POST \
--data '{"name": "production", "deploy_access_levels": [{"group_id": 9899826}], "required_approval_count": 1}' \
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/projects/22034114/protected_environments"