Skip to content

Commit

Permalink
#270 Adjust prune workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
chang-annie committed Aug 19, 2024
1 parent 18f3e04 commit 565f6fa
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 6 deletions.
40 changes: 40 additions & 0 deletions .github/scripts/release_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# Initialize array to store digest SHAs
declare -a digests

image_names=$(gh api \
-H 'Accept: application/vnd.github+json' \
-H '-GitHub-Api-Version: 2022-11-28' \
--paginate '/orgs/boozallen/packages?package_type=container' | jq -r '.[] | select((.name | startswith("aissemble")) and (.name | endswith("-chart") | not)) | .name')

for name in $image_names; do
echo "Processing image: $name"

release_versions=$(gh api \
-H 'Accept: application/vnd.github+json' \
-H '-GitHub-Api-Version: 2022-11-28' \
--paginate /orgs/boozallen/packages/container/"$name"/versions \
| jq -r '.[] | .metadata.container.tags[] | select(test("^\\d+\\.\\d+\\.\\d?$"))' | \
jq -R -s 'split("\n") | map(select(length > 0)) | map(select(. != "1.7.0" and . != "1.7.0-arm64" and . != "1.7.0-amd64"))')

echo "All Release Versions:"
echo "$release_versions"

for version in $(echo "$release_versions" | jq -r '.[]'); do

echo "Processing release_version: $version"
manifest_list_shas=$(docker buildx imagetools inspect --raw "${REGISTRY}/${ORG}/${name}:${version}" | jq -r '.manifests[].digest' | paste -s -d ' ' -)

echo "Manifest List: $manifest_list_shas"
digests+=("$manifest_list_shas")
echo "Digests: $digests"
done

done

# Join digests into a single string separated by spaces
digests_string=$(echo "${digests[*]}")

# Save the output to $GITHUB_OUTPUT
echo "${digests_string}" >> "$GITHUB_OUTPUT"
77 changes: 77 additions & 0 deletions .github/scripts/snapshot_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Initialize array to store digest SHAs
declare -a digests

image_names=$(gh api \
-H 'Accept: application/vnd.github+json' \
-H '-GitHub-Api-Version: 2022-11-28' \
--paginate '/orgs/boozallen/packages?package_type=container' | jq -r '.[] | select((.name | startswith("aissemble")) and (.name | endswith("-chart") | not)) | .name')

for name in $image_names; do
echo "Processing image: $name"

all_snapshot_versions=$(gh api \
-H 'Accept: application/vnd.github+json' \
-H '-GitHub-Api-Version: 2022-11-28' \
--paginate /orgs/boozallen/packages/container/"$name"/versions \
| jq -r '.[] | .metadata.container.tags[] | select(test("-SNAPSHOT(-arm64|-amd64)?$"))'| jq -R -s 'split("\n")')

echo "All Snapshot Versions:"
echo $all_snapshot_versions

latest_snapshot_version=$(echo "$all_snapshot_versions" | jq -r '.[] | select(endswith("-SNAPSHOT"))' | sort | tail -n 1)
echo "Latest Snapshot Version: $latest_snapshot_version"

version_part="${latest_snapshot_version%-SNAPSHOT}"

# Extract the major, minor, and patch components using IFS (Internal Field Separator)
IFS='.' read -r major minor patch <<< "$version_part"
echo "Major: $major"
echo "Minor: $minor"
echo "Patch: $patch"

previous_minor=$((minor - 1))
echo "Previous minor: $previous_minor"
# Check if previous_minor is valid (not negative)
if [ $previous_minor -ge 0 ]; then
patch_pattern=${major}.${previous_minor}.[1-9]-SNAPSHOT$

for version in $(echo "$all_snapshot_versions" | jq -r '.[]'); do
if [[ $version =~ ^${patch_pattern}$ ]]; then

echo "Existing Patch Version found: $version"
all_patch_versions+=("$version")

# Print the array to verify
echo "All Patch Versions array: "
echo $(printf "%s\n" ${all_patch_versions[@]})

sorted_versions=($(printf "%s\n" "${all_patch_versions[@]}" | sort -V))
echo "Sorted Versions:"
echo "${sorted_versions[@]}"
latest_patch_version=($(printf "%s\n" "${all_patch_versions[@]}" | sort -V | tail -n 1))

echo "Latest Patch Version: $latest_patch_version"
latest_patch_version_digest=$(docker buildx imagetools inspect --raw "ghcr.io/boozallen/${name}:${latest_patch_version}" | jq -r '.manifests[].digest' | paste -s -d ' ' -)

digests+=("$latest_patch_version_digest")

fi
done
fi



latest_snapshot_manifest_list_shas=$(docker buildx imagetools inspect --raw "${REGISTRY}/${ORG}/${name}:${latest_snapshot_version}" | jq -r '.manifests[].digest' | paste -s -d ' ' -)

echo "Manifest List: $latest_snapshot_manifest_list_shas"
digests+=("$latest_snapshot_manifest_list_shas")
echo "Digests: $digests"

done

digests_string=$(echo "${digests[*]}")

# Save the output to $GITHUB_OUTPUT
echo "${digests_string}" >> "$GITHUB_OUTPUT"
35 changes: 29 additions & 6 deletions .github/workflows/prune.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,46 @@ on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

schedule:
- cron: "0 0 * * *" # every day at midnight
# schedule:
# - cron: "0 0 * * *" # every day at midnight

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Prune
uses: snok/container-retention-policy@v2
# Prevents multi-platform release images from being pruned by identifying all manifest lists
- name: Fetch multi-platform package version SHAs
id: multi-arch-digests
run: |
chmod +x .github/scripts/release_images.sh
.github/scripts/release_images.sh
# Prevents the latest snapshot images from being pruned by identifying all manifest lists
- name: Fetch latest snapshot version SHAs
id: latest-snapshot-digests
run: |
chmod +x .github/scripts/snapshot_images.sh
.github/scripts/snapshot_images.sh
- name: Concatenate digests
id: concat-digests
run: |
skip_shas="${{ steps.multi-arch-digests.outputs.multi-arch-digests }},${{ steps.latest-snapshot-digests.outputs.latest-snapshot-digests }}"
echo "skip_shas=$skip_shas" >> $GITHUB_OUTPUT
- name: Prune old release versions
uses: snok/container-retention-policy@v3
with:
skip-shas: ${{ steps.concat-digests.outputs.skip_shas }}
image-names: aissemble-*
image-tags: "!1.7.0 !1.7.0-arm64 !1.7.0-amd64"
cut-off: Two days ago UTC
account-type: org
org-name: boozallen
keep-at-least: 2
untagged-only: true
dry-run: false
# untagged-only: true
dry-run: true
token: ${{ secrets.GHCR_IO_TOKEN }}

0 comments on commit 565f6fa

Please sign in to comment.