Commit 5a594d8d authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Add script to prune old Docker images and integrate into CI/CD pipelines

parent e090ecb6
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
#!/bin/bash
# Copyright 2022-2026 ETSI SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

MAX_AGE_DAYS="${MR_DOCKER_IMAGE_PRUNE_DAYS:-7}"
CUTOFF_EPOCH="$(date -u -d "${MAX_AGE_DAYS} days ago" +%s)"
IMAGE_REFS_TO_REMOVE="$(mktemp)"
trap 'rm -f "${IMAGE_REFS_TO_REMOVE}"' EXIT

docker image ls --format '{{.Repository}} {{.Tag}}' | while read -r REPOSITORY TAG; do
    if [ "${REPOSITORY}" = "<none>" ]; then
        continue
    fi
    if [[ ! "${TAG}" =~ ^mr[0-9]+$ ]]; then
        continue
    fi

    IMAGE_REF="${REPOSITORY}:${TAG}"
    CREATED="$(docker image inspect --format '{{.Created}}' "${IMAGE_REF}" 2>/dev/null || true)"
    if [ -z "${CREATED}" ]; then
        continue
    fi

    CREATED_EPOCH="$(date -u -d "${CREATED}" +%s)"
    if [ "${CREATED_EPOCH}" -lt "${CUTOFF_EPOCH}" ]; then
        echo "${IMAGE_REF}" >> "${IMAGE_REFS_TO_REMOVE}"
    fi
done

if [ ! -s "${IMAGE_REFS_TO_REMOVE}" ]; then
    echo "No Docker images tagged 'mr[0-9]+' older than ${MAX_AGE_DAYS} days found."
    exit 0
fi

echo "Removing Docker images tagged 'mr[0-9]+' older than ${MAX_AGE_DAYS} days:"
sort -u "${IMAGE_REFS_TO_REMOVE}"
sort -u "${IMAGE_REFS_TO_REMOVE}" | xargs --no-run-if-empty docker image rm --force
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ end2end_test acl_end2end:
  #needs:
  #  - build acl_end2end
  before_script:
    - bash scripts/prune_old_mr_docker_images.sh
    # Cleanup old ContainerLab scenarios
    - containerlab destroy --all --cleanup || true

+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ end2end_test ecoc22:
  #needs:
  #  - build ecoc22
  before_script:
    - bash scripts/prune_old_mr_docker_images.sh
    # Do Docker cleanup
    - docker ps --all --quiet | xargs --no-run-if-empty docker stop
    - docker container prune --force
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ end2end_test eucnc24:
  #needs:
  #  - build eucnc24
  before_script:
    - bash scripts/prune_old_mr_docker_images.sh
    # Cleanup old ContainerLab scenarios
    - containerlab destroy --all --cleanup || true

+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ end2end_test l2_vpn_gnmi_oc:
  #needs:
  #  - build l2_vpn_gnmi_oc
  before_script:
    - bash scripts/prune_old_mr_docker_images.sh
    # Cleanup old ContainerLab scenarios
    - containerlab destroy --all --cleanup || true

Loading