在部署阶段,如何在gitlab ci中添加.env文件? [英] How do I add a .env file in gitlab ci during deployment stage?
问题描述
所以我在回购中有一个我正在处理的react/typescript应用程序,在回购中有一个我忽略的.env文件,以免泄露我的秘密,并提供一个.env-example要配置的重要环境变量的文件.我的问题是,因为我没有将.env文件推送到我的仓库中,所以当我通过 google app引擎部署应用程序时(这是在 gitlab- ci.yml 文件),这些环境变量将不会在生产环境中出现,而我需要在我的webpack.config.js文件中执行这些操作时,它们才能使我的应用正常工作.
So I have a react/typescript app in my repo that I'm working on and in my repo I have a .env file that I'm ignoring so that my secrets don't get exposed and a .env-example file of important environment variables to configure. My problem is, since I'm not pushing the .env file to my repo, when I deploy my app through the google app engine(this is done in the deployment stage in my gitlab-ci.yml file), these environment variables will not be present in production and I need them for my app to work as I do something like this in my webpack.config.js file.
const dotenv = require('dotenv').config({ path: __dirname + '/.env' });
然后
new webpack.DefinePlugin({
'process.env': dotenv.parsed
})
这是我的.gitlab-ci文件,以防万一在这里想要看到的人.
Here is my .gitlab-ci file for reference in case anyone here wants to see.
cache:
paths:
- node_modules/
stages:
- build
- test
- deploy
Build_Site:
image: node:8-alpine
stage: build
script:
- npm install --progress=false
- npm run-script build
artifacts:
expire_in: 1 week
paths:
- build
Run_Tests:
image: node:8-alpine
stage: test
script:
- npm install --progress=false
- npm run-script test
Deploy_Production:
image: google/cloud-sdk:latest
stage: deploy
environment: Production
only:
- master
script:
- echo $DEPLOY_KEY_FILE_PRODUCTION > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud config set project $PROJECT_ID_PRODUCTION
- gcloud info
- gcloud --quiet app deploy
after_script:
- rm /tmp/$CI_PIPELINE_ID.json
此外,请随时批评我的gitlab-ci.yml文件,以便我做得更好.
Also, feel free to critique my gitlab-ci.yml file so I can make it better.
推荐答案
我不知道您是否仍需要此功能,但这是我实现的目标,您想要实现的目标.
I don't know if you still need this, but this is how I achieved, what you wanted to.
-
在gitlab存储库配置中创建环境变量
Create your environment variables in your gitlab repo config
创建setup_env.sh
:
#!/bin/bash
echo API_URL=$API_URL >> .env
echo NODE_ENV=$NODE_ENV >> .env
- 修改您的
.gitlab-ci.yml
.在下面的before_script:
部分 中进行验证
- Modify your
.gitlab-ci.yml
. Upsert below to yourbefore_script:
section
- chmod +x ./setup_env.sh
- ./setup_env.sh
- 在
webpack.config.js
中,使用 https://www.npmjs.com/package/dotenv
- In
webpack.config.js
make use of https://www.npmjs.com/package/dotenv
require('dotenv').config();
这将传递webpack.config.js
文件中可用的.env
变量.
This passes your .env
variables available in webpack.config.js
file.
将此添加到您的plugins
数组(添加所需的那些变量):
Add this to your plugins
array (add those variables you need):
new webpack.DefinePlugin({
'process.env.API_URL': JSON.stringify(process.env.API_URL),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
...
})
现在,您的部署应使用gitlab设置中指定的环境变量.
Now your deployment should use your environment variables specified in you gitlab settings.
这篇关于在部署阶段,如何在gitlab ci中添加.env文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!