addnab / docker-run-action

MIT License
209 stars 93 forks source link

Problem passing github environment variables #25

Closed hipersayanX closed 1 week ago

hipersayanX commented 3 years ago

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 commented 2 years ago

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 commented 1 year ago

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_

djarbz commented 1 week ago

@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 commented 1 week ago

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.