Tzach
Tzach

Reputation: 13376

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?

Upvotes: 5

Views: 4873

Answers (1)

Maroun
Maroun

Reputation: 95948

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.

Upvotes: 4

Related Questions