iac/.forgejo/workflows/CD.yml

127 lines
4.2 KiB
YAML
Raw Normal View History

2024-07-12 23:57:29 -04:00
name: Deploy to Hosts
on:
pull_request:
types: [closed]
jobs:
deploy:
if: github.event.pull_request.merged == true
2024-07-30 20:37:13 -04:00
runs-on: docker
2024-07-12 23:57:29 -04:00
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Fetch all history for git diff
run: git fetch --depth=2
2024-07-30 22:13:47 -04:00
- name: Install jq
run: |
apt-get update && apt-get install -y jq
2024-07-12 23:57:29 -04:00
- name: Detect modified folders
id: detect-changes
run: |
if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
git fetch --unshallow
fi
2024-07-23 21:47:12 -04:00
folders=$(git diff --name-only HEAD~1 HEAD | grep '^docker/' | cut -d/ -f2 | sort | uniq)
2024-07-12 23:57:29 -04:00
echo "Modified folders: $folders"
2024-07-30 20:17:39 -04:00
echo "::set-output name=folders::$folders"
2024-07-12 23:57:29 -04:00
- name: Deploy to hosts
run: |
2024-07-30 20:17:39 -04:00
IFS=' ' read -r -a folder_array <<< "${{ steps.detect-changes.outputs.folders }}"
2024-07-12 23:57:29 -04:00
for folder in "${folder_array[@]}"; do
case $folder in
2024-09-16 23:30:26 -04:00
actual)
target_host="ubu.lan"
;;
2024-07-12 23:57:29 -04:00
arrs)
target_host="arrs.lan"
;;
arm)
target_host="arm.lan"
;;
AI)
target_host="ai.lan"
;;
authentik)
target_host="auth.lan"
;;
2024-11-07 21:56:02 -05:00
ag-main)
target_host="dns.lan"
2024-07-12 23:57:29 -04:00
;;
2024-10-13 23:31:21 -04:00
exporters)
target_host="all"
;;
2024-10-22 12:23:04 -04:00
grafana)
target_host="ubu.lan"
;;
2024-07-12 23:57:29 -04:00
jellyfin)
target_host="jf.lan"
;;
kasm)
target_host="kasm.lan"
;;
netboot)
target_host="netboot.lan"
;;
2024-09-16 23:30:26 -04:00
nexterm)
target_host="ubu.lan"
2024-07-12 23:57:29 -04:00
;;
2024-09-16 23:30:26 -04:00
npm)
target_host="npm.lan"
;;
paperless)
target_host="ubu.lan"
2024-07-12 23:57:29 -04:00
;;
portainer)
target_host="port.lan"
;;
2024-10-12 13:55:37 -04:00
runner)
target_host="runner.lan"
;;
2024-07-12 23:57:29 -04:00
# Add cases for other folders/hosts
*)
echo "Unknown folder: $folder"
2024-11-13 22:14:00 -05:00
exit 1
2024-07-12 23:57:29 -04:00
;;
esac
echo "Triggering AWX Job with target host: $target_host and folder: $folder"
curl -X POST -k -H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ secrets.AWX_API_TOKEN }}" \
-d "{\"extra_vars\": {\"target_host\": \"$target_host\", \"folder\": \"$folder\"}}" \
2024-11-13 00:31:47 -05:00
"https://awx.mafyuh.xyz/api/v2/job_templates/13/launch/" > /dev/null 2>&1
2024-11-13 22:14:00 -05:00
echo "AWX Job Triggered, waiting for logs..."
2024-07-12 23:57:29 -04:00
2024-12-02 20:02:41 -05:00
# Wait for job completion and check logs
while true; do
# Fetch the most recent job ID
job_id=$(curl -s -H "Authorization: Bearer ${{ secrets.AWX_API_TOKEN }}" \
https://awx.mafyuh.xyz/api/v2/job_templates/13/jobs/?order_by=-id | jq -r '.results[0].id')
# Fetch the logs for the current job
logs=$(curl -s -H "Authorization: Bearer ${{ secrets.AWX_API_TOKEN }}" \
https://awx.mafyuh.xyz/api/v2/jobs/$job_id/stdout/?format=txt)
2024-07-30 20:17:39 -04:00
2024-12-02 20:02:41 -05:00
# Check if the logs contain the play recap
if echo "$logs" | grep -q "ok=.* changed=.* unreachable=.* failed=.* skipped=.* rescued=.* ignored=.*"; then
echo "AWX job completed. Displaying logs:"
echo "$logs"
# Check for failed tasks in the play recap
if echo "$logs" | grep -q "failed=[1-9]"; then
echo "Detected failed steps in AWX job. Failing Actions run."
exit 1
else
echo "No failures detected in AWX job."
fi
2024-11-13 00:22:39 -05:00
2024-12-02 20:02:41 -05:00
# Exit loop as the job is complete
break
else
echo "AWX job still running. Waiting before checking again..."
sleep 15
fi
done