【问题标题】:Is it possible to change Gitlab CI variable value after pipeline has started?管道启动后是否可以更改 Gitlab CI 变量值?
【发布时间】:2018-11-23 08:49:03
【问题描述】:

我正在尝试根据自己的执行进度创建一个动态 gitlab 管道。例如,我有 2 个环境,每个环境的部署将根据 before_script 中脚本的执行启用/禁用。它对我不起作用,似乎管道变量值在管道启动后无法更改。有什么建议? (请看下面我的 gitlab-ci.yml)

variables:
  RELEASE: limited

stages:
  - build
  - deploy


before_script:
  - export RELEASE=${check-release-type-dynamically.sh}

build1:
  stage: build
  script:
    - echo "Do your build here"

## DEPLOYMENT
deploy_production_ga:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  allow_failure: false
  only:
  - branches
  only:
   variables:
   - $RELEASE == "general_availability"


deploy_production_limited:
  stage: update_prod_env
  script:
  - echo "deploy environment for limited customers"
  allow_failure: false
  only:
  - branches
  only:
   variables:
   - $RELEASE == "limited"

【问题讨论】:

    标签: continuous-integration gitlab gitlab-ci continuous-deployment


    【解决方案1】:

    无法在定义中评估变量。如果您真的想使用 shell 脚本来决定部署什么,您可以使用 bash if 子句:

    stages:
      - build
      - update_prod_env
    
    build1:
      stage: build
      script:
        - echo "Do your build here"
    
    deploy_production_ga:
      stage: update_prod_env
      script:
      - if [ "$(./check-release-type-dynamically.sh)" == "general_availability" ]; then
          echo "deploy environment for all customers"
        fi
      only:
      - branches    
    
    deploy_production_limited:
      stage: update_prod_env
      script:
      - if [ "$(./check-release-type-dynamically.sh)" == "limited" ]; then
          echo "deploy environment for all customers"
        fi
      only:
      - branches    
    

    然而,这确实是一个糟糕的设计。这两个作业都会在每次提交时执行,但只有一个会做某事。最好通过分支来区分它们。仅将内容提交到您要部署到的分支:

    stages:
      - build
      - update_prod_env
    
    build1:
      stage: build
      script:
        - echo "Do your build here"
    
    deploy_production_ga:
      stage: update_prod_env
      script:
      - echo "deploy environment for all customers"
      only:
      - branches-general_availability    
    
    deploy_production_limited:
      stage: update_prod_env
      script:
      - echo "deploy environment for all customers"
      only:
      - branches-limited
    

    这样只会执行您想要执行的构建作业。

    我注意到的其他几件事:

    export RELEASE=${check-release-type-dynamically.sh} 使用 () 而不是 {} 作为子shell。此外,如果 shell 脚本位于同一目录中,则必须在前面加上 ./。它应该看起来像: export RELEASE=$(./check-release-type-dynamically.sh)

    allow_failure: false 这是 gitlab-ci 中的默认值,没有必要。

    variables:
    - $RELEASE == "general_availability"
    

    变量的语法错误,使用:

    variables:
      VARIABLE_NAME: "Value of Variable"
    

    看看https://docs.gitlab.com/ee/ci/yaml/

    【讨论】:

    • 谢谢@mles。使用分支是一种选择,但我想在 gitlab 10.7 link 中使用变量表达式,其中将根据变量值执行作业,如下所示:variables: - $RELEASE == "general_availability"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2016-05-16
    • 2022-06-22
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多