参考:
https://developer.aliyun.com/article/1540773
https://github.com/ViggoZ/producthunt-daily-hot/blob/main/.github/workflows/generate_markdown.yml
1、创建github项目
目录:
.github/workflows/fetch-news.yml actions执行yaml(必须这样的目录结构)
fetch-news.py 是python脚本
fetch-news.py
import random
def generate_random_number():
random_number = random.randint(1, 100) # 生成1到100之间的随机数
with open("news.txt", "w") as file:
file.write(f"Random Number: {random_number}\n")
if __name__ == "__main__":
generate_random_number()
2、构建actions ymal文档
.github/workflows/fetch-news.yml
cron 程序执行周期
token 是秘钥
git push https://
s
e
c
r
e
t
s
.
P
A
T
@
g
i
t
h
u
b
.
c
o
m
/
{{ secrets.PAT }}@github.com/
secrets.PAT@github.com/{{ github.repository }}.git HEAD:${{ github.ref }} 内容在同步到github上
name: Fetch Latest News
on:
schedule:
- cron: '*/5 * * * *' # 每5分钟运行一次
jobs:
fetch-news:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
token: ${{ secrets.PAT }} # 使用 PAT 进行 checkout
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Fetch news
run: |
python fetch_news.py
- name: Commit and push news
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add news.txt
git commit -m "Update news"
git push https://${{ secrets.PAT }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }}
env:
PAT: ${{ secrets.PAT }}
3、设置各种权限、token
1)首先github上设置开发者秘钥
https://github.com/settings/tokens?type=beta
2)本项目设置
token设置:
可以设置为PAT,value是前面github设置的token值
actions通用设置: