This repository has been archived on 2024-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
Auto-Homelab/.forgejo/workflows/CD.yml

84 lines
3 KiB
YAML

name: Deploy to Hosts
on:
pull_request:
types: [closed]
jobs:
deploy:
if: github.event.pull_request.merged == true
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Fetch all history for git diff
run: git fetch --depth=2
- name: Detect modified folders
id: detect-changes
run: |
if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then
git fetch --unshallow
fi
folders=$(git diff --name-only HEAD~1 HEAD | cut -d/ -f1 | sort | uniq)
echo "Modified folders: $folders"
echo "::set-output name=folders::$folders"
- name: Determine target host
id: determine-host
run: |
folder=$(echo "${{ steps.detect-changes.outputs.folders }}" | head -n 1)
if [ -z "$folder" ]; then
echo "No modified folders detected"
exit 1
fi
case $folder in
arrs)
target_host="arrs.lan"
;;
ag_main)
target_host=${{ secrets.AGMAIN_IP }}
;;
downloaders)
target_host=${{ secrets.DOWNLOADERS_IP }}
;;
AI)
target_host="ai.lan"
;;
# Add cases for other folders/hosts
*)
echo "Unknown folder: $folder"
exit 1
;;
esac
echo "::set-output name=target_host::$target_host"
echo "::set-output name=folder::$folder"
- name: Trigger AWX Job
run: |
TARGET_HOST="${{ steps.determine-host.outputs.target_host }}"
FOLDER="${{ steps.determine-host.outputs.folder }}"
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\"}}" \
"https://awx.mafyuh.xyz/api/v2/job_templates/13/launch/"
- name: Delay for 2 minutes
run: sleep 120 # 120 seconds = 2 minutes
- name: Fetch AWX Job Logs
run: |
# Fetch the most recent job from AWX
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 job
logs=$(curl -s -H "Authorization: Bearer ${{ secrets.AWX_API_TOKEN }}" https://awx.mafyuh.xyz/api/v2/jobs/$job_id/stdout/?format=json)
# Display the logs
echo "Range:"
echo "Start: $(echo "$logs" | jq -r '.range.start')"
echo "End: $(echo "$logs" | jq -r '.range.end')"
echo "Absolute End: $(echo "$logs" | jq -r '.range.absolute_end')"
echo "Content:"
echo "$(echo "$logs" | jq -r '.content')"