分支删除触发 gitlab CI
分支删除触发 gitlab CI
使用 environment , 在 gitlab branch 被删除的时候,触发 CI
Stopping an environment
- 尝试在 JOB A 中申明一个变量,并停止。
- 使用
on_stop
action 动作, 在删除分支时(同时删除变量), 触发运行 JOB B
Stop an environment when a branch is deleted
在 CI 中配置一个 环境变量 , 当 branch 被删除的时候清理该 环境变量, 触发 on_stop
动作, 需求。
随后这段代码是节选,在 delpoy_action
job 中创建了一个变量 clean/$CI_COMMIT_REF_NAME
, 并预置了一个 动作触发器 on_stop
。
当变量被删除的时候, 就会触发 deploy_clean
job
|
|
有几点需要注意:
- 在
deploy_action
job 中需要设置 环境变量与触发器 - 在
deploy_clean
job 中需要设置GIT_STRATEGY: none
避免默认 git 操作而造成失败: Git Strategy - GitLab - 环境变量本身是有作用域的(仓库, 分支, Commit 等), 其选用应该选择与
branch/tag
生命周期一致的变量, 例如这里的clean/$CI_COMMIT_REF_NAME
: Ref Specs for Runners - GitLab delpoy_action
与delpoy_clean
两个 job 的 rules 应该保持一致, 否则可能造成 pipeline 不能覆盖的问题。
参考资料
Ref Specs for Runners
When a runner picks a pipeline job, GitLab provides that job’s metadata. This includes the Git refspecs , which indicate which ref (branch, tag, and so on) and commit (SHA1) are checked out from your project repository.
The refs refs/heads/<name>
and refs/tags/<name>
exist in your project repository. GitLab generates the special ref refs/pipelines/<id>
during a running pipeline job. This ref can be created even after the associated branch or tag has been deleted. It’s therefore useful in some features such as
automatically stopping an environment
, and
merge trains
that might run pipelines after branch deletion.
Git Strategy
Git Strategy - GitLab There are three possible values:
clone
,fetch
, andnone
.
If left unspecified, jobs use the project’s pipeline setting .
- 原文链接:https://typonotes.com/posts/2021/06/30/gitlab-ci-trigger-when-branch-deleted/
- 本文为原创文章,转载注明出处。
- 欢迎 扫码关注公众号
Go与云原生
或 订阅网站 https://typonotes.com/ 。 - 第一时间看后续精彩文章。觉得好的话,请猛击文章右下角「在看」,感谢支持。