Skip to content

Commit

Permalink
Add an artifactory cleanup action
Browse files Browse the repository at this point in the history
  • Loading branch information
loicalbertin committed Apr 26, 2021
1 parent 3a57738 commit 4bf0d06
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/actions-test.yml

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/cleanup_artifactory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Artifactory Cleanup
on:
workflow_dispatch:
inputs:
from_date:
description: ''
required: false
default: '30 days ago'
schedule:
- cron: '0 12 7,14,21,28 * *'

defaults:
run:
shell: bash

jobs:
cleanup:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Setup jfrog CLI
uses: jfrog/setup-jfrog-cli@v1
env:
JF_ARTIFACTORY_1: ${{ secrets.JF_ARTIFACTORY_SERVER_1 }}

- name: Ping Artifactory with jfrog CLI
run: |
# Ping the server
jfrog rt ping
- name: Run Cleanup
run: |
./build/gh-action-cleanup-artifactory.sh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FROM_DATE: ${{ github.event.inputs.from_date || '30 days ago' }}
39 changes: 39 additions & 0 deletions build/gh-action-cleanup-artifactory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

set -euo pipefail

comp_date=$(date --date="${FROM_DATE:=14 days ago}" +"%Y-%m-%dT%H:%M:%S.000Z")

local_dist_path="yorc-bin-dev-local/ystia/yorc/dist"

all_paths=$(jfrog rt s "${local_dist_path}/*/yorc-*.tgz" --limit 0 | jq -r ".[]| [.modified, .path] | @tsv" | sed -e "s@${local_dist_path}/\(.*\)/yorc-.*\.tgz@\1@g" | sort)

function get_pr_state() {
gh pr view "${1}" --json state | jq -r ".state"
}

function does_branch_exit() {
gh api --silent "/repos/:owner/:repo/branches/${1}" 2> /dev/null
return $?
}

function delete_artifactory_path() {
jfrog rt del --quiet "${local_dist_path}/${1}" || echo "failed to delete ${local_dist_path}/${1}"
}

echo "${all_paths}" | while read line ; do
item_date=$(echo "$line" | awk '{print $1}')
if [[ "${item_date}" > "${comp_date}" ]] ; then
continue
fi
ref=$(echo "${line}" | awk -F '\t' '{print $2}')
if [[ "${ref}" == PR-* ]] ; then
if [[ "$(get_pr_state "${ref##*PR-}")" != "OPEN" ]] ; then
delete_artifactory_path "${ref}"
fi
else
if ! does_branch_exit "${ref}" ; then
delete_artifactory_path "${ref}"
fi
fi
done

0 comments on commit 4bf0d06

Please sign in to comment.