GitHub workflow for automating Python script with secrets not working

788 views Asked by At

Following is my Python script that adds links from liked tweets to Pocket:

from dotenv import load_dotenv
load_dotenv()
import os
import re
import tweepy
from pocket import Pocket

#Twitter keys
consumer_key = os.environ.get('API_key')
consumer_secret = os.environ.get('API_secretkey')

#Pocket keys
p_consumer_key = os.environ.get('Pocket_consumer_key')
p_access_token = os.environ.get('Pocket_access_token')

#authenticate and call twitter api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())

p = Pocket(consumer_key=p_consumer_key, access_token=p_access_token)

#gets JSON of liked tweets 
fav = api.favorites('importhuman', count=100, tweet_mode='extended')

links = []
for status in fav:
    url_list = status['entities']['urls']
    if url_list != []:
        for item in url_list:
            link = item['expanded_url']
            if link not in links:
                if re.search("//twitter.com/", link) is None:
                    links.append(link)
                    p.add(link)

I'm trying to automate this script to run every 5 minutes, for which I'm trying to design a GitHub workflow but it's not working so far.

name: run-five-minutes

on:
  schedule:
    - cron: '*/5 * * * *'

jobs:
  build: 
    runs-on: ubuntu-latest
    steps: 
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with: 
          python-version: '3.x'
      - run: pip install -r requirements.txt
        env: 
          API_key: ${{ secrets.API_KEY }}
          API_secretkey: ${{ secrets.API_SECRETKEY }}
          Pocket_consumer_key: ${{ secrets.POCKET_CONSUMER_KEY }}
          Pocket_access_token: ${{ secrets.POCKET_ACCESS_TOKEN }}
      - run: python3 app.py 

The requirements.txt file has python-dotenv, tweepy, and pocket. Any help would be appreciated.

1

There are 1 answers

0
importhuman On BEST ANSWER

I figured it out. The problem was that I was setting up the secrets in an environment before the script ran, so it did not have access to the secrets. I just switched the order, and it works.

So, instead of

- run: pip install -r requirements.txt
  env: 
      API_key: ${{ secrets.API_KEY }}
      API_secretkey: ${{ secrets.API_SECRETKEY }}
      Pocket_consumer_key: ${{ secrets.POCKET_CONSUMER_KEY }}
      Pocket_access_token: ${{ secrets.POCKET_ACCESS_TOKEN }}
- run: python3 app.py 

I did this:

- run: pip install -r requirements.txt
- run: python3 app.py 
  env: 
      API_key: ${{ secrets.API_KEY }}
      API_secretkey: ${{ secrets.API_SECRETKEY }}
      Pocket_consumer_key: ${{ secrets.POCKET_CONSUMER_KEY }}
      Pocket_access_token: ${{ secrets.POCKET_ACCESS_TOKEN }}