如何在 GitHub Actions 中使用 env 文件? [英] How do I use an env file with GitHub Actions?

查看:68
本文介绍了如何在 GitHub Actions 中使用 env 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个环境(dev、qa、prod),我使用 .env 文件来存储机密等......现在我要切换到 GitHub Actions,我想使用我的 .env 文件并声明它们进入 github 操作 yml 的 env 部分.

I have multiple environments (dev, qa, prod) and I'm using .env files to store secrets etc... Now I'm switching to GitHub Actions, and I want to use my .env files and declare them into the env section of the github actions yml.

但从我目前所见,似乎无法设置文件路径,必须手动重新声明所有变量.

But from what I've seen so far, it seems that I can not set a file path and I have to manually re-declare all variables.

作为最佳实践,我应该如何进行?

How should I proceed as best practice?

推荐答案

您使用的是 Circleci Contexts,因此您拥有每个 env 的一组秘密.我知道他们正在努力将秘密带到组织级别,也许是团队级别......如果他们会像我们在 CCI 中那样创建某种上下文,则没有任何信息.

You were using Circleci Contexts, so with that you had a set of secrets of each env. I know they are working to bring secrets to org level, and maybe team level... there is no info if they will create sort of contexts like we have in CCI.

我已经考虑在 yml 上使用 ${env}_GITHUB_KEY 添加 env 作为秘密名称的前缀,例如 STAGE_GITHUB_KEY 或 INTEGRATION_GITHUB_KEY 作为现在的解决方法......你怎么看?

I have thought on adding the env as prefix of the secret name like STAGE_GITHUB_KEY or INTEGRATION_GITHUB_KEY using ${env}_GITHUB_KEY on the yml as a workaround for now... What do you think?

---原始答案:如果我理解您的话,您已经将 dotenv 文件存储在某个地方,并且您想将所有这些秘密注入到步骤中,而不必手动将它们添加到 github 秘密并在您迁移的每个工作流中进行映射......对吗?

--- Original answer: If I understand you well, you already have the dotenv files stored somewhere and you want to inject all those secrets into the steps, without having to manually add them to github secrets and do the mapping in each workflow you migrate... right?

某人执行了一项操作,该操作读取 dotenv 文件并将其值放入输出中,因此您可以在进一步的步骤中使用它们链接.这是链接:https://github.com/marketplace/actions/dotenv-action

There is an action made by someone that reads a dotenv file and put its values into ouputs, so you can use them linked in further steps. Here is the link: https://github.com/marketplace/actions/dotenv-action

.env 文件中的任何内容都将转换为输出变量.例如带有内容的 .env 文件:

Whatever is present in the .env file will be converted into an output variable. For example .env file with content:

VERSION=1.0
AUTHOR=Mickey Mouse

你这样做:

id: dotenv
uses: ./.github/actions/dotenv-action

然后你可以像这样引用alpine版本 ${{ steps.dotenv.outputs.version }}

Then later you can refer to the alpine version like this ${{ steps.dotenv.outputs.version }}

这篇关于如何在 GitHub Actions 中使用 env 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆