GitHub workflow for automating Python script with secrets not working

importhuman

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.

importhuman

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 }}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Keyword secrets is not working in github actions workflow

Key Vault secrets in GitHub Actions workflow

How to use GitHub action secrets in a reusable workflow?

How do I include GitHub secrets in a python script?

Automating python script on a mac not running

How to reference GitHub Secrets in GitHub Actions workflow for .Renviron file

Executing workflow commands for GitHub Actions using a Python script

GitHub Actions workflow path filter not working

GitHub Actions workflow has stopped working

Why latest commit date is not working in Github workflow?

How to run a Ruby script on a Github Workflow

How to access GitHub action secrets with python?

Is it safe to pass GitHub Secrets as an argument to a python code?

How can I use secrets from a GitHub environment to pass it to a reusable workflow?

Is there a way to pass secrets under a different key in main.workflow in Github Actions?

Accessing Openshift Secrets Data in Python Script

trying to create script that scan for secrets in python code

Automating SQL script

Automating Git release workflow with CI job

GitHub action workflow schedule not working on non-default branch

Unable to access environment variables into a bash shell script in GitHub Workflow

How run a script with variable in the name under PowerShell in GitHub workflow / actions

Where are github secrets stored?

Automating Hive with python

Automating Winmerge comparison in Python

Automating login with Selenium (Python)

Automating vncserver with expect not working as expected

Automating a rename files bash script

Automating a script on a cell value change