2023-04-24 16:01:13 +00:00
|
|
|
#! /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;
|
2023-04-24 16:01:13 +00:00
|
|
|
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
|
|
|
|
2023-04-24 16:01:13 +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;
|
2024-02-13 14:33:11 +00:00
|
|
|
if ($git_branch eq $main_branch) {
|
2024-07-24 15:58:07 +00:00
|
|
|
$ENV{'ARGO_APP_NAMESPACE'} = "production";
|
2024-02-13 14:33:11 +00:00
|
|
|
} else {
|
2024-07-24 15:58:07 +00:00
|
|
|
$ENV{'ARGO_APP_NAMESPACE'} = "development"
|
2024-02-13 14:33:11 +00:00
|
|
|
}
|
2023-05-15 09:25:55 +00:00
|
|
|
$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}){
|
2023-05-21 15:38:20 +00:00
|
|
|
print `argocd app create -f /tmp/application.yaml --upsert` or die $!;
|
2023-06-23 15:55:48 +00:00
|
|
|
print `argocd app sync --prune -l application=badhouseplants -l branch=$git_branch` or die $!;
|
2023-05-15 09:25:55 +00:00
|
|
|
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 $!;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|