This is a simple python script that creates a .env file for GitHub Actions. All of your secrets from .env in your local machine will be copied to GitHub Actions. and your workflow will be edited to use the secrets from GitHub Actions by creating .env file at the GitHub Actions Runner.
I was tired of copying and pasting my .env file to GitHub Actions, and updating my workflow with tons of echo commands.
- You don't have to copy and paste your .env file to GitHub Actions.
- You don't have to update your workflow file with echo commands.
- .env file will be created at GitHub Actions Runner, so you don't have to worry about your .env file being exposed to the public.
- Whenever you modify your .env file, you can run this script again to update your GitHub Secrets and your workflow file.
- This script will read your .env file and upload every secret to GitHub Actions, except for secrets that start with ___.
- This script will read your workflow file and fine the step named "CREATE_DOT_ENV_FILE". (If you don't have this step, please add it.)
- CREATE_DOT_ENV_FILE's run command will be replaced with a command that creates .env file with secrets from GitHub Actions.
- Because GitHub Hosted Runner have your .env file, every deployment step will be able to use your secrets.
- Whenever you modify your .env file, you can run this script again to update your GitHub Secrets and your workflow file.
git clone https://github.com/pjc1991/python-create-env-github-action.gitcp python-create-env-github-action/create_dotenv.py {your-project-path}
cp python-create-env-github-action/requirements.txt {your-project-path}
# if your project is using python already, you shouldn't copy requirements.txt
# just install required libraries to your project's virtual environment on your own
# or you could make second virtual environment for this script only. (Really?)
# Actually, if your project is using python, copying and pasting create_dotenv.py to your project will be enough.sudo apt install python3.9
# if you don't have python3.9 already
python3.9 -m venv venv
source venv/bin/activate && python3.9 -m pip install -r requirements.txt
# only if you copied requirements.txt for virtual environment- Go to Settings > Developer settings > Personal access tokens > Tokens (classic) > Generate new token
- Name your token and give it repo scope and write:packages scope.
- Copy your token and save it somewhere safe. (It will be written to .env file later.)
touch .env
# if you don't have .env file already# .env
SECRET_KEY1=secret_key1
SECRET_KEY2=secret_key2
# the following secrets is required for this script to work
___GITHUB_REPOSITORY___=your_id_or_organization/your_repository
___GITHUB_TOKEN___=your_github_token_with_repo_scope_and_so_on # see 4. Create GitHub Token
___GITHUB_ACTION_WORKFLOW_PATH___=.github/workflows/deploy.yml # example
# create_dotenv.py will not upload secrets that start with ___python3.9 create_dotenv.py