1

I'm trying to create a GitHub workflow that runs a python script (which outputs three graphs), add those graphs to the readme.md then commit the changes to the repo and display the graphs on the readme page. I would like to trigger a new push happening.

as a bash script it would look like this:

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

I'm not really sure where to start on it or how to set up the action. I tried setting up one but it wouldn't make any changes.

nima
  • 7,796
  • 12
  • 36
  • 53
  • Can you add some details about what you've tried so far. I think the tool you're looking for is a "commit hook," but some more details about your attempts thus far would help. – Chris Hanson Feb 24 '20 at 22:33
  • Yeah, so far what I've been trying to do is do it using a bash script, where it goes through a timed cycle and then repeats. The repository contains the python script and other data that is updated on a near daily basis and the script generates graphs based on the data that we want to keep up to date all the time. The problem with that is it would have to be hosted somewhere. My boss did say that there is a way to do it all in github but we both don't know how to – Omar Kaheel Feb 25 '20 at 22:16
  • Yes, Github can do it. I just did a vaguely similar thing. On push, I change a setting in a Jupyter notebook, and add some cells at the end. See [here](https://github.com/fomightez/bendit-binder/blob/master/.github/workflows/make_lw_settings.yml) but fair warning that looks complex because I am doing the changes in a different branch. See [here](https://github.com/fomightez/bendit-binder/tree/80bd7d5bd3cf9daaa092becb886ed93e8eab7b23/.github/workflows) for when I was simply copying the notebook and changing it, but that was before I realized I didn't need a dockerfile to run a Python script. – Wayne Feb 26 '20 at 19:45

1 Answers1

2

See this answer for how to commit back to your repository during a workflow.

In your case it might look something like this. Tweak it where necessary.

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

Alternatively, raise a pull request instead of committing immediately using create-pull-request action.

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
peterevans
  • 34,297
  • 7
  • 84
  • 83