70 lines
2.4 KiB
Perl
Executable File
70 lines
2.4 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
use warnings;
|
|
# --------------------------------------
|
|
# -- Drone variables
|
|
# --------------------------------------
|
|
my $drone_url="$ENV{'DRONE_SYSTEM_PROTO'}://$ENV{'DRONE_SYSTEM_HOST'}";
|
|
my $drone_project=$ENV{'DRONE_REPO'};
|
|
my $drone_api="$drone_url/api/repos/$drone_project/builds";
|
|
# --------------------------------------
|
|
# -- Gitea variables
|
|
# --------------------------------------
|
|
my $gitea_url=$ENV{'GITEA_URL'} || 'https://git.badhouseplants.net/api/v1';
|
|
my $gitea_org=$ENV{'GITEA_ORG'} || 'badhouseplants';
|
|
my $gitea_package=$ENV{'GITEA_PACKAGE'} || 'badhouseplants-net';
|
|
my $gitea_api="$gitea_url/packages/$gitea_org/container/$gitea_package";
|
|
my $gitea_token=$ENV{'GITEA_TOKEN'};
|
|
my $gitea_user=$ENV{'GITEA_USER'} || $ENV{'DRONE_COMMIT_AUTHOR'};
|
|
# ---------------------------------------
|
|
# -- Get recent builds from drone-ci
|
|
# ---------------------------------------
|
|
my $builds = "curl -X 'GET' $drone_api -H 'accept: application/json' | jq -r '.[].after'";
|
|
my @builds_out = `$builds`;
|
|
chomp @builds_out;
|
|
# ---------------------------------------
|
|
# -- Get a list of all commits + 'latest'
|
|
# ---------------------------------------
|
|
my $commits = "argocd app list -o yaml -l application=badhouseplants | yq '.[].metadata.labels.commit_sha'";
|
|
my @commits_out = `$commits`;
|
|
chomp @commits_out;
|
|
push @commits_out, 'latest';
|
|
# --------------------------------------
|
|
# -- Rclone variables
|
|
# -------------------------------------
|
|
my $dirs = "rclone lsf badhouseplants-minio:/badhouseplants-net";
|
|
my @dirs_out = `$dirs`;
|
|
chomp @dirs_out;
|
|
# ---------------------------------------
|
|
# -- Compare builds to commits
|
|
# -- And remove obsolete imgages from
|
|
# -- registry
|
|
# ---------------------------------------
|
|
print "Cleaning up the container registry\n";
|
|
foreach my $line (@builds_out)
|
|
{
|
|
print "Checking if $line is in @commits_out\n\n";
|
|
if ( ! grep( /^$line$/, @commits_out ) ) {
|
|
my $cmd = "curl -X 'DELETE' -s \"$gitea_api/$line\" -H 'accept: application/json' -u $gitea_user:$gitea_token || true";
|
|
print "Removing ${line}\n\n";
|
|
my $output = `$cmd`;
|
|
print "$output \n";
|
|
}
|
|
}
|
|
|
|
print "Cleaning up Minio\n";
|
|
foreach my $line (@dirs_out)
|
|
{
|
|
print "Checking if $line is in @commits_out\n\n";
|
|
chop($line);
|
|
if ( ! grep( /^$line$/, @commits_out ) ) {
|
|
my $cmd = "rclone purge badhouseplants-minio:/badhouseplants-net/$line";
|
|
print "Removing ${line}\n\n";
|
|
my $output = `$cmd`;
|
|
print "$output \n";
|
|
}
|
|
}
|
|
|
|
|