简体   繁体   English

如何将机密作为环境变量传递到 Docker Github Action 中?

[英]How to pass secrets as environment variables into a Docker Github Action?

In my workflow I'm running a custom action using the following step:在我的工作流程中,我使用以下步骤运行自定义操作:

- name: Run action
  uses: ./backend
  env:
    MY_SECRET: ${{ secrets.MY_SECRET }}

And here's action.yml :这是action.yml

name: "Backend"
on: [pull_request]
runs:
  using: 'docker'
  image: "Dockerfile"

For some reason, MY_SECRET is empty in my Dockerfile .出于某种原因,我的Dockerfile MY_SECRET为空。 I tried accessing it both in a shell script file and the RUN command:我尝试在 shell 脚本文件和RUN命令中访问它:

RUN echo "MY_SECRET: $MY_SECRET"

But it's always empty.但它总是空的。

I tried both repository-level and organization-level secrets, but the environment variable is always empty.我尝试了存储库级别和组织级别的机密,但环境变量始终为空。

Any idea why?知道为什么吗?

You can't use $MY_SECRET inside the container.您不能在容器内使用$MY_SECRET Instead, you should pass argument through the --build-arg flag.相反,您应该通过--build-arg标志传递参数。

I think you should do something like:我认为你应该做这样的事情:

steps:
  - run: docker build --build-arg MY_SECRET=$MY_SECRET .

And then you'll be able to access MY_SECRET in the build phase.然后您将能够在构建阶段访问MY_SECRET

If you're building using an action and not manually, you should find out how to pass the build arguments.如果您使用操作而不是手动构建,您应该了解如何传递构建参数。

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

相关问题 将 Github 机密传递给 docker github 操作 - pass Github secrets to a docker github action 如何将多个环境变量传递给 docker 容器? - How to pass multiple environment variables to a docker container? 如何使用 docker 在 Angular 应用程序中传递环境变量 - How to pass environment variables in angular application with docker 为什么Docker Secrets比环境变量更安全? - Why is Docker Secrets more secure than environment variables? Docker环境变量无法正确传递 - Docker environment variables not pass correctly 我应该在 docker 中使用用户机密还是环境变量 - Should I use user-secrets or environment variables with docker 加密数据库:环境变量中的秘密与 Docker 的秘密? - Encrypting Databases: Secret in Environment Variables vs Docker Secrets? 如何将变量从 GitHub 操作传递到 Docker 容器? - How to pass a variable from GitHub action to Docker container? 如何将环境变量从 docker-compose 传递到 NodeJS 项目中? - How to pass environment variables from docker-compose into the NodeJS project? 如何将环境变量传递给docker-compose的应用程序 - How to pass environment variables to docker-compose's applications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM