Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem passing github environment variables #25

Closed
hipersayanX opened this issue Jul 21, 2021 · 4 comments
Closed

Problem passing github environment variables #25

hipersayanX opened this issue Jul 21, 2021 · 4 comments

Comments

@hipersayanX
Copy link

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?

@archonic
Copy link

archonic commented Aug 11, 2022

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?

@kamiyo
Copy link

kamiyo commented Aug 21, 2023

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: #19 (comment)_
There is maybe an issue with some secrets not working? But looks like it's closed: #21 (comment)_

@djarbz
Copy link

djarbz commented Oct 9, 2024

@kamiyo solution worked for me, but I had to use ${{ secrets.SECRET }} instead.
My container runs as UID 1000, but in GH Actions we need to force to run as root if we are writing to bind mounts.

- name: Pull and Run the automation container
  uses: addnab/docker-run-action@v3
  with:
    # username: ${{ secrets.DOCKER_USERNAME }}
    # password: ${{ secrets.DOCKER_PASSWORD }}
    registry: gcr.io
    image: ${{ github.repository }}:latest
    options: >-
      --hostname 'actions'
      --domainname 'runner.github.com'
      --user root
      --env TZ=America/Chicago
      --env KEY
      --env DISCORD_WEBHOOK_URL
      --env TRACE=TRUE
      --env APP_DEBUG=TRUE
      -v ${{ github.workspace }}/latest_logs:/app/latest_logs:rw
      -v ${{ github.workspace }}/question_Source:/app/questionSource:rw
    run: python3 main.py
  env:
    KEY: ${{ secrets.APP_CS_KEY }}
    DISCORD_WEBHOOK_URL: ${{ secrets.APP_DISCORD_WEBHOOK_URL }}

@hipersayanX
Copy link
Author

I did not remembered this issue existed. I have found the solution just a few months ago. github environment variables can't be referenced with the env. prefix, instead the right way is using the github. instead, like this:

options: >-
  -v ${{ github.workspace }}:/sources
  -e GITHUB_REF=${{ github.ref }}
  -e GITHUB_SERVER_URL=${{ github.server_url }}
  -e GITHUB_REPOSITORY=${{ github.repository }}
  -e GITHUB_RUN_ID=${{ github.run_id }}
  -e GIT_COMMIT_HASH=${{ github.sha }}
  -e GIT_BRANCH_NAME=${{ github.ref_name }}
  ...

Here is the related documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants