This repository has been archived on 2024-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
badhouseplants-net-old/scripts/deploy-app.pl

71 lines
2.3 KiB
Perl
Raw Permalink Normal View History

#! /usr/bin/perl
use strict;
use warnings;
2023-05-15 09:25:55 +00:00
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);
2023-05-15 09:25:55 +00:00
my $main_branch = "main";
2023-05-15 09:25:55 +00:00
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 $!;
2023-05-15 09:25:55 +00:00
print `argocd app sync -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 $!;
}
}
}
}