migrate-mongo icon indicating copy to clipboard operation
migrate-mongo copied to clipboard

how to load environemnt variables on github actions

Open raza2022 opened this issue 4 years ago • 2 comments

I don't know am I using it right but I have a use case where I need to load GitHub secrets with dotenv file so here is my use case I am using migrate-mongo in next js which is working fine locally within nestJS application however as I am using migration within isolated JS file, so I have to load env variables separately too, like

mongodb+srv://${secrets.DATABASE_USER}:${process.env.DATABASE_PASSWORD} so locally I just added the required dotenv module with local file path on top of migrate-mongo config file and it's working fine

require('dotenv').config({ path: '.env.development.local' })

however I couldn't able to find a solution for github action pipelines as I know in .yml file secrets can be accessed as {{ secrets.DATABASE_USER }} so I tried these ways in migrate-mongo config

require('dotenv').config() require('dotenv').config({ path: 'secrets' })

but nothing works, so my question what is the best way to achieve it on GitHub actions (with or without dotenv) any solution/guidence will be greatly appreciated

raza2022 avatar Jun 30 '21 16:06 raza2022

@Moeriki @davidmat @jesstelford @nabeards any suggestion/guidance about this use case?

raza2022 avatar Jul 06 '21 07:07 raza2022

Hello @raza2022

I don't know if I understood your issue right, but I just did something similar and it was a pain because I suck at that kind of stuff.

First, you need to understand this : https://vsupalov.com/docker-arg-env-variable-guide

Based on that, you know you will need an ENV with the secret to go into your container. This ENV will come from your ARG in the Dockerfile.

So, to do it, just pass along the secret in the github action workflow file you are using. For instance :

      - name: Build the thing
        uses: docker/build-push-action@v2
        with:
          context: .
          file: Dockerfile
          push: true
          build-args: |
            SUPER_SECRET_THAT_HAS_BEEN_SET=${{ secrets.SUPER_SECRET_THAT_HAS_BEEN_SET }}

In the Dockerfile, you will now have an ARG that is called SUPER_SECRET_THAT_HAS_BEEN_SET. Just create an ENV out of it.

ARG SUPER_SECRET_THAT_HAS_BEEN_SET
ENV SUPER_SECRET_THAT_HAS_BEEN_SET ${SUPER_SECRET_THAT_HAS_BEEN_SET:-null}

now, your should be able to access it through process.env.SUPER_SECRET_THAT_HAS_BEEN_SET.

mongodb+srv://${process.env.SUPER_SECRET_THAT_HAS_BEEN_SET}:${process.env.SUPER_SECRET_THAT_HAS_BEEN_SET}

daveboulard avatar Jul 06 '21 09:07 daveboulard

Closing this, as this is not a migrate-mongo issue.

seppevs avatar Sep 27 '23 07:09 seppevs