Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JFrog Artifactory cleanup action #567

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions .github/workflows/artifactory-cleanup.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Cleanup Artifacts

on:
workflow_dispatch: # Allows manual triggering of the workflow
# schedule:
# - cron: '0 0 * * *' # Adjust the schedule as needed
workflow_dispatch:
schedule:
- cron: '0 0 * * 6'

env:
CONTAINER_REPO: ${{ secrets.CONTAINER_REPO }}
ARTIFACTORY_URL: ${{ secrets.CONTAINER_REGISTRY }}
ARTIFACTORY_URL: ${{ secrets.ARTIFACTORY_URL }}
CONTAINER_REGISTRY_USERNAME: ${{ secrets.CONTAINER_REGISTRY_USERNAME }}
CONTAINER_REGISTRY_PASSWORD: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }}

Expand All @@ -20,42 +20,42 @@ jobs:
- name: Set up JFrog CLI
run: |
curl -fL https://getcli.jfrog.io | sh
sudo mv jfrog /usr/local/bin/
sudo mv jfrog /usr/local/bin/jf
sudo chmod +x /usr/local/bin/jf
jf -v

- name: Configure JFrog CLI
run: |
jfrog config add data-catalogue-rt --url=${{ env.ARTIFACTORY_URL }} --user=${{ env.CONTAINER_REGISTRY_USERNAME }} --password=${{ env.CONTAINER_REGISTRY_PASSWORD }}
jf config add data-catalogue-rt --url=${{ env.ARTIFACTORY_URL }} --user=${{ env.CONTAINER_REGISTRY_USERNAME }} --password=${{ env.CONTAINER_REGISTRY_PASSWORD }}

- name: Check JFrog Connection
id: check_connection
run: |
response=$(jfrog rt ping)
response=$(jf rt ping)
echo "Response: $response"
echo "::set-output name=status::$response"
echo "STATUS=$response" >> $GITHUB_ENV


- name: Search and Remove Old Artifacts
if: steps.check_connection.outputs.status == 'OK'
if: ${{ env.STATUS == 'OK' }}
run: |
folders=("mfin-data-catalogue" "mfin-data-catalogue-nginx") # Define the folders to search
n= 5 # Number of images to keep
folders=("mfin-data-catalogue" "mfin-data-catalogue-nginx")
n=30
repo=${{ env.CONTAINER_REPO }}

for folder in "${folders[@]}"; do
# Create a search pattern
for folder in "${folders[@]}"; do
pattern="$repo/$folder/1.0.0-dev*/"

# Search and store results in a separate file

jf rt s "$pattern" --limit=1000 --sort-by="created" --sort-order="desc" | jq -r '.[].path' | cut -d'/' -f1-3 | sort -u > "${folder//\//_}_artifacts.txt"

# Check total entries and delete old folders if necessary

total_folders=$(wc -l < "${folder//\//_}_artifacts.txt")
if [ $total_folders -gt $n ]; then
echo "Deleting $((total_folders - n)) old folders from $folder..."
#head -n $((total_folders - n)) "${folder//\//_}_artifacts.txt" | xargs -I {} jf rt del "{}" --quiet
head -n $((total_folders - n)) "${folder//\//_}_artifacts.txt" | xargs -I {} echo "{}"
echo "Deleting $((total_folders - n)) old Images from $folder..."
head -n $((total_folders - n)) "${folder//\//_}_artifacts.txt" | xargs -I {} echo "{}"
head -n $((total_folders - n)) "${folder//\//_}_artifacts.txt" | xargs -I {} jf rt del "{}" --dry-run=true --quiet

else
echo "No folders to delete in $folder."
echo "No Images to delete in $folder."
fi
done

Expand Down