How to trigger a GitHub action only when the response of a script is different and it changes a particular file

Use case: I’m having a python script to fetch a ICS (iCalendar) feed, which parses the ICS into a YAML file.

How can I create a GitHub action, which will schedule the script to run every day and check whether after it’s execution, the YAML files changes or not.

If the YAML file changes, then only it might send a PR

You can use a scheduled event to trigger the workflow. As for detecting changes I’d suggest just overwriting the YAML file and then using git diff --exit-code to detect if it actually changed.

1 Like

Hey, thanks for your reply!

Do you mean something like this with a scheduled event:

       # Runs the Script to convert ics to yaml format

      - name: Run ics-to-yml.py
        run: python assets/script/ics-to-yml.py

      - name: Check git diff
        if: git diff --exit-code == 1
        # Creates Pull Request
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.PAT }}
          branch-suffix: timestamp
          path: ./master
          title: Update upcoming events
          labels: automated-build
          commit-message: Update upcoming events

Is there any other way to do this?

Close, but you can’t use a shell command directly in the if condition. You’ll have to use an extra step to set an output or environment variable, e.g.:

- name: Check diff
  run: |
    if git diff --exit-code; then
        echo "CHANGED=false" >>${GITHUB_ENV}
    else
        echo "CHANGED=true" >>${GITHUB_ENV}
    fi

- name: Create pull request
  if: env.CHANGED == 'true'
  # ...
1 Like

I really appreciate for your help, thank youu!

1 Like

Hey, I’m not sure why it’s saying it’s not a git repository. Thus all the jobs run successfully, but no changes occur

Did you use actions/checkout before the other things? If no, you definitely need to do that.

If yes check its logs. By default it creates a Git repository, but if the installed Git version is too old (that might happen with a job container or self-hosted runner) it might just download the files over the API.

Yes, I used actions/checkout before these.

I think in this, the git is initialized:

Looks like it is, but the working directory looks odd with the /master part at the end. Is that the same directory the file gets written to and that git diff is running in?

1 Like