Commit only when file exists

Viewed 788

I am using the code below in YML file to commit and push "data.xlsx" file within scheduled github actions. There are sometimes this file ("data.xlsx") doesn't get created so commit returns errors - "Your branch is up to date with 'origin/main'. nothing to commit, working tree clean. Error: Process completed with exit code 1" Is there any way to not to run this section when file does not exist

- name: Commit files
  run: |
    git config --local user.name actions-user
    git config --local user.email "actions@github.com"
    git add *
    git commit -am "GH ACTION Headlines $(date)"
    git push origin main -f
  env:
    REPO_KEY: ${{secrets.GITHUB_TOKEN}}
    username: github-actions
1 Answers

You could use this file-existence action that returns a boolean output if the file exists or not.

Then add an if condition to your Commit files step depending on this boolean output.

It would look like this:

 - name: "Check file existence"
   id: check_files
   uses: andstor/file-existence-action@v1
   with:
     files: "data.xlsx"
- name: Commit files
  if: steps.check_files.outputs.files_exists == 'true'
  run: |
    git config --local user.name actions-user
    git config --local user.email "actions@github.com"
    git add *
    git commit -am "GH ACTION Headlines $(date)"
    git push origin main -f
  env:
    REPO_KEY: ${{secrets.GITHUB_TOKEN}}
    username: github-actions
Related