|
2 | 2 |
|
3 | 3 | This project aims to clean up Scaleway Container Registry tags to keep only the N latest tags for each image. It is useful for managing disk space and keeping the registry organized. |
4 | 4 |
|
5 | | -## Usage |
| 5 | +## Requirements |
6 | 6 |
|
7 | | -1. **Build the Application:** |
8 | | - ```bash |
9 | | - go build -o reg-clean |
10 | | - ``` |
| 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 | 11 |
|
12 | | -2. **Run the Application:** |
13 | | - ```bash |
14 | | - ./reg-clean |
15 | | - ``` |
| 12 | +## Step 1 : Build and push to Container registry |
16 | 13 |
|
17 | | -## Environment Variables |
| 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: |
18 | 16 |
|
19 | | -The application requires the following environment variables to be set: |
| 17 | +```shell |
| 18 | +# First command is to login to container registry, you can find it in Scaleway console |
| 19 | +docker login rg.fr-par.scw.cloud/registry-cleaner -u nologin --password-stdin <<< "$SCW_SECRET_KEY" |
20 | 20 |
|
21 | | -- `SCW_DEFAULT_ORGANIZATION_ID`: Your Scaleway organization ID. |
22 | | -- `SCW_ACCESS_KEY`: Your Scaleway access key. |
23 | | -- `SCW_SECRET_KEY`: Your Scaleway secret key. |
24 | | -- `SCW_PROJECT_ID`: Your Scaleway project ID. |
25 | | -- `SCW_NUMBER_VERSIONS_TO_KEEP`: The number of latest tags to keep for each image. |
26 | | -- `SCW_NO_DRY_RUN` (optional): Set to `true` to perform actual deletions. If not set, the application will run in dry-run mode, only logging the actions that would be taken. |
| 21 | +# Here we build the image to push |
| 22 | +docker build -t rg.fr-par.scw.cloud/registry-cleaner/versions-retention:v1 . |
27 | 23 |
|
28 | | -## Example |
| 24 | +## TIP: for Apple Silicon or other ARM processors, please use the following command as Serverless Jobs supports amd64 architecture |
| 25 | +# docker buildx build --platform linux/amd64 -t rg.fr-par.scw.cloud/registry-cleaner/versions-retention:v1 . |
29 | 26 |
|
30 | | -To run the application in dry-run mode and keep the 5 latest tags for each image, set the following environment variables and run: |
| 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 | +``` |
31 | 30 |
|
32 | | -```bash |
33 | | -export SCW_DEFAULT_ORGANIZATION_ID=your-organization-id |
34 | | -export SCW_ACCESS_KEY=your-access-key |
35 | | -export SCW_SECRET_KEY=your-secret-key |
36 | | -export SCW_PROJECT_ID=your-project-id |
37 | | -export SCW_NUMBER_VERSIONS_TO_KEEP=5 |
| 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. |
38 | 33 |
|
39 | | -./reg-clean |
40 | | -``` |
| 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 |
41 | 65 |
|
42 | | -To run the application and actually delete the tags, set `SCW_NO_DRY_RUN` to `true`: |
| 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. |
43 | 67 |
|
44 | | -```bash |
45 | | -export SCW_NO_DRY_RUN=true |
| 68 | +# Additional content |
46 | 69 |
|
47 | | -./reg-clean |
48 | | -``` |
| 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