Setting environment variables in GitHub Actions can be a major pain as it is non-dynamic. Based on how many environment variables you want to use in your action, using an environment file is much easier to handle because you can reuse them in different scenarios. Sad things first: GitHub Actions does not support .env files yet. But there are some solutions on how to fix that. We briefly look into a possible solution using Python.
Structure of our .env File
We use a common, non-special file as source for our environment variables.
Python Script to set Environment Variables
We assume you have this script in the same folder as your “test.env” file
The GitHub Action
Please note that you have to set ‘ACTIONS_ALLOW_UNSECURE_COMMANDS‘ to true to make GitHub accept your Python print as actual variables. Also, the environment variables will only be available in the next step.
Finally! You may now use environment files in GitHub Actions. For example, you can use these in your tests. The full example may be found at https://github.com/code-specialist/github-actions-environment-variables.
We hope this saves you some hours researching the topic. If there are any questions, feel free to comment or contact us.
I am a developer and entrepreneur from Germany. I chose studying computer science because I love building things. I am of the opinion that there isn’t one truth (especially for computer science concepts) and whoever claims so isn’t that trustworthy in my eyes. I constantly try to improve things and myself. In my spare time I support businesses by offering them most of my services for free. Beside that I am kind of addict to e-learning courses.
Our Book Recommendations on Python (Affiliate)