Jasper123pyah
Jasper123pyah

Reputation: 109

How to pass a Github Secret as Environment Variable to Docker?

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.

Upvotes: 7

Views: 12055

Answers (2)

Krzysztof Madej
Krzysztof Madej

Reputation: 40553

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}

Upvotes: 5

Jasper123pyah
Jasper123pyah

Reputation: 109

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

CONN_STRING=${{ secrets.CONN_STRING }}

Upvotes: 3

Related Questions