From 9e1339182b4113110204e99ed6c7864ae3ab5888 Mon Sep 17 00:00:00 2001 From: johnson2427 <37009091+johnson2427@users.noreply.github.com> Date: Fri, 30 Aug 2024 12:46:01 -0500 Subject: [PATCH] feat: adding retention policy (#2247) --- .github/workflows/build.yaml | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 9dca727dfd..622f2f48e1 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -39,7 +39,6 @@ jobs: - name: Show tags run: | - git tag echo ${{ steps.meta.outputs.tags }} - name: Extract version from tag @@ -77,3 +76,51 @@ jobs: labels: ${{ steps.meta.outputs.labels }} build-args: | SLIM_IMAGE=${{ steps.meta.outputs.tags }}-slim + + - name: Fetch all tags and store them + run: | + # List all tags + tags=$(git tag -l) + echo "All tags:" + echo "$tags" + + # Save tags in an environment variable for later use + echo "ALL_TAGS=$(echo $tags | tr '\n' ' ')" >> $GITHUB_ENV + + - name: Retain last two minor versions + run: | + # Fetch all tags in the format X.Y.Z + echo "Using stored tags..." + all_tags="${{ env.ALL_TAGS }}" + echo "All tags:" + echo "$all_tags" + + # Extract the minor versions (X.Y) from tags in the format vX.X.X + latest_two_minors=$(echo "$all_tags" | tr ' ' '\n' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sed -E 's/^v([0-9]+\.[0-9]+)\.[0-9]+$/\1/' | uniq | tail -n 2) + echo "Last two minor versions:" + echo "$latest_two_minors" + + keep_tags="" + for minor in $latest_two_minors; do + echo "Processing minor version: $minor" + patches=$(echo "$all_tags" | tr ' ' '\n' | grep "^v$minor\.") + echo "Latest patch for $minor: $patches" + keep_tags="$keep_tags $patches" + done + + # Store the tags in the environment variable + keep_tags=$(echo $keep_tags | tr ' ' '\n' | paste -sd ',' -) + echo "Tags to keep: $keep_tags" + echo "keep_tags=$keep_tags" >> $GITHUB_ENV + echo "keep_tags=$keep_tags" + + - name: Run container retention policy + if: github.ref == 'refs/heads/main' + uses: snok/container-retention-policy@v3.0.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + keep-tag-names: | + stable + latest + ${{ env.keep_tags }} +