Repository navigation
Open
Labels
enhancementNew feature or requestNew feature or request
Description
What would you like to be added?
Purpose:
Checks if a GitHub Actions artifact exists in the current workflow run.
Used to conditionally download artifacts and avoid errors when artifacts
are missing or haven't been created yet.
Key Features:
- Validates artifact is not expired and has non-zero size
- Returns false if artifact name is empty (disables artifact checking)
- Handles API errors gracefully without failing the workflow
# Query GitHub REST API to check if an artifact with the given name exists
# This only works for artifacts uploaded with actions/upload-artifact@v4
- name: artifact exists
uses: actions/github-script@v8
id: artifact-exists
with:
script: |
const artifactName = "${{inputs.artifact_name}}";
// If artifact name is empty, skip the check (allows disabling artifact lookup)
if (!artifactName) {
console.log("Artifact name is empty. Not looking for any github artifact.");
core.setOutput("exists", false);
return;
}
try {
// Call GitHub REST API to list all artifacts from this workflow run
const response = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId
});
console.log(response.data)
const artifacts = response.data.artifacts;
// Search for artifact matching the name, and verify it's valid:
// - Not expired (still accessible)
// - Has content (size > 0 bytes)
const artifactExists = artifacts.find(a =>
a.name === artifactName &&
!a.expired &&
a.size_in_bytes > 0
);
// Output boolean indicating whether a valid artifact was found
core.setOutput("exists", Boolean(artifactExists));
} catch (e) {
// Handle errors gracefully - don't fail the workflow
console.error("Error listing artifacts", e);
core.setOutput("exists", false);
}
Why is this needed?
To prevent failure if the artifact is missing, allow customize the behaviur easily if the artifact is mising
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Activity