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 20, 2024
1 parent 18f3e04 commit 8f66d07
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 10 deletions.
36 changes: 36 additions & 0 deletions .github/scripts/release_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/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" \
-H "Authorization: Token ${GITHUB_TOKEN}" \
--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
release_versions=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Token ${GITHUB_TOKEN}" \
--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"))')

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

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

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

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

# Save the output to $GITHUB_OUTPUT
echo "multi-arch-digests=${digests_string}" >> "$GITHUB_OUTPUT"
58 changes: 58 additions & 0 deletions .github/scripts/snapshot_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/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" \
-H "Authorization: Token ${GITHUB_TOKEN}" \
--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
all_snapshot_versions=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Token ${GITHUB_TOKEN}" \
--paginate "/orgs/boozallen/packages/container/${name}/versions" \
| jq -r '.[] | .metadata.container.tags[] | select(test("-SNAPSHOT(-arm64|-amd64)?$"))'| jq -R -s 'split("\n")')

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

echo "Processing snapshot image ${name}:${latest_snapshot_version}"
latest_snapshot_manifest_list_shas=$(docker buildx imagetools inspect --raw "ghcr.io/boozallen/${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")

# Find if there are any patch versions from the previous minor available
# Extract version from latest snapshot
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"
previous_minor=$((minor - 1))
# 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
# Found a patch version matching the given pattern
all_patch_versions+=("$version")
latest_patch_version=($(printf "%s\n" "${all_patch_versions[@]}" | sort -V | tail -n 1))

echo "Patch Version found: ${name}:${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
done

digests_string=$(echo "${digests[*]}")
#echo "Digest String: $digests_string"

# Save the output to $GITHUB_OUTPUT
echo "latest-snapshot-digests=${digests_string}" >> "$GITHUB_OUTPUT"
45 changes: 35 additions & 10 deletions .github/workflows/prune.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,48 @@ 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
# Required in order to access script files in this repository
- uses: actions/checkout@v4

# Prevents multi-platform release images from being pruned by identifying all manifest lists
- name: Fetch multi-platform package version SHAs
id: multi-arch-digests
env:
GITHUB_TOKEN: ${{ secrets.GHCR_IO_TOKEN }}
run: bash ${GITHUB_WORKSPACE}/.github/scripts/release_images.sh

# Prevents the latest snapshot images from being pruned
- name: Fetch latest snapshot version SHAs
id: latest-snapshot-digests
env:
GITHUB_TOKEN: ${{ secrets.GHCR_IO_TOKEN }}
run: bash ${GITHUB_WORKSPACE}/.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.0.0
with:
skip-shas: ${{ steps.concat-digests.outputs.skip_shas }}
image-names: aissemble-*
cut-off: Two days ago UTC
account-type: org
org-name: boozallen
keep-at-least: 2
untagged-only: true
dry-run: false
image-tags: "!1.7.0 !1.7.0-arm64 !1.7.0-amd64"
cut-off: 2d
account: boozallen
keep-n-most-recent: 12
# untagged-only: true
dry-run: true
token: ${{ secrets.GHCR_IO_TOKEN }}

0 comments on commit 8f66d07

Please sign in to comment.