GitHub 用于自动化 Python 脚本的工作流无法正常工作
GitHub workflow for automating Python script with secrets not working
以下是我的 Python 脚本,用于将喜欢的推文的链接添加到 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)
我正在尝试将此脚本自动化为每 5 分钟 运行,为此我正在尝试设计一个 GitHub 工作流程,但它目前无法正常工作。
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
requirements.txt
文件有 python-dotenv、tweepy 和 pocket。任何帮助将不胜感激。
我明白了。问题是我在脚本 运行 之前的环境中设置了机密,因此它无法访问机密。我刚换了顺序,就可以了。
所以,而不是
- 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
我这样做了:
- 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 }}
以下是我的 Python 脚本,用于将喜欢的推文的链接添加到 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)
我正在尝试将此脚本自动化为每 5 分钟 运行,为此我正在尝试设计一个 GitHub 工作流程,但它目前无法正常工作。
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
requirements.txt
文件有 python-dotenv、tweepy 和 pocket。任何帮助将不胜感激。
我明白了。问题是我在脚本 运行 之前的环境中设置了机密,因此它无法访问机密。我刚换了顺序,就可以了。
所以,而不是
- 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
我这样做了:
- 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 }}