简体   繁体   中英

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 :

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

For some reason, MY_SECRET is empty in my Dockerfile . I tried accessing it both in a shell script file and the RUN command:

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. Instead, you should pass argument through the --build-arg flag.

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.

If you're building using an action and not manually, you should find out how to pass the build arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM