Github action check if a file already exists

Viewed 2822

I have the following git action which allows me to download an image.

I have to make sure if the file already exists to skip the "Commit file" and the "Push changes"

How can I check if the file already exists if it already exists nothing is done.

on: 
  workflow_dispatch:
  
name: Scrape File
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Check out current commit
     
      - name: Url
        run: |
         URL=$(node ./action.js)
         echo $URL
         echo "URL=$URL" >> $GITHUB_ENV
      
      - uses: suisei-cn/actions-download-file@v1
        id: downloadfile
        name: Download the file
        with:
          url: ${{ env.URL }}
          target: assets/
      - run: ls -l 'assets/'
          
      - name: Commit files
        run: |
         git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
         git config --local user.name "github-actions[bot]"
         git add .
         git commit -m "Add changes" -a
         
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
         github_token: ${{ secrets.GITHUB_TOKEN }}
         branch: ${{ github.ref }}
1 Answers

There are a few options here - you can go directly with bash and do something like this:

if test -f "$FILE"; then
    #file exists
fi

or use one of the existing actions like this:

- name: Check file existence
  id: check_files
  uses: andstor/file-existence-action@v1
  with:
    files: "assets/${{ env.URL }}"

- name: File exists
  if: steps.check_files.outputs.files_exists == 'true'
  run: echo "It exists !"
Related