badhouseplants-net/scripts/deploy-app.pl

71 lines
2.3 KiB
Perl
Executable File

#! /usr/bin/perl
use strict;
use warnings;
my $chart_version = `cat chart/Chart.yaml | yq '.version'` or die $1;
chomp($chart_version);
my $git_branch = `git rev-parse --abbrev-ref HEAD`;
chomp($git_branch);
my $git_commit_sha = `git rev-parse HEAD`;
chomp($git_commit_sha);
my $main_branch = "main";
my $values = "";
my $remark_secret = `openssl rand -hex 12`;
chomp($remark_secret);
$ENV{'ARGO_APP_CHART_VERSION'} = $chart_version;
$ENV{'ARGO_APP_BRANCH'} = $git_branch;
$ENV{'ARGO_APP_HOSTNAME'} = "$git_branch-dev.badhouseplants.net";
$ENV{'ARGO_APP_IMAGE_TAG'} = $git_commit_sha;
$ENV{'ARGO_REMARK_SECRET'} = $remark_secret;
# ----------------------------------
# -- Fill the Application manifest
# -- with correct values
# ----------------------------------
if ($git_branch eq $main_branch) {
print "Using the main values file\n";
print `envsubst < ./kube/values-main.yaml > /tmp/values.yaml` or die $!;
} else {
print "Using the preview values file\n";
print `envsubst < ./kube/values-preview.yaml > /tmp/values.yaml` or die $!;
}
print `yq -i '.values' /tmp/values.yaml` or die $!;
print `envsubst < ./kube/application.yaml > /tmp/application.yaml` or die $!;
print `yq -i '.spec.source.helm.values = load_str("/tmp/values.yaml")' /tmp/application.yaml` or die $!;
if(!defined $ENV{DEPLOY_SCRIPT_DEBUG}){
print `argocd app create -f /tmp/application.yaml --upsert` or die $!;
print `argocd app sync --prune -l application=badhouseplants -l branch=$git_branch` or die $!;
print `argocd app wait -l application=badhouseplants -l branch=$git_branch` or die $!;
}
# ----------------------------------
# -- Remove all `Applications` for
# -- branches that do not exists
# ----------------------------------
my @all_applications = `argocd app list -l application=badhouseplants -o yaml | yq '.[].metadata.name'` or die $!;
chomp(@all_applications);
foreach my $app (@all_applications) {
$app =~ s/badhouseplants-//;
}
my @all_branches = `git branch --format='%(refname:short)' -r` or die $!;
chomp(@all_branches);
foreach my $branch (@all_branches) {
$branch =~ s/origin\///;
}
foreach my $app (@all_applications) {
if ( !grep( /^$app$/, @all_branches ) ) {
if ($app ne "application") {
print "$app should be removed\n";
if(!defined $ENV{DEPLOY_SCRIPT_DEBUG}){
print `argocd app delete --yes badhouseplants-$app` or die $!;
}
}
}
}