john
john

Reputation: 1036

Commit only when file exists

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 "[email protected]"
    git add *
    git commit -am "GH ACTION Headlines $(date)"
    git push origin main -f
  env:
    REPO_KEY: ${{secrets.GITHUB_TOKEN}}
    username: github-actions

Upvotes: 1

Views: 2384

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 23270

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@v2
   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 "[email protected]"
    git add *
    git commit -am "GH ACTION Headlines $(date)"
    git push origin main -f
  env:
    REPO_KEY: ${{secrets.GITHUB_TOKEN}}
    username: github-actions

Note: But sure to give the job the permissions: write-all otherwise you would need to use a PAT instead of the GITHUB_TOKEN to push the code.

Upvotes: 2

Related Questions