May 2, 2022

Python and GitHub Secrets

This is a simple note to show how to access GitHub Secrets with Python.

First, to add a new secret, go to GitHub repository > Settings > Secrets > New Repository Secret.

Second, define a Name ('SEC_NAME') and put in the Value.

 

Next, map them as environment variables in GitHub Actions Workflow.

....

    - name: Run tests

        env:

            API_KEY: ${{ secrets.SEC_NAME }}

        run:  |

....

 

Finally, refer to the env variable in Python script.

import os

API_KEY = os.environ['API_KEY']

....