Skip to content

Instantly share code, notes, and snippets.

@jnhmcknight
Last active May 25, 2022 13:56
Show Gist options
  • Save jnhmcknight/151c084ae224547ed8bb53dffec23b54 to your computer and use it in GitHub Desktop.
Save jnhmcknight/151c084ae224547ed8bb53dffec23b54 to your computer and use it in GitHub Desktop.
Add or update repo secrets based on the contents of one or more .env files
#!/usr/bin/env python3
"""
Add or update repo secrets based on the contents of one or more .env files
To run this, you will need:
- Python3 installed
- have created a personal access token with the relevant privileges within GitHub
- copied this gist into a file named `repo-secret.py`
- and complete the following:
chmod +x repo-secret.py
pip3 install python-dotenv pygithub
export GITHUB_TOKEN="<your-personal-access-token>"
Afterwards, you can run it passing as many env filenames as you want. At least 1 env filename
is required:
repo-secret.py <gh-org-or-username>/<repo-name> <envfile1> <envfile2> ...
"""
import os
import sys
from dotenv import dotenv_values
from github import Github
if len(sys.argv) < 3:
print('USAGE: repo-secret.py ORG/REPO ENVFILE [ENVFILE [...]]')
sys.exit(1)
gh = Github(os.environ['GITHUB_TOKEN'])
repo = gh.get_repo(sys.argv[1])
print(f'Modifying repo secrets for {sys.argv[1]}')
for filename in sys.argv[2:]:
values = dotenv_values(filename)
for key,value in values.items():
print(f' -> {key}')
repo.create_secret(key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment