前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[gitlab] 分支删除触发 gitlab CI

[gitlab] 分支删除触发 gitlab CI

作者头像
老麦
发布2022-12-24 09:28:32
6070
发布2022-12-24 09:28:32
举报
文章被收录于专栏:Go与云原生Go与云原生

使用 environment , 在 gitlab branch 被删除的时候,触发 CI

Stopping an environment

  1. 尝试在 JOB A 中申明一个变量,并停止。
  2. 使用 on_stop action 动作, 在删除分支时(同时删除变量), 触发运行 JOB B
Stop an environment when a branch is deleted

Stop an environment when a branch is deleted GitLab

在 CI 中配置一个 环境变量 , 当 branch 被删除的时候清理该 环境变量, 触发 on_stop 动作, 需求。

随后这段代码是节选,在 delpoy_action job 中创建了一个变量 clean/$CI_COMMIT_REF_NAME, 并预置了一个 动作触发器 on_stop。当变量被删除的时候, 就会触发 deploy_clean job

代码语言:javascript
复制
delpoy_action:
  stage: deploy
  script:
    - echo "Deploy a app"
  environment:
    name: clean/$CI_COMMIT_REF_NAME # 预置变量
    url: https://$CI_ENVIRONMENT_SLUG.example.com # 其实没什么用
    on_stop: deploy_clean  # 预置触发器 及 触发动作

deploy_clean: # 清理动作
  stage: deploy
  script:
    - echo "Remove app"
  environment:
    name: clean/$CI_COMMIT_REF_NAME
    action: stop # 删除变量
  variables:
    GIT_STRATEGY: none  # 这里需要设置 git 策略为 none。否则默认策略是 fetch 或者 clone, 会因为 branch 被删除而失败。

有几点需要注意:

  1. deploy_action job 中需要设置 环境变量与触发器
  2. deploy_clean job 中需要设置 GIT_STRATEGY: none 避免默认 git 操作而造成失败: Git Strategy - GitLab
  3. 环境变量本身是有作用域的(仓库, 分支, Commit 等), 其选用应该选择与 branch/tag 生命周期一致的变量, 例如这里的 clean/$CI_COMMIT_REF_NAME: Ref Specs for Runners - GitLab
  4. delpoy_actiondelpoy_clean 两个 job 的 rules 应该保持一致, 否则可能造成 pipeline 不能覆盖的问题。

参考资料

Ref Specs for Runners

Ref Specs for Runners - GitLab

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, and none .

If left unspecified, jobs use the project’s pipeline setting .

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 熊猫云原生Go 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Stopping an environment
    • Stop an environment when a branch is deleted
    • 参考资料
      • Ref Specs for Runners
        • Git Strategy
        相关产品与服务
        对象存储
        对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档