logo
0
0
WeChat Login

CNB Auto Clean Plugin

CNB automatic clean plugin that supports cleaning repository Releases, Git Tags, and Docker image tags.

Features

  • Clean Releases (filtered by name/tag prefix, time, or count)
  • Clean Git Tags (filtered by name prefix or count)
  • Clean Docker Image Tags (filtered by name prefix, time, or count, based on push_at)
  • Supports dry-run mode (output only, no deletion)
  • Automatic pagination to traverse full dataset
  • Outputs pipeline variables for subsequent steps

Clean Types

Control which resources to clean via the cleanType parameter:

cleanTypeDescription
releaseOnly clean Releases
tagOnly clean Git Tags
dockerOnly clean Docker Image Tags
all (default)Clean all of the above

Note: When cleanType is docker or all, ensure the CNB_REPO_SLUG_LOWERCASE environment variable is available, or specify it via the imageSlug parameter.

Filter Rules

Specify filtering strategy via the filter parameter, in the format TYPE=PARAM:

Filter TypeFormatDescriptionApplicable To
TAGNAME_PREFIXTAGNAME_PREFIX=v1.Match by tag name prefixRelease / Tag / Docker
NAME_PREFIXNAME_PREFIX=build-Match by name prefixRelease / Tag / Docker
RECENT_NRECENT_N=5Keep the most recent N, delete the restRelease / Tag / Docker
RECENT_N_DAYSRECENT_N_DAYS=30Delete records older than N daysRelease / Docker
RECENT_N_DAYS_RETAIN_NRECENT_N_DAYS_RETAIN_N=30,3Keep most recent N + delete others older than N daysRelease / Docker

Note: RECENT_N_DAYS and RECENT_N_DAYS_RETAIN_N are not applicable to Git Tags (Git Tags do not have a CreatedAt field). Docker time-based filtering uses the last_pusher.push_at field.

Usage

Use in .cnb.yml pipeline configuration:

Example 1: Clean All Types (Default)

stages:
  - name: auto-clean
    image: docker.cnb.cool/r_r/auto-clean:latest
    settings:
      cleanType: "all"
      filter: "RECENT_N_DAYS=30"

Example 2: Clean Only Releases, Keep Last 10

stages:
  - name: auto-clean-releases
    image: docker.cnb.cool/r_r/auto-clean:latest
    settings:
      cleanType: "release"
      filter: "RECENT_N=10"

Example 3: Clean Docker Image Tags with Prefix + Time Strategy

stages:
  - name: auto-clean-docker
    image: docker.cnb.cool/r_r/auto-clean:latest
    settings:
      cleanType: "docker"
      imageSlug: "owner/my-repo"        # Optional, defaults to CNB_REPO_SLUG_LOWERCASE
      imageName: "my-image"             # Optional, defaults to repo name
      filter: "TAGNAME_PREFIX=v2024."

Example 4: Dry Run Mode (Preview Without Deleting)

stages:
  - name: auto-clean-preview
    image: docker.cnb.cool/r_r/auto-clean:latest
    settings:
      cleanType: "all"
      filter: "RECENT_N_DAYS_RETAIN_N=90,5"
      dryRun: true

Parameters

cleanType

  • Type: string
  • Default: all
  • Required: No
  • Description: The type(s) to clean. Supported values: release, tag, docker, all

Example:

settings:
  cleanType: "docker"

filter

  • Type: string
  • Default: None
  • Required: Yes
  • Description: Filter rule in the format TYPE=PARAM. See Filter Rules above for details.

Example:

settings:
  filter: "RECENT_N_DAYS=60"

dryRun

  • Type: boolean
  • Default: false
  • Required: No
  • Description: Trial run mode. When set to true, only outputs the list of items that would be deleted without performing actual deletions.

Example:

settings:
  dryRun: true

imageSlug

  • Type: string
  • Default: Value of CNB_REPO_SLUG_LOWERCASE environment variable
  • Required: No (implicitly required when cleanType includes docker)
  • Description: Repository path (lowercase) where the Docker image package resides. Used to locate the image package in the registry. Specify manually for cross-repository cleaning.

Example:

settings:
  imageSlug: "other-owner/image-repo"

imageName

  • Type: string
  • Default: Last segment extracted from imageSlug (repository name)
  • Required: No
  • Description: Docker image name. Only effective when cleanType includes docker.

Example:

settings:
  imageName: "my-app-image"

slug

  • Type: string
  • Default: Current repository path (CNB_REPO_SLUG)
  • Required: No
  • Description: Git repository path. Not needed for same-repository operations; specify for cross-repository operations.

endpoint

  • Type: string
  • Default: Current platform's OPENAPI address
  • Required: No
  • Description: Platform API address. Not needed for same-platform operations; specify for cross-platform operations, e.g., https://api.cnb.cool.

token

  • Type: string
  • Default: Current user's temporary token
  • Required: No
  • Description: Platform token. Pass this parameter for cross-platform operations. Must include the following permissions:
    • Release/Tag cleanup: repo-contents:r
    • Docker Tag cleanup: registry:r

Output Variables

Whether dry-run or not, the following pipeline variables are output:

##[set-output total-cleaned=15]
##[set-output cleaned-releases=5]
##[set-output cleaned-tags=8]
##[set-output cleaned-docker-tags=2]

Can be referenced in subsequent steps:

VariableDescription
total-cleanedTotal deleted count (sum of all types)
cleaned-releasesRelease deleted count (0 if cleanType does not include release)
cleaned-tagsGit Tag deleted count (0 if cleanType does not include tag)
cleaned-docker-tagsDocker Tag deleted count (0 if cleanType does not include docker)

Complete Example

main:
  push:
    - stages:
        - name: auto-clean
          image: docker.cnb.cool/r_r/auto-clean:latest
          settings:
            cleanType: "all"
            filter: "RECENT_N_DAYS_RETAIN_N=90,5"
            dryRun: false
          exports:
            total-cleaned: TOTAL_CLEANED
            cleaned-releases: CLEANED_RELEASES
            cleaned-tags: CLEANED_TAGS
            cleaned-docker-tags: CLEANED_DOCKER_TAGS
        - name: echo result
          script:
            - echo "Total cleaned: $TOTAL_CLEANED"
            - echo "Releases: $CLEANED_RELEASES"
            - echo "Tags: $CLEANED_TAGS"
            - echo "Docker Tags: $CLEANED_DOCKER_TAGS"

Cross-Repository/Cross-Platform Example

main:
  push:
    - stages:
        - name: auto-clean-cross-repo
          image: docker.cnb.cool/r_r/auto-clean:latest
          settings:
            slug: "other-owner/other-repo"
            imageSlug: "other-owner/other-repo"
            endpoint: "https://api.cnb.cool"
            token: "${OTHER_REPO_TOKEN}"
            cleanType: "all"
            filter: "RECENT_N_DAYS=180"

Error Handling

When encountering API errors or missing parameters, the plugin will exit with a non-zero status code, causing the pipeline to fail. Common error scenarios:

  • Invalid cleanType value → shows supported type list
  • Missing filter parameter → prompts required field
  • Incorrect filter format → prompts correct format TYPE=PARAM
  • Docker cleaning missing ImageSlug → prompts need for CNB_REPO_SLUG_LOWERCASE or manual imageSlug
  • Insufficient permissions → API returns 401/403 error

License

MIT License