我有多个环境(dev,qa,prod),我使用.env文件来存储秘密等等。现在我切换到GitHub操作,我想使用我的.env文件并将它们声明到github yml的env
部分。
但是,从我到目前为止所看到的情况来看,我似乎无法设置文件路径,必须手动重新声明所有变量。
作为最佳做法,我应该如何进行?
发布于 2020-08-11 01:13:32
这里的一个快速解决方案可能是在需要之前手动创建.env
文件。
- name: 'Create env file'
run: |
touch .env
echo API_ENDPOINT="https://xxx.execute-api.us-west-2.amazonaws.com" >> .env
echo API_KEY=${{ secrets.API_KEY }} >> .env
cat .env
多变量的更好方法
如果您有很多env变量,只需将整个文件粘贴到名为ENV_FILE
的github秘密中,只需回显整个文件。
- name: 'Create env file'
run: |
echo "${{ secrets.ENV_FILE }}" > .env
发布于 2020-10-20 20:13:19
最简单的方法是将.env文件创建为github秘密,然后在操作中创建.env文件。
因此,第1步是将.env文件作为base64编码字符串在github中作为秘密创建:
openssl base64 -A -in qa.env -out qa.txt
或
cat qa.env | base64 -w 0 > qa.txt
然后在你的行动中,你可以做一些类似的事情
- name: Do Something with env files
env:
QA_ENV_FILE: ${{ secrets.QA_ENV_FILE }}
PROD_ENV_FILE: ${{ secrets.PROD_ENV_FILE }}
run: |
[ "$YOUR_ENVIRONMENT" = qa ] && echo $QA_ENV_FILE | base64 --decode > .env
[ "$YOUR_ENVIRONMENT" = prod ] && echo $PROD_ENV_FILE | base64 --decode > .env
确定$YOUR_ENVIRONMENT
有许多方法,但通常可以从GITHUB_REF
对象中提取。您的应用程序应该能够根据需要从.env文件中读取。
发布于 2021-05-15 04:24:30
我建议在.env
操作工作流中使用您的GitHub文件变量的3种非常简单的方法。它们的差异取决于您是将文件存储在存储库中(最糟糕的做法)还是将其排除在外(最佳实践)。
.env
文件保存在存储库中:- There are some [ready-made actions](https://duckduckgo.com/?q=github+actions+dotenv&atb=v242-1&ia=web&iai=r1-1&page=1&sexp=%7B%22biaexp%22%3A%22b%22%2C%22msvrtexp%22%3A%22b%22%2C%22earlydeep%22%3A%22a%22%7D) that allow to read the `.env` variables (e.g. [Dotenv Action](https://github.com/marketplace/actions/dotenv-action),[Simple Dotenv](https://github.com/marketplace/actions/simple-dotenv)).
.env
变量时,简单、手动、烦人)将文件保留在存储库之外:- You manually copy the content of the respective `.env` files (say `.env.stage`, `.env.production`) into the respective GitHub Actions [secret variables](https://docs.github.com/en/actions/security-guides/encrypted-secrets) (say `WEBSITE_ENV_STAGE`, `WEBSITE_ENV_PRODUCTION`).
- Then at your GitHub Actions workflow script create the `.env` file from the desired variable like this `echo "${{secrets.WEBSITE_ENV_STAGE }}" > .env` and use it in the workflow.
.env
变量,然后在GitHub上同步这些变量,只需单击一次),如上面第2项所述,文件不在存储库中。- Now you use the GitHub Actions API to [create or update the secrets](https://docs.github.com/en/rest/reference/actions#create-or-update-a-repository-secret). On your local machine in the `dev` environment you write the NodeJS script that calls the API endpoint and write the `.env` files to the desired GitHub Actions secret variable (say as above into `WEBSITE_ENV_STAGE` or to both stage and production variables at once);
这是在工作流中使用.env
文件的变量的相当广泛的选择。使用任何匹配您的偏好和情况。
仅仅为了获取信息,有第四种方式可以使用一些第三方服务,如多腾夫库或HasiCorp跳台 (有更多的此类服务),在这种方式中,您可以将秘密变量用于读取这些变量,以便在构建时使用CI/CD管道创建.env
文件。请阅读那里的细节。
https://stackoverflow.com/questions/60176044
复制相似问题