How to pass a Github Secret as Environment Variable to Docker?
Asked Answered
S

2

7

I'm getting started with CI/CD and Docker and i wanted to pass a connection string to docker in my workflow file.

deploy:
    runs-on: ubuntu-latest
    needs: publish
    steps:
    - name: deploy to server
      uses: appleboy/ssh-action@master
      env: 
        CONN_STRING: ${{ secrets.CONN_STRING }}
      with:
        host: ${{ secrets.SECRET_IP }}
        username: ${{ secrets.SERVER_USERNAME }}
        key: ${{ secrets.SERVER_KEY }}
        port: 22
        script: docker stop *** && docker rm **** && docker pull **** && docker run --env CONN_STRING=$CONN_STRING -d --name ******

As you can see i made an env called "CONN_STRING" which gets the connection string out of my github secrets. After that i want to pass it into the dockerscript by "CONN_STRING=$CONN_STRING". However my docker keeps crashing since I've added this. Anyone knows what I'm doing wrong? The **** are merely names of my project, which i'd like to keep private.

Seriatim answered 9/9, 2021 at 10:45 Comment(0)
S
3

Turns out you can just skip the environment variable in yml and use

CONN_STRING=${{ secrets.CONN_STRING }}
Seriatim answered 9/9, 2021 at 18:59 Comment(2)
where shall this be written CONN_STRING=${{ secrets.CONN_STRING }} (in which file)?Lavina
In github action fileFerne
H
5

You can add arg after FROM step:

ARG CONN_STRING
ENV connection_string=$CONN_STRING

and then pass it to a docker build command '--build-arg CONN_STRING=$CONN_STRING'

and then later in docker file you can refer to connection string as this ${connection_string}

Hoatzin answered 9/9, 2021 at 11:29 Comment(0)
S
3

Turns out you can just skip the environment variable in yml and use

CONN_STRING=${{ secrets.CONN_STRING }}
Seriatim answered 9/9, 2021 at 18:59 Comment(2)
where shall this be written CONN_STRING=${{ secrets.CONN_STRING }} (in which file)?Lavina
In github action fileFerne

© 2022 - 2024 — McMap. All rights reserved.