通过UI设置Gitlab CI/CD环境变量

3

我是gitlab CI/CD设置的新手。我不想将敏感凭据(如API密钥、密码等)放入我的分支中。为此,GitLab(以及其他CI/CD服务)能够设置环境变量。

到目前为止,我已经做了以下工作:

  1. Via UI (Project ⇒ Settings ⇒ CI/CD ⇒ Variables)

    First go to Project ⇒ Settings ⇒ CI/CD ⇒ Variables and add them like this: enter image description here

    Now here trying to get the File with all your config-values(e.g. with dotenv).

    require("dotenv");
    module.exports = process.env.NODE_ENV.trim() === "production" ? _config.production : _config.development;
    

当前的 .gitlab-ci.yaml 文件为:

image: node:8.9.0

cache:
  paths:
   - node_modules/

stages:
 - ver
 - init
 - test
 - build
 - deploy

 ver:
   stage: ver
   script:
   - node -v

init:
  stage: init-dev
  script:
   - npm install
  tags:
   - dev_server
  only:
   - dev  
  variables:
    ENV_PRODUCTION: "/builds/AkdiD/8/abcde/projectName/ENV_PRODUCTION" 
   
test:
   stage: test
   script:
    - npm test

build:
 stage: build
 script:
  - echo "BUILD_VERSION=production" >> build.env
 artifacts:
   reports:
   dotenv: build.env   

 deploy:
   stage: deploy-dev
   script:
    - npm run killcurrent
    - echo $ENV_PRODUCTION
    - echo $BUILD_VERSION
    - npm run staging
   tags:
    - dev_server
   only:
    - dev
问题: 我需要将ENV_PRODUCTION文件名放在哪里(yaml文件或其他地方),以便服务器获取该值? 编辑变量如下所示- 但服务器仍未获取这些变量。我是否应更改/放置.gitlab-ci.yml文件中的某些内容?

enter image description here

1个回答

1

设置文件类型的自定义环境变量(GitLab 11.11+)似乎不是引用/设置包括敏感信息在内的变量列表的正确方式。
文件类型的变量通常用于表示证书等内容。

您应该定义变量,可能是组级别的环境变量

您可以定义每个项目或每个组的变量,并将其设置为管道环境变量。
组级别的变量存储在存储库之外(不在.gitlab-ci.yml中),并安全地传递给GitLab Runner,在管道运行期间使它们可用。
对于不使用外部密钥存储或使用GitLab与HashiCorp Vault的集成的高级用户,我们建议使用组环境变量来存储密码、SSH密钥和凭据等机密信息。


感谢@VonC,我已经更新了自定义环境变量(因为我在帖子中更新了图像),但服务器仍然没有获取这些变量。我已经阅读了文章,但是并没有得到太多的信息。我应该在.gitlab-ci.yml文件中更改一些内容吗?更改会是什么?还有一个问题。我可以在这里放置MONGO_DB、REDIS_HOST和其他环境变量吗?这是一个好习惯吗? - Amit Rathee
@AmitRathee 是的,您可以添加其他变量,前提是它们适用于组内的所有项目。 - VonC
1
非常感谢您的回复@VonC,即使添加了其他变量,服务器仍然无法获取这些值并显示未定义的值。我应该在.gitlab-ci.yml文件中添加什么?它会是什么? - Amit Rathee
@AmitRathee 你如何引用这些变量?就像 https://docs.gitlab.com/12.10/ee/ci/variables/#syntax-of-environment-variables-in-job-scripts 中所示的那样? - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接