如何在每次提交仓库代码时自动执行index.py更新README.md文件
实现提交时自动更新README的方案
你可以直接使用GitHub Actions实现该需求,无需依赖外部服务器,操作步骤如下:
1. 新建工作流配置文件
在仓库根目录创建.github/workflows文件夹,在该文件夹下新建update-readme.yml文件,填入以下配置:
name: Update README Books List on: push: branches: [ main ] # 此处替换为你的实际主分支名,如master jobs: update-readme: runs-on: ubuntu-latest permissions: contents: write steps: - name: 拉取仓库代码 uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} - name: 配置Python环境 uses: actions/setup-python@v5 with: python-version: '3.10' # 替换为你的index.py适配的Python版本 - name: 执行index.py生成新README # 若你的环境默认用python3启动,可改为python3 index.py run: python index.py - name: 提交并推送README变更 run: | git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" git add readme.md git diff --quiet && git diff --staged --quiet || (git commit -m "auto: 更新README书籍列表" && git push)
2. 配置仓库权限
进入仓库的Settings > Actions > General页面,拉到Workflow permissions板块,勾选Read and write permissions后保存,确保工作流有提交代码的权限。
可选适配调整
- 若你的
index.py依赖第三方Python库,在执行脚本的步骤前新增依赖安装步骤:- name: 安装Python依赖 run: pip install 你的依赖包名称 - 若需要在其他分支的推送事件中触发该流程,修改
on.push.branches的配置项即可 - 提交步骤内置了空提交校验,只有
readme.md实际发生变更时才会执行推送,不会产生无效提交记录
内容的提问来源于stack exchange,提问作者Inazense
相关产品推荐
相关产品推荐

