参考:
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 randomdef 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 Newson:schedule:- cron: '*/5 * * * *' # 每5分钟运行一次jobs:fetch-news:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v2with:token: ${{ secrets.PAT }} # 使用 PAT 进行 checkout- name: Set up Pythonuses: actions/setup-python@v2with:python-version: '3.8'- name: Install dependenciesrun: |python -m pip install --upgrade pippip install requests- name: Fetch newsrun: |python fetch_news.py- name: Commit and push newsrun: |git config --global user.name "github-actions[bot]"git config --global user.email "github-actions[bot]@users.noreply.github.com"git add news.txtgit 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通用设置: