Cleanup Artifacts #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cleanup Artifacts | |
on: | |
workflow_dispatch: # Allows manual triggering of the workflow | |
# schedule: | |
# - cron: '0 0 * * *' # Adjust the schedule as needed | |
env: | |
CONTAINER_REPO: ${{ secrets.CONTAINER_REPO }} | |
ARTIFACTORY_URL: ${{ secrets.CONTAINER_REGISTRY }} | |
CONTAINER_REGISTRY_USERNAME: ${{ secrets.CONTAINER_REGISTRY_USERNAME }} | |
CONTAINER_REGISTRY_PASSWORD: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }} | |
jobs: | |
cleanup: | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Set up JFrog CLI | |
run: | | |
curl -fL https://getcli.jfrog.io | sh | |
sudo mv jfrog /usr/local/bin/jf | |
sudo chmod +x /usr/local/bin/jf | |
jf -v | |
- name: Configure JFrog CLI | |
run: | | |
jf config add data-catalogue-rt --url=https://artifacts.developer.gov.bc.ca:443 --user=${{ env.CONTAINER_REGISTRY_USERNAME }} --password=${{ env.CONTAINER_REGISTRY_PASSWORD }} | |
- name: Check JFrog Connection | |
id: check_connection | |
run: | | |
response=$(jf rt ping) | |
echo "Response: $response" | |
echo "STATUS=$response" >> $GITHUB_ENV | |
- name: Search and Remove Old Artifacts | |
if: ${{ env.STATUS == 'OK' }} | |
run: | | |
folders=("mfin-data-catalogue" "mfin-data-catalogue-nginx") | |
n = 5 | |
repo=${{ env.CONTAINER_REPO }} | |
for folder in "${folders[@]}"; do | |
pattern="$repo/$folder/1.0.0-dev*/" | |
jf rt s "$pattern" --limit=1000 --sort-by="created" --sort-order="desc" | jq -r '.[].path' | cut -d'/' -f1-3 | sort -u > "${folder//\//_}_artifacts.txt" | |
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 "{}" | |
else | |
echo "No folders to delete in $folder." | |
fi | |
done | |
- name: Clean Up Temporary Files | |
run: | | |
rm -f *_artifacts.txt |