Gitlab-ci 变量插值

huangapple go评论56阅读模式
英文:

Gitlab-ci variable interpolation

问题

在gitlab-ci中,我有两个文件存储在production/configurations.yml和staging/configurations.yml文件夹中。它们包含了每个环境的配置信息。

我想根据用户在手动运行流水线时提供的值来包含文件(可以是production或staging)。然而,我无法解决这个问题,因为变量只在作业运行时才可用,在此之前它是空的,因此验证会失败,显示以下错误:

"This GitLab CI configuration is invalid: Local file configurations.yml does not exist!"

stages:
 - build
 - deploy
 
variables:
  DEPLOY_ENVIRONMENT:
    value: "production"
    options:
      - "production"
      - "staging"

    description: "The deployment target. Set to 'staging' by default."
  DEPLOY_COLOUR:
    value: "blue"
    options:
      - "blue"
      - "green"
    description: "The deployment target. Set to 'blue' by default."    
  DEPLOY_VERSION:
    value: ""
    description: "The deployment version. e.g. 1.0.0 for production && 1.0.0-rc for staging."   
  DEPLOY_APPLICATION:
    value: "app1"
    options:
      - "app1"
      - "app2"
    description: "The application to deploy"   


include: 
 - local: $DEPLOY_ENVIRONMENT/configurations.yml
 

Update_GIT:
  stage: build
...

这种做法可行吗?有什么想法或解决方法吗?

英文:

In gitlab-ci I have two files stored in production/configurations.yml and staging/configurations.yml, folders. They hold the configurations for each environment.

I would like to include the file as per the value informed by the users when manually running the pipelines( either production or staging). However, I couldn't work out how to do that, as the variable will only be available when the job runs, until then it is null, therefore the validation fails with:

"This GitLab CI configuration is invalid: Local file configurations.yml does not exist!"

stages:
 - build
 - deploy
 
variables:
  DEPLOY_ENVIRONMENT:
    value: "production"
    options:
      - "production"
      - "staging"

    description: "The deployment target. Set to 'staging' by default."
  DEPLOY_COLOUR:
    value: "blue"
    options:
      - "blue"
      - "green"
    description: "The deployment target. Set to 'blue' by default."    
  DEPLOY_VERSION:
    value: ""
    description: "The deployment version. e.g. 1.0.0 for production && 1.0.0-rc for staging."   
  DEPLOY_APPLICATION:
    value: "app1"
    options:
      - "app1"
      - "app2"
    description: "The application to deploy"   

  

include: 
 - local: $DEPLOY_ENVIRONMENT/configurations.yml
 

Update_GIT:
  stage: build
...

is that even possible? Any ideas/workarounds, please?

答案1

得分: 1

Gitlab具有一个预定义的变量CI_COMMIT_BRANCH,它是用户提交分支的名称。假设您的用户对于暂存和生产分支有一个常见的命名约定(例如"develop"、"main"、"prod"等),您可以使用workflow:rules:variables来根据用户在流水线中运行的分支来设置该变量。

来自Gitlab文档的示例:

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "test"
      variables:
        DEPLOY_ENVIRONMENT: "staging" 
    - if: $CI_COMMIT_BRANCH == "prod" || $CI_COMMIT_BRANCH == "master"
      variables:
        DEPLOY_ENVIRONMENT: "production" 

您可以在这里找到Gitlab的所有预定义变量,包括CI_COMMIT_BRANCH及其用途。

编辑:

抱歉,我没有看到您问题的一部分,即用户手动设置变量的部分。现在我明白了。也许不是最优雅的解决方案,但您可以向您的包含引入规则,以依赖于DEPLOY_ENVIRONMENT

include:
  - local: staging/configuration.yml
    rules:
      - if: $DEPLOY_ENVIRONMENT == "staging"
  - local: production/configuration.yml
    rules:
      - if: $DEPLOY_ENVIRONMENT == "production"
英文:

Gitlab has a predefined variable CI_COMMIT_BRANCH that is the name of the user's commit branch. Assuming your users have a common naming convention for staging and production branches (i.e. "develop", "main", "prod" etc.) you can use workflow:rules:variables to set the variable according to which branch the user is running through the pipeline.

Example stolen from Gitlab documentation:

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "test"
      variables:
        DEPLOY_ENVIRONMENT: "staging" 
    - if: $CI_COMMIT_BRANCH == "prod" || $CI_COMMIT_BRANCH == "master"
      variables:
        DEPLOY_ENVIRONMENT: "production" 

All of Gitlab's predefined variables, including CI_COMMIT_BRANCH and their uses can be found here.

EDIT:

Sorry, I didn't see the part of your question where users manually set the variable. I understand now. Perhaps not the most elegant solution, but you can introduce rules to your include to be dependent on DEPLOY_ENVIRONMENT

include:
  - local: staging/configuration.yml
    rules:
      - if: $DEPLOY_ENVIRONMENT == "staging"
  - local: production/configuration.yml
    rules:
      - if: $DEPLOY_ENVIRONMENT == "production"

</details>



huangapple
  • 本文由 发表于 2023年6月13日 00:36:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定