首页 > 解决方案 > 使用 github 操作将 python 生成的图提交到 repo

问题描述

我正在尝试创建一个运行 python 脚本(输出三个图表)的 GitHub 工作流,将这些图表添加到 readme.md 然后将更改提交到 repo 并在自述文件页面上显示图表。我想触发新的推动。

作为 bash 脚本,它看起来像这样:

git pull
python analysis_1.py
git add .
git commit -m "triggered on action"
git push

我不确定从哪里开始或如何设置操作。我尝试设置一个,但它不会做出任何改变。

标签: pythongitgithubgithub-actions

解决方案


有关如何在工作流程期间提交回存储库的信息,请参阅此答案。

在您的情况下,它可能看起来像这样。在必要时对其进行调整。

on:
  push:
    branches:
      - master
jobs:
  updateGraphs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-python@v1
        with:
          python-version: '3.x'

      - name: Generate graphs
        run: python analysis_1.py

      - name: Update graphs
        run: |
          git config --global user.name 'Your Name'
          git config --global user.email 'your-username@users.noreply.github.com'
          git commit -am "Update graphs"
          git push

或者,使用create-pull-request操作提出拉取请求,而不是立即提交。

on:
  push:
    branches:
      - master
jobs:
  updateGraphs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-python@v1
        with:
          python-version: '3.x'

      - name: Generate graphs
        run: python analysis_1.py

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Update graphs
          title: Update graphs
          branch: update-graphs

推荐阅读