Learn how to share artifacts between jobs in GitHub CI/CD workflows
In GitHub Actions, artifacts allow files or directories to be shared between jobs in a workflow. This can be useful for passing data, build outputs, test results, or deployment artifacts across different steps or jobs.
1. Creating and Uploading Artifacts
Upload-artifact Action
The actions/upload-artifact action allows files to be uploaded as artifacts. These artifacts can then be downloaded by subsequent jobs in the workflow.
Example: Uploading Artifacts
xxxxxxxxxx241jobs2 build3 runs-onubuntu-latest4 steps5nameCheckout Repository6 usesactions/checkout@v37nameBuild Artifacts8 run./build.sh9 usesactions/upload-artifact@v310 with11 namebuild-artifacts12 pathbuild/13 deploy14 needsbuild15 runs-onubuntu-latest16 steps17nameCheckout Repository18 usesactions/checkout@v319nameDownload Artifacts20 usesactions/download-artifact@v321 with22 namebuild-artifacts23nameDeploy24 run./deploy.sh --build ./build/2. Downloading Artifacts
Download-artifact Action
The actions/download-artifact action retrieves previously uploaded artifacts and makes them available for use in subsequent jobs.
Example: Downloading Artifacts
xxxxxxxxxx241jobs2 build3 runs-onubuntu-latest4 steps5nameCheckout Repository6 usesactions/checkout@v37nameBuild Artifacts8 run./build.sh9 usesactions/upload-artifact@v310 with11 namebuild-artifacts12 pathbuild/13 deploy14 needsbuild15 runs-onubuntu-latest16 steps17nameCheckout Repository18 usesactions/checkout@v319nameDownload Artifacts20 usesactions/download-artifact@v321 with22 namebuild-artifacts23nameDeploy24 run./deploy.sh --build ./build/3. Artifact Retention
GitHub provides configurable artifact retention to manage how long artifacts are stored after a workflow completes.
Setting Artifact Retention
You can specify how long artifacts should be retained:
xxxxxxxxxx291jobs2 deploy3 needsbuild4 runs-onubuntu-latest5 outputs6 build-artifact$ steps.download-artifact.outputs.artifact_path 7 steps8nameDownload Artifacts9 usesactions/download-artifact@v310 with11 namebuild-artifacts12nameDeploy13 run./deploy.sh --build $ steps.download-artifact.outputs.artifact_path 14 artifact15 namebuild-artifacts16 paths17build/18 outputs19 artifact_path$ steps.upload-artifact.outputs.artifact_path 20 cleanup21 ifalways()22 runs-onubuntu-latest23 needsdeploy24 steps25nameClean Up Artifacts26 usesactions/artifact-cleanup@v327 with28 namebuild-artifacts29 retention-days7In this example:
Artifacts are retained for 7 days before being deleted automatically.
4. Deleting Artifacts
You can manually delete artifacts via actions using the actions/artifact-cleanup action or automatically after a specified retention period.
Deleting Artifacts
xxxxxxxxxx91jobs2 cleanup3 runs-onubuntu-latest4 steps5nameClean Up Old Artifacts6 usesactions/artifact-cleanup@v37 with8 namebuild-artifacts9 retention-days30This deletes all artifacts older than 30 days.
5. Using Permissions
Upload Permissions: Grant necessary permissions for uploading artifacts:
xxxxxxxxxx21permissions2artifactswriteDownload Permissions: Ensure artifacts can be downloaded:
xxxxxxxxxx21permissions2artifactsread
6. Artifact Limits and Storage
GitHub imposes limits on artifact sizes and total storage:
Maximum artifact size: 100 GB per workflow run.
Total storage limit: 2 GB per user (across all repositories).
Best Practices for Using Artifacts
Granular Artifacts: Create multiple smaller artifacts for different types of outputs (e.g., build artifacts, test reports).
Limit Retention: Set a reasonable retention period for artifacts to manage storage costs and reduce the lifecycle of unused artifacts.
Secure Access: Ensure sensitive data is stored securely, especially when handling API keys or other sensitive information.
Error Handling: Implement error handling in workflows if artifact operations fail (e.g., downloads, deletions).
Matrix Strategy: Use artifacts effectively in matrix testing to share results between environments (e.g., different OS, Node.js versions).






















Leave a Reply