【问题标题】:Make DEPLOY job automatic for release branches and manual for others with GitLab CI使用 GitLab CI 使发布分支的 DEPLOY 作业自动化,其他人的手动部署作业
【发布时间】:2018-08-30 07:07:02
【问题描述】:

下一个解决方案应该有效。

deploy_release:
  stage: deploy
  tags:
  - linux
  only: 
  - master
  - stable
  retry: 2
  script:
  - do_action 1
  - do_action 2
  - git push artifacts

deploy_manual:
  stage: deploy
  tags:
  - linux
  except: 
  - master
  - stable
  when: manual
  retry: 2
  script:
  - do_action 1
  - do_action 2
  - git push artifacts

但它有一个 ☝️ 显着缺乏 - script: 重复了 2 次。

我认为这样写是个好主意:

.deploy_base:
  stage: deploy
  tags:
  - linux
  retry: 2
  script:
  - do_action 1
  - do_action 2
  - git push artifacts

deploy_release:
  include: .deploy_base
  only: 
  - master
  - stable

deploy_manual:
  include: .deploy_base
  except: 
  - master
  - stable
  when: manual

但我怀疑这会奏效。 是否可以在 YAML 中做类似的事情?


另一个直截了当的想法是

script:移动到单独的文件deploy_script.sh

并在萌芽状态完成问题。

【问题讨论】:

标签: deployment yaml gitlab-ci


【解决方案1】:

这里是 https://docs.gitlab.com/ce/ci/yaml/README.html#extends

extends

在 GitLab 11.3 中引入

extends 定义了一个条目名称,使用 extends 的作业将从中继承。
extends 替代使用更灵活和可读的 YAML 锚。

.tests:
  only:
    refs:
      - branches

rspec:
  extends: .tests
  script: rake rspec
  stage: test
  only:
    variables:
      - $RSPEC

【讨论】:

    【解决方案2】:

    感谢这个问答yaml repeated node that is a key

    解决办法是:

    .deploy_base:  &deploy_base
      stage: deploy
      tags:
      - linux
      retry: 2
      script:  &deploy_script
      - do_action 1
      - do_action 2
      - git push artifacts
    
    deploy_release:
      only:  &deploy_release_only
      - master
      - stable
      script: *deploy_script
    
    deploy_manual:
      except: *deploy_release_only
      when: manual
      script: *deploy_script
    

    甚至更好:

    继承.deploy_base:

    .deploy_base: &deploy_base
      stage: deploy
      tags:
      - DlpcsCore
      - linux
      retry: 2
      variables:
        URL: 'git@gitlab.com:Yahoo/HeavenShine-bin.git'
      script: &deploy_script
      - do_act_1
      - do_action_2
    
    deploy_release:
      << : *deploy_base
      only: &deploy_release_only
      - master
      - stable
      - CI
      #- /^master[-_].+$/
      #- /^(.+)[+]bin$/
    
    deploy_manual:
      << : *deploy_base
      except: *deploy_release_only
      when: manual
    

    要了解更多信息,请搜索 YAML 合并

    【讨论】:

      【解决方案3】:

      Gitlab 现在通过 rules 指令支持此功能:https://docs.gitlab.com/ee/ci/yaml/#rules-clauses

      【讨论】:

        猜你喜欢
        • 2016-08-08
        • 1970-01-01
        • 2023-02-16
        • 1970-01-01
        • 2023-02-11
        • 2019-03-16
        • 2022-10-13
        • 2022-06-12
        • 2019-05-22
        相关资源
        最近更新 更多