use a python script
All checks were successful
Deploy Containers / Prepare (push) Successful in 4s

This commit is contained in:
2025-08-01 00:33:42 -04:00
parent 801c88ca6e
commit e50ae8aa03
3 changed files with 59 additions and 21 deletions

View File

@@ -24,5 +24,4 @@ jobs:
ssh-add <(echo "${{ secrets.SSH_KEY }}")
echo "HOST *" > ~/.ssh/config
echo "${{ secrets.VAULT_PASS }}" > ~/.vault_pass.txt
chmod +x ./scripts/deploy_containers.sh
./scripts/deploy_containers.sh "${{ github.event.before }}" "${{ github.sha }}"
python3 ./scripts/deploy_containers.py "${{ github.event.before }}" "${{ github.sha }}"

View File

@@ -0,0 +1,58 @@
import re
import sys
import subprocess
bracket_regex = r'\[([^\]]*)\]'
quote_regex = r'"([^"]*)"'
def git_diff():
args = sys.argv
res = subprocess.run(f"git diff --name-only {args[1]} {args[2]}", capture_output=True, shell=True, text=True)
return res.stdout.strip().split("\n")
def construct_ansible_command(server_name = None, tag = None):
command = "ANSIBLE_CONFIG=ansible.cfg /usr/bin/ansible-playbook main.yml --vault-password-file ~/.vault_pass.txt"
if server_name:
command += f" -l {server_name}"
if tag:
command += f" --tags {tag}"
return command
def run_deployment(server_name = None, tag = None):
if tag:
print(f"Deploying task '{tag}'..")
command = construct_ansible_command(tag=tag)
elif server_name:
print(f"Deploying caddy on server '{server_name}'..")
command = construct_ansible_command(server_name, "caddy_server")
res = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
lines = res.stdout.decode(encoding='utf-8').split("\n")
for ind, line in enumerate(lines):
if "fatal:" in line:
host = re.findall(bracket_regex, line)[0]
task_failed = re.findall(bracket_regex, lines[ind - 1])[0]
reason_failed = re.findall(quote_regex, line)
print("\n---------------------")
print(" Deployment failed!")
print(f" Task: {task_failed}")
print(f" Host: {host}")
print(f" Reason: {reason_failed[2].split(":")[1].strip()}")
print("---------------------\n")
def main():
diff = git_diff()
for file in diff:
if "host_vars" in file:
server_name = file.split("/")[1].split(".")[0]
run_deployment(server_name=server_name)
if "tasks" in file:
task_name = file.split("/")[1].split(".")[0] + "_deploy"
run_deployment(tag=task_name)
if __name__ == "__main__":
main()

View File

@@ -1,19 +0,0 @@
#!/bin/bash
# all new/updated tasks in the diff
new_tasks=($(git diff --name-only $1 $2 | grep '\.yml$'))
echo $new_tasks
echo $1 $2
if [ ! -z "$new_tasks" ]; then
for task in "${new_tasks[@]}"; do
ansible_tag=$(echo "$task" | awk -F/ '{print $2}')
if [[ "$ansible_tag" != "all.yml" && "$ansible_tag" != "all.template.yml" && "$ansible_tag" != "main.yml" ]] ; then
tag=${ansible_tag%.*}_deploy
if [[ "$tag" != "_deploy" ]] ; then
ansible-playbook main.yml --tags "$tag" --vault-password-file ~/.vault_pass.txt
fi
elif [[ "$task" == "host_vars/jade.yml" || "$task" == "host_vars/jackson.yml" ]] ; then
ansible-playbook main.yml --tags "caddy_deploy" --vault-password-file ~/.vault_pass.txt
fi
done
fi