Bright Labs

How to check that a downloaded file exists: bash, PowerShell and GitHub Actions

Your script downloads a file, and the next line needs that file to be there. In bash, check it with test -f, and add test -s if an empty file should count as missing. In PowerShell, use Test-Path with -PathType Leaf. In a GitHub Actions workflow, run the same check in a step, or gate a step on hashFiles() returning something other than an empty string.

The check itself is short. In CI, check the path first: the file may have landed somewhere other than where you're looking. Here's each check, then how to find out where actions/download-artifact actually put your file.

Check for the file in bash: test -f, -e and -s

The shell's test command does this. Under POSIX, -f is true if the path resolves to an existing directory entry for a regular file. -e is looser: it's true for any existing directory entry, and that includes a directory. So if the path turns out to be a folder, -e passes and -f doesn't.

Two more are worth knowing. -d is true if the path resolves to an existing directory entry for a directory. -s is true if the path resolves to an existing directory entry for a file that has a size greater than zero. That last one catches a download that left an empty file behind.

test reports its answer through its exit status. Zero means the expression evaluated as true, and 1 means it evaluated as false.

Example you write: stop the script if the file is missing or empty
file="downloads/report.zip"
if ! test -f "$file"; then
  echo "Missing: $file"
  exit 1
fi
if ! test -s "$file"; then
  echo "Empty: $file"
  exit 1
fi

Put the real path in the message. When this fails in CI, the log line names the file that was missing.

Check for the file in PowerShell: Test-Path

In PowerShell, Test-Path is the check. Microsoft's reference says it determines whether all elements of the path exist. To check for a file specifically, add -PathType Leaf. The reference describes a Leaf as an element that doesn't contain other elements, such as a file.

From Microsoft's Test-Path reference: check for a file
Test-Path -Path $PROFILE -PathType Leaf
Example you write: the same check on your downloaded file
if (-not (Test-Path -Path "downloads\report.zip" -PathType Leaf)) {
  Write-Error "Missing: downloads\report.zip"
  exit 1
}

Find where actions/download-artifact put your file

If the check fails right after a download step, check the path before you blame the download. The path input of actions/download-artifact is the destination path, and its README says the default is $GITHUB_WORKSPACE.

The catch is the folder layout. It changes depending on whether you pass a name:

The README's own examples list what landed with ls -R. Add that step once while debugging, read the tree in the log, and then fix the path in your check.

Example you write: list what landed in your download path
- name: Display structure of downloaded files
  run: ls -R your/destination/dir

Gate a GitHub Actions step on the file with hashFiles

Sometimes you don't want a failure. You just want to skip a step when the file isn't there. GitHub's expressions reference says hashFiles returns a single hash for the set of files that match the path pattern. If the pattern does not match any files, it returns an empty string. So compare it with an empty string in the step's if.

Example you write: run the step only when the file exists
- name: Publish the report
  if: hashFiles('downloads/report.zip') != ''
  run: ./publish.sh downloads/report.zip

Know its limits before you rely on it:

When the file is there on one run and missing on the next

If the same check passes on one run and fails on the next with no code change, a green re-run doesn't tell you why it was red. Before you re-run, look at how that job has behaved on the same commit and on main.

One option for that is FlakeRadar, our free Chrome extension for GitHub pull requests. It reads the repository's own GitHub Actions history and labels each failing job as Known flake, New failure, Also failing on the default branch, or Not enough history. A job counts as flaky when it failed and then passed on the same commit. The free plan covers public repositories with 30 days of history, plus one-click re-run if you turn it on. A Known flake label is evidence, not a promise, so if the missing file matters, read the log.

Sources

Questions

Should I use test -e or test -f for a downloaded file?

Use -f. Under POSIX, -e is true for any existing directory entry, including a directory, and -f is true only for a regular file.

Can a downloaded file exist and still be empty?

Yes. test -f passes on an empty regular file. Add test -s, which is true only for a file that has a size greater than zero.

Why does hashFiles return an empty string when the file did download?

hashFiles returns an empty string when the pattern matches no files, and it only matches files inside GITHUB_WORKSPACE, with paths relative to it. Check that the file isn't outside the workspace, or one folder deeper under the artifact's name because you downloaded all artifacts with no name.

Is a download step that fails only sometimes safe to re-run?

A green re-run doesn't tell you why the file was missing. Read the log for the missing path before you merge.

Related