skip to Main Content

I am trying to move my bot written using discord.py/pycord to github for easier access, and I accidentally pushed my bot tokn to the hub, thankfully discord reset it for me and nothing hapened.

Now i want to use GitHub repository secrets to prevent this from happening, but i am having some trouble when trying to import the token into my code.

Here I’ve made a simple repo to experiment with this:

test.py:

import os
SECRET = os.environ['SECRET']
if SECRET == "TrustNo1":
    print("No one can be trusted")
print(SECRET)

workflow.yml:

name: build bot.py
on: [push]

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: load content
              uses: actions/checkout@v2
              
            - name: load python
              uses: actions/setup-python@v4
              with:
                python-version: '3.10' # install the python version needed

            - name: start bot
              env:
                TOKEN: ${{ secrets.SECRET }}
              run: python test.py

The following error occurs at the "start bot" step in the workflow:

Traceback (most recent call last):
  File "/home/runner/work/test-repo/test-repo/test.py", line 2, in <module>
    SECRET = os.environ['SECRET']
  File "/opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/os.py", line 680, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET'
Error: Process completed with exit code 1.

If i try to echo the SECRET in the workflow.yml i get ***, so it has has acces to the token, but when it imports to python it all breaks down.

I’m still quite new to git and GitHub, so please don’t use advanced terms.

2

Answers


  1. Chosen as BEST ANSWER

    The problem was that in the workflow.yml, the variable was named "TOKEN", and in the test.py it was called "SECRET"


  2.             - name: start bot
                  env:
                    TOKEN: ${{ secrets.SECRET }}
    

    In the GitHub Action you named the variable secrets.SECRET but in environment variables you named it TOKEN. Either change the name of the environment variable to SECRET:

                - name: start bot
                  env:
                    SECRET: ${{ secrets.SECRET }}
    

    or change your code:

        SECRET = os.environ['TOKEN']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search