简体   繁体   English

如何将 gitlab ci 环境变量转储到文件

[英]how to dump gitlab ci environment variables to file

the question问题

How to dump all Gitlab CI environment variables (with variables set in the project or group CI/CD settings) to a file, but only them, without environment variables of the host on which gitlab runner is executed?如何将所有 Gitlab CI 环境变量(在项目或组 CI/CD 设置中设置变量)转储到一个文件,但只有它们,没有执行 gitlab 运行程序的主机的环境变量?

Background背景

We are using gitlab CI/CD to deploy our projects to a docker server.我们正在使用 gitlab CI/CD 将我们的项目部署到 docker 服务器。 Each project contains a docker-compose.yml file which uses various environment variables, eg db passwords.每个项目都包含一个docker-compose.yml文件,该文件使用各种环境变量,例如数据库密码。 We are using .env file to store this variables, so one can start/restart the containers after deployment from command line, without accessing gitlab.我们使用.env文件来存储这些变量,因此可以在部署后从命令行启动/重启容器,而无需访问 gitlab。

Our deployments script looks something like this:我们的部署脚本看起来像这样:

deploy: 
  script:
    #...
    - cp docker-compose.development.yml {$DEPLOY_TO_PATH}/docker-compose.yml
    - env > variables.env
    - docker-compose up -d 
    #...

And the docker-compose.yml file looks like this: docker-compose.yml文件如下所示:

version: "3"
services:
  project:
    image: some/image
    env_file:
      - variables.env
    ...

The problem is now the .env file contains both gitlab variables and hosts system environment variables and in the result the PATH variable is overwritten .现在的问题是.env文件包含 gitlab 变量和主机系统环境变量,结果PATH 变量被覆盖

I have developed a workaround with grep :我已经开发了一个解决方法grep

env | grep -Pv "^PATH" > variables.env

It allowed us to keep this working for now, but I think that the problem might hit us again with another variables which would be set to different values inside a container and on the host system.它允许我们暂时保持这个工作,但我认为这个问题可能会再次打击我们,因为另一个变量将在容器内和主机系统上设置为不同的值。

I know I can list all the variables in docker-compose and similar files, but we already have quite a few of them in a few projects so it is not a solution.我知道我可以列出 docker-compose 和类似文件中的所有变量,但我们在一些项目中已经有相当多的变量,所以这不是解决方案。

You need to add to script next command您需要添加到脚本下一个命令

script:
  ...
  # Read certificate stored in $KUBE_CA_PEM variable and save it in a new file
  - echo "$KUBE_CA_PEM" > variables.env
  ...

This might be late, but I did something like this:这可能会迟到,但我做了这样的事情:

  script:
    - env |grep -v "CI"|grep -v "FF"|grep -v "GITLAB"|grep -v "PWD"|grep -v "PATH"|grep -v "HOME"|grep -v "HOST"|grep -v "SH" > application.properties
    - cat application.properties

It's not the best, but it works.这不是最好的,但它有效。 The one problem with this is you can have variables with a string containing one of the exclusions, ie.这样做的一个问题是您可以使用包含其中一个排除项的字符串的变量,即。 "CI","FF","GITLAB","PWD","PATH","HOME","HOME","SH" "CI","FF","GITLAB","PWD","PATH","HOME","HOME","SH"

My reusable solution /tools/gitlab/script-gitlab-variables.yml :我的可重用解决方案/tools/gitlab/script-gitlab-variables.yml

variables:
  # Default values
  GITLAB_EXPORT_ENV_FILENAME: '.env.gitlab.cicd'

.script-gitlab-variables:
  debug:
    # section_start
    - echo -e "\e[0Ksection_start:`date +%s`:gitlab_variables_debug[collapsed=true]\r\e[0K[GITLAB VARIABLES DEBUG]"
    # command
    - env
    # section_end
    - echo -e "\e[0Ksection_end:`date +%s`:gitlab_variables_debug\r\e[0K"
  export-to-env:
    # section_start
    - echo -e "\e[0Ksection_start:`date +%s`:gitlab_variables_export_to_env[collapsed=true]\r\e[0K[GITLAB VARIABLES EXPORT]"
    # verify mandatory variables
    - test ! -z "$GITLAB_EXPORT_VARS" && echo "$GITLAB_EXPORT_VARS" || exit $?
    # display variables
    - echo "$GITLAB_EXPORT_ENV_FILENAME"
    # command
    - env | grep -E "^($GITLAB_EXPORT_VARS)=" > $GITLAB_EXPORT_ENV_FILENAME
    # section_end
    - echo -e "\e[0Ksection_end:`date +%s`:gitlab_variables_export_to_env\r\e[0K"
  cat-env:
    # section_start
    - echo -e "\e[0Ksection_start:`date +%s`:gitlab_variables_cat-env[collapsed=true]\r\e[0K[GITLAB VARIABLES CAT ENV]"
    # command
    - cat $GITLAB_EXPORT_ENV_FILENAME
    # section_end
    - echo -e "\e[0Ksection_end:`date +%s`:gitlab_variables_cat-env\r\e[0K"

How to use .gitlab-ci.yml :如何使用.gitlab-ci.yml

include:
  - local: '/tools/gitlab/script-gitlab-variables.yml'

Your Job:
  variables:
    GITLAB_EXPORT_VARS: 'CI_BUILD_NAME|GITLAB_USER_NAME'
  script:
    - !reference [.script-gitlab-variables, debug]
    - !reference [.script-gitlab-variables, export-to-env]
    - !reference [.script-gitlab-variables, cat-env]

Result cat.env.gitlab.cicd :结果cat.env.gitlab.cicd

CI_BUILD_NAME=Demo
GITLAB_USER_NAME=Benjamin

What you need dump all :你需要什么dump all

# /tools/gitlab/script-gitlab-variables.yml
  dump-all:
    - env > $GITLAB_EXPORT_ENV_FILENAME

# .gitlab-ci.yml
  script:
    - !reference [.script-gitlab-variables, dump-all]

I hope I could help我希望我能帮上忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM