docker-run-action icon indicating copy to clipboard operation
docker-run-action copied to clipboard

Problem passing github environment variables

Open hipersayanX opened this issue 3 years ago • 2 comments

Hi, I'm trying to pass some github predefined environment variables. I've tried passing the variables as:

options: >-
  -e GITHUB_REF=${{ env.GITHUB_REF }}
  -e GITHUB_SERVER_URL=${{ env.GITHUB_SERVER_URL }}
  -e GITHUB_REPOSITORY=${{ env.GITHUB_REPOSITORY }}
  -e GITHUB_RUN_ID=${{ env.GITHUB_RUN_ID }}
  ...

but the variables are empty inside the container. Then tried

options: >-
  -e GITHUB_REF="${GITHUB_REF}"
  -e GITHUB_SERVER_URL="${GITHUB_SERVER_URL}"
  -e GITHUB_REPOSITORY="${GITHUB_REPOSITORY}"
  -e GITHUB_RUN_ID="${GITHUB_RUN_ID}"
  ...

but it just pass the text as-is without interpreting the variables. Then tried creating a heredoc in a previous step

- name: Export github environment variables
  run: |
    chmod +x export_env.sh
    export_env.sh

the contents of export_env.sh is

#!/bin/sh

cat << EOF > set_env.sh
#!/bin/sh

export GITHUB_REF="${GITHUB_REF}"
export GITHUB_SERVER_URL="${GITHUB_SERVER_URL}"
export GITHUB_REPOSITORY="${GITHUB_REPOSITORY}"
export GITHUB_RUN_ID="${GITHUB_RUN_ID}"
EOF

when importing set_env.sh in the container

source ./set_env.sh

the file set_env.sh doesn't exists inside the container. Then, how I pass those variables to the container?

hipersayanX avatar Jul 21 '21 16:07 hipersayanX

I'm also wondering how to provide the env to the container. The docker run command looks as if it's trying to provide all the env vars which are available within the workflow and some extras, but the contents are blank. I thought this was intentional obfuscation for the sake of logging but I did a sanity check with this:

-
  name: Printenv
  uses: addnab/docker-run-action@v3
  with:
    image: user/repo
    run: printenv

The only env present were those in the Dockerfile. I also tried this:

- 
  name: Printenv
  uses: addnab/docker-run-action@v3
  with:
    image: user/repo
    options: -e EXAMPLE=value -e SECRET=${{ env.SECRET }}
    run: printenv

The first EXAMPLE works by itself but the second -e will get "docker: invalid reference format."

The PR #23 solves this issue. Is there anything I can do to speed up the merge there?

archonic avatar Aug 11 '22 23:08 archonic

This is an old issue, but since it took me a while to figure out, I am writing an answer here.

I believe there are two issues between the two comments here. First is that when you are forwarding the env, you don't need to assign it. Second is that you should use the env setting under the step to set the environment variable, and there you can use the ${{ secrets.SECRET }} form. So something like this works for me:

- name: Printenv
  uses: addnab/docker-run-action@v3
  with:
    image: user/repo
    options: -e EXAMPLE=value -e SECRET
    run: printenv
  env:
    SECRET: ${{ env.SECRET }}

See here: https://github.com/addnab/docker-run-action/issues/19#issuecomment-854532202_ There is maybe an issue with some secrets not working? But looks like it's closed: https://github.com/addnab/docker-run-action/issues/21#issuecomment-873851972_

kamiyo avatar Aug 21 '23 03:08 kamiyo