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.
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
fiPut 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.
Test-Path -Path $PROFILE -PathType Leafif (-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:
- With a name (or an ID), the README says a single artifact will always be extracted directly to the specified path. Your file sits right in path.
- Without a name, all artifacts for the run are downloaded, and by default the action creates a directory named after each artifact. Your file is one folder deeper, under the artifact's name.
- The step's download-path output is the absolute path where the artifacts were downloaded. Print it, or check against it, and you're not guessing.
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.
- name: Display structure of downloaded files
run: ls -R your/destination/dirGate 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.
- name: Publish the report
if: hashFiles('downloads/report.zip') != ''
run: ./publish.sh downloads/report.zipKnow its limits before you rely on it:
- The path is relative to GITHUB_WORKSPACE and can only include files inside the workspace. A file you downloaded to a temp folder outside it never matches.
- Matching follows glob patterns and is case-insensitive on Windows, so on a Windows runner the pattern report.zip also matches Report.zip.
- If you'd rather see a missing file as an error, not a skip, use the shell check from earlier instead.
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
- POSIX test utility specification
- Microsoft Test-Path cmdlet reference
- actions/download-artifact usage and outputs
- GitHub Actions expressions and hashFiles reference
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.