|
| 1 | +# Scaleway Container Registry Tag Cleaner |
| 2 | + |
| 3 | +This project aims to clean up Scaleway Container Registry tags to keep only the N latest tags for each image, which is useful for managing disk space and organizing the registry. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Scaleway Account |
| 8 | +- Docker daemon running to build the image |
| 9 | +- Container registry namespace created, for this example we assume that your namespace name is `registry-cleaner`: [doc here](https://www.scaleway.com/en/docs/containers/container-registry/how-to/create-namespace/) |
| 10 | +- API keys generated, Access Key and Secret Key [doc here](https://www.scaleway.com/en/docs/iam/how-to/create-api-keys/) |
| 11 | + |
| 12 | +## Step 1: Build and Push to Container Registry |
| 13 | + |
| 14 | +Serverless Jobs, like Serverless Containers (which are suited for HTTP applications), works |
| 15 | +with containers. So first, use your terminal reach this folder and run the following commands: |
| 16 | + |
| 17 | +```shell |
| 18 | +# First, log in to the container registry. You can find your login details in the Scaleway console. |
| 19 | +docker login rg.fr-par.scw.cloud/registry-cleaner -u nologin --password-stdin <<< "$SCW_SECRET_KEY" |
| 20 | + |
| 21 | +# Build the image to push. |
| 22 | +docker build -t rg.fr-par.scw.cloud/registry-cleaner/versions-retention:v1 . |
| 23 | + |
| 24 | +## TIP: For Apple Silicon or other ARM processors, use the following command as Serverless Jobs supports the amd64 architecture. |
| 25 | +# docker buildx build --platform linux/amd64 -t rg.fr-par.scw.cloud/registry-cleaner/versions-retention:v1 . |
| 26 | + |
| 27 | +# Push the image online to be used on Serverless Jobs. |
| 28 | +docker push rg.fr-par.scw.cloud/registry-cleaner/versions-retention:v1 |
| 29 | +``` |
| 30 | + |
| 31 | +> [!TIP] |
| 32 | +> As we do not expose a web server and we do not require features such as auto-scaling, Serverless Jobs are perfect for this use case. |
| 33 | +
|
| 34 | +To check if everyting is ok, on the Scaleway Console you can verify if your tag is present in Container Registry. |
| 35 | + |
| 36 | +## Step 2: Creating the Job Definition |
| 37 | + |
| 38 | +On Scaleway Console on the following link you can create a new Job Definition: https://console.scaleway.com/serverless-jobs/jobs/create?region=fr-par |
| 39 | + |
| 40 | +1. On Container image, select the image you created in the step before. |
| 41 | +2. You can set the image name to something clear like `registry-version-retention` too. |
| 42 | +3. For the region you can select the one you prefer :) |
| 43 | +4. Regarding the resources you can keep the default values, this job is fast and do not require specific compute power or memory. |
| 44 | +5. To schedule your job for example every night at 2am, you can set the cron to `0 2 * * *`. |
| 45 | +6. Important: advanced option, you need to set the following environment variables: |
| 46 | + |
| 47 | +> [!TIP] |
| 48 | +> For sensitive data like `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` we recommend to inject them via Secret Manager, [more info here](https://www.scaleway.com/en/docs/serverless/jobs/how-to/reference-secret-in-job/). |
| 49 | +
|
| 50 | +- **Environment Variables**: Set the required environment variables: |
| 51 | + - `SCW_DEFAULT_ORGANIZATION_ID`: Your Scaleway organization ID. |
| 52 | + - `SCW_ACCESS_KEY`: Your Scaleway API access key. |
| 53 | + - `SCW_SECRET_KEY`: Your Scaleway API secret key. |
| 54 | + - `SCW_PROJECT_ID`: Your Scaleway project ID. |
| 55 | + - `SCW_NUMBER_VERSIONS_TO_KEEP`: The number of latest tags to keep for each image. |
| 56 | + - `SCW_NO_DRY_RUN`: Set to `true` to delete namespaces; otherwise, it will perform a dry run. |
| 57 | + |
| 58 | +* Then click "Create Job" |
| 59 | + |
| 60 | +## Step 3: Run the job |
| 61 | + |
| 62 | +On your created Job Definition, just click the button "Run Job" and within seconds it should be successful. |
| 63 | + |
| 64 | +## Troubleshooting |
| 65 | + |
| 66 | +If your Job Run state goes in error, you can use the "Logs" tab in Scaleway Console to get more informations about the error. |
| 67 | + |
| 68 | +# Additional content |
| 69 | + |
| 70 | +- [Jobs Documentation](https://www.scaleway.com/en/docs/serverless/jobs/how-to/create-job-from-scaleway-registry/) |
| 71 | +- [Other methods to deploy Jobs](https://www.scaleway.com/en/docs/serverless/jobs/reference-content/deploy-job/) |
| 72 | +- [Secret key / access key doc](https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/) |
| 73 | +- [CRON schedule help](https://www.scaleway.com/en/docs/serverless/jobs/reference-content/cron-schedules/) |
0 commit comments