【问题标题】:How to pass secrets as environment variables into a Docker Github Action?如何将秘密作为环境变量传递给 Docker Github Action?
【发布时间】:2021-12-06 02:25:38
【问题描述】:

在我的工作流程中,我正在使用以下步骤运行自定义操作:

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

这里是action.yml

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

由于某种原因,MY_SECRET 在我的Dockerfile 中为空。我尝试在 shell 脚本文件和 RUN 命令中访问它:

RUN echo "MY_SECRET: $MY_SECRET"

但它总是空的。

我尝试了存储库级别和组织级别的机密,但环境变量始终为空。

知道为什么吗?

【问题讨论】:

    标签: docker github dockerfile github-actions


    【解决方案1】:

    您不能在容器内使用$MY_SECRET。相反,您应该通过 --build-arg 标志传递参数。

    我认为你应该这样做:

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

    然后您就可以在构建阶段访问MY_SECRET

    如果您使用操作而不是手动构建,您应该了解如何传递构建参数。

    【讨论】:

    • 谢谢!在文档中找到以下内容:To access the environment variable in a Docker container action, you must pass the input using the args keyword in the action metadata file. For more information about the action metadata file for Docker container actions, see "Creating a Docker container action."
    • @Tzach 太好了。所以我相信它会做到的。
    猜你喜欢
    • 2021-11-06
    • 2020-09-29
    • 2021-06-10
    • 2022-06-16
    • 2018-12-02
    • 2020-11-07
    • 2015-08-10
    • 2018-08-23
    • 2021-12-05
    相关资源
    最近更新 更多