Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.env files in Github Actions CI/CD workflows: how to provide these into the workflow

I use Github Actions workflows for my CI/CD processes for Node and PHP projects.

Within a workflow I clone my repository into Github Actions runner virtual machine. Then in order to run tests within a workflow I have to have the .env file in the cloned repository.

The problem is my .env file is not a part of repository (which is the ubuquitous practice).

To solve the problem I use what I consider a workaround: set up MY_PROJECT_ENV Github Action sercret variable, manually put there the content of my .env file and then dynamically create the .env file within my workflow with Linux console echo "${{ secrets.MY_PROJECT_ENV}}" > .env. This works.

But I would like to know are there other approaches for providing .env files to Github Actions workflows?

like image 681
Valentine Shi Avatar asked Jun 18 '20 10:06

Valentine Shi


People also ask

Where do I put the .env file?

The “.env” file Starting from v1.28 , the .env file is placed at the base of the project directory.

How do I pass variables from one workflow to another in GitHub Actions?

If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output. You can then reference this job output from a step in another job.

Should I push .env to GitHub?

You shouldn't commit/include your . env file in your git repo because env stands for environment. You will different environment variables for your LOCAL, STAGING(development), PRODUCTION environments.


1 Answers

There are 3 ways to do this I know by now. I put the answer to my own question a year after in the different question. See there.

For the sake of SO rules and findablity I put here a summary.

  1. You keep your .env file in the repository. Use dotenv actions to read your file into the workflow.

  2. You keep the file out of the repository. Then you have 2 ways of getting .env variables:

    2.1. as I wrote in my question above manually copy the file content to the GitHub actions secret variable and then in your workflow create the .env file from that variable.

    2.2. Use the GitHub Actions API to create/update the secrets: write the NodeJS script on your machine (chances are you anyway use Webpack, Gulp or the like Node thing so you have Node installed).

    The script should read the local .env files and write their content to the GH secrets. Of course you can write a custom console utilty to do this with any language you use in your project.

As easy as this :)

like image 181
Valentine Shi Avatar answered Oct 01 '22 23:10

Valentine Shi