This worker script is designed to clean Docker repositories hosted in Artifactory based on configurable cleanup policies. It is useful for managing storage and maintaining repository hygiene.
- Cleans Docker repositories based on:
- Maximum age of images
- Maximum number of image versions
- Supports cleanup based on:
- Image creation date
- Last download date
- Provides optional dry-run mode for safe execution.
- Handles complex repositories with multiple Docker files.
- Logs processed repositories and cleanup details for transparency.
The worker accepts the following configuration parameters:
dockerRepos
: A list of Docker repositories to clean. Only repositories listed here will be processed.
-
byDownloadDate
(boolean, default:false
):-
false
: Images are cleaned based on their creation date. -
true
: Images are cleaned based on their last download date or, if unavailable, their last update date.
-
-
dryRun
(boolean, default:false
):-
true
: Simulates the cleanup process without deleting any files. -
false
: Executes the cleanup, deleting the identified files.
-
{
"dockerRepos": ["example-docker-local", "example-docker-local-2"],
"byDownloadDate": false,
"dryRun": true
}
Cleanup policies are defined using labels in the Docker image. The worker supports the following policies:
-
maxDays
: Specifies the maximum number of days an image can exist in the repository. Older images will be deleted.- When
byDownloadDate=true
: Images downloaded or updated within the lastmaxDays
will be preserved.
- When
-
maxCount
: Specifies the maximum number of image versions to retain. Excess versions will be deleted, starting with the oldest.- When
byDownloadDate=true
: Image age is determined first by the Last Downloaded Date and then by the Modification Date if the image has never been downloaded.
- When
Labels can be added to the Dockerfile before building the image. For example:
LABEL com.jfrog.artifactory.retention.maxCount="10"
LABEL com.jfrog.artifactory.retention.maxDays="7"
When deployed, these labels are automatically converted into properties in Artifactory. The worker reads these properties to determine the cleanup policy for each image.
Cleanup can be triggered using the JFrog CLI. For example:
jf worker exec my-worker - <<EOF
{
"dockerRepos": ["example-docker-local"],
"byDownloadDate": false,
"dryRun": true
}
EOF
Alternatively, execute with a payload file:
jf worker exec my-worker @payload.json
-
The worker has a maximum execution timeout of 5 seconds. If the cleanup process for a complex repository exceeds this limit:
-
Files that can be deleted within the timeout are processed.
-
A timeout error is returned:
{ "message": "Worker execution timeout" }
-
Remaining files are not processed.
-
-
Repositories with large numbers of images or complex cleanup requirements may require multiple executions to fully clean.
-
It is recommended to periodically monitor and trigger the worker for such repositories.
- The worker logs the received payload and the processed repositories for debugging purposes. Example:
Payload - {
"dockerRepos": ["example-docker-local"],
"byDownloadDate": false,
"dryRun": false
}
Repos - ["example-docker-local"]
- Errors and unprocessed files due to timeout are logged for transparency.
-
Configure cleanup policies using labels in the Dockerfile.
-
Deploy the images to Artifactory.
-
Define the worker payload:
{
"dockerRepos": ["example-docker-local"],
"byDownloadDate": true,
"dryRun": false
}
-
Trigger the worker using the JFrog CLI.
-
Review the logs for details about the cleanup process.
-
Timeout Management: For large or complex repositories, consider breaking cleanup into smaller tasks.
-
Dry Run: Always perform a dry run for initial testing to validate the cleanup logic.
-
Monitoring: Regularly monitor the storage usage and worker logs to ensure efficient repository management.