Skip to content

[feat req] Artifact exists sub action #459

Open

Description

@Jiri-Stary

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions