Retention policies are the rules you define to manage the lifecycle of your stored artifacts. Think of them as intelligent housekeepers for your artifact repository. Their primary goal is to specify how long artifacts should be kept, and which ones are important enough to retain. This isn't just about tidiness; it's crucial for controlling storage costs, improving repository performance (fewer items to index), meeting compliance requirements (e.g., "keep all release binaries for 5 years"), and ensuring your developers don't drown in outdated build snapshots. Common policy examples include "keep the last N successful builds," "delete artifacts older than X days," or "retain all artifacts associated with a production release tag indefinitely."
While retention policies define what to keep or discard, garbage collection (GC) is the active process that enforces these rules. It's the execution phase where your repository scans its contents, identifies artifacts that violate your established policies (e.g., an untagged build artifact now older than its specified retention period), and then physically deletes them. Garbage collection typically runs on a scheduled basis rather than in real-time, allowing it to efficiently clean up large volumes of data without impacting immediate repository operations. It's important to differentiate: policies are the strategy, GC is the tactical operation.
Implementing effective retention policies and understanding garbage collection is a balancing act. You need enough historical data for debugging, rollbacks, and auditing, but not so much that it inflates storage costs or slows down your systems. A best practice is to tier your policies: for example, keep release-tagged artifacts much longer (or indefinitely) than development or snapshot builds. Always consider legal and compliance mandates for how long specific artifact types must be stored. Finally, actively monitor your garbage collection jobs; failures can lead to unexpected storage bloat, while overly aggressive policies might delete critical artifacts.
Key Takeaways
- Retention policies define the rules for artifact storage duration and importance.
- Garbage collection is the automated process that enforces these defined retention policies.
- They are essential for managing storage costs, improving repository performance, and ensuring compliance.
- Tailor different policies for different artifact types (e.g., indefinite for releases, short for dev builds).
- Regularly monitor garbage collection activity to prevent storage issues or accidental data loss.
Code Example
# Example .gitlab-ci.yml snippet for job artifact retention
build_job:
stage: build
script:
- echo "Building application..."
- mkdir -p build_output
- echo "Build artifact content" > build_output/app.zip
artifacts:
paths:
- build_output/
expire_in: 1 week # Policy: delete build job artifacts after 1 week
deploy_to_prod_job:
stage: deploy
script:
- echo "Deploying to production..."
- echo "Deployment successful." > deploy_report.log
artifacts:
paths:
- deploy_report.log
expire_in: never # Policy: keep deploy reports indefinitely (for auditing)How this code works
This GitLab CI/CD configuration snippet illustrates how to define specific retention policies for job artifacts, which are files generated by jobs and stored by GitLab. This is vital for managing storage space (a form of garbage collection) while ensuring critical artifacts are available for as long as needed. The code shows two distinct jobs, each producing artifacts with different lifespans according to their importance.
The build_job creates an artifact from the build_output/ directory. For these artifacts, the artifacts section includes expire_in: 1 week, meaning GitLab will automatically delete them after seven days, freeing up server space. In contrast, the deploy_to_prod_job generates a deploy_report.log artifact, and its expire_in: never policy dictates that this report should be kept indefinitely, serving as a permanent record for auditing purposes. A subtle but important detail is that if expire_in isn't explicitly set, GitLab CI will default to a project-level setting or a general default (often 30 days), which could lead to unexpected artifact deletion if not considered.