feat: add gitea actions workflows
This commit is contained in:
122
.gitea/workflows/deploy.yml
Normal file
122
.gitea/workflows/deploy.yml
Normal file
@@ -0,0 +1,122 @@
|
||||
name: Deploy Holiday Property Booking
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- develop
|
||||
- qa
|
||||
- master
|
||||
- main
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- name: Deploy branch
|
||||
shell: bash
|
||||
env:
|
||||
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
||||
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
||||
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
||||
GITEA_REF: ${{ gitea.ref }}
|
||||
run: |
|
||||
set -Eeuo pipefail
|
||||
|
||||
mkdir -p ~/.ssh
|
||||
printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
||||
chmod 600 ~/.ssh/id_ed25519
|
||||
ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts
|
||||
|
||||
REPO_URL=ssh://git@git.dumas.ddns.net:222/chris.dumas/holiday-property-booking.git
|
||||
|
||||
case "$GITEA_REF" in
|
||||
refs/heads/develop)
|
||||
TARGET_DIR=/mnt/appdata/dev/holiday-property-booking
|
||||
TARGET_BRANCH=develop
|
||||
COMPOSE_FILE=docker-compose.dev.yml
|
||||
PROJECT=holiday-property-booking-dev
|
||||
PORT=7003
|
||||
HEALTH_URL=http://192.168.1.15:7003/api/health
|
||||
;;
|
||||
refs/heads/qa)
|
||||
TARGET_DIR=/mnt/appdata/qa/holiday-property-booking
|
||||
TARGET_BRANCH=qa
|
||||
COMPOSE_FILE=docker-compose.qa.yml
|
||||
PROJECT=holiday-property-booking-qa
|
||||
PORT=6003
|
||||
HEALTH_URL=http://192.168.1.15:6003/api/health
|
||||
;;
|
||||
refs/heads/master)
|
||||
TARGET_DIR=/mnt/appdata/prod/holiday-property-booking
|
||||
TARGET_BRANCH=master
|
||||
COMPOSE_FILE=docker-compose.prod.yml
|
||||
PROJECT=holiday-property-booking-prod
|
||||
PORT=5003
|
||||
HEALTH_URL=http://192.168.1.15:5003/api/health
|
||||
;;
|
||||
refs/heads/main)
|
||||
TARGET_DIR=/mnt/appdata/prod/holiday-property-booking
|
||||
TARGET_BRANCH=main
|
||||
COMPOSE_FILE=docker-compose.prod.yml
|
||||
PROJECT=holiday-property-booking-prod
|
||||
PORT=5003
|
||||
HEALTH_URL=http://192.168.1.15:5003/api/health
|
||||
;;
|
||||
*)
|
||||
echo "Skipping unmapped ref: $GITEA_REF"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
echo "Deploying branch $TARGET_BRANCH to $TARGET_DIR with project $PROJECT"
|
||||
|
||||
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=yes "${DEPLOY_USER}@${DEPLOY_HOST}" "
|
||||
set -euo pipefail
|
||||
|
||||
ssh-keyscan -p 222 -H git.dumas.ddns.net >> ~/.ssh/known_hosts
|
||||
|
||||
NODE_BASE_IMAGE=node:20-alpine@sha256:fb4cd12c85ee03686f6af5362a0b0d56d50c58a04632e6c0fb8363f609372293
|
||||
|
||||
ensure_node_base_image() {
|
||||
for attempt in 1 2 3 4 5; do
|
||||
if docker pull \"\$NODE_BASE_IMAGE\"; then
|
||||
echo \"\$NODE_BASE_IMAGE is available\"
|
||||
return 0
|
||||
fi
|
||||
echo \"\$NODE_BASE_IMAGE pull failed on attempt \${attempt}; retrying...\"
|
||||
sleep 5
|
||||
done
|
||||
|
||||
echo \"\$NODE_BASE_IMAGE pull failed after retries\"
|
||||
return 1
|
||||
}
|
||||
|
||||
if [ ! -d '$TARGET_DIR/.git' ]; then
|
||||
mkdir -p '$TARGET_DIR'
|
||||
GIT_SSH_COMMAND='ssh -i ~/.ssh/id_ed25519 -p 222 -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes' git clone --branch '$TARGET_BRANCH' --single-branch '$REPO_URL' '$TARGET_DIR'
|
||||
fi
|
||||
|
||||
cd '$TARGET_DIR'
|
||||
|
||||
GIT_SSH_COMMAND='ssh -i ~/.ssh/id_ed25519 -p 222 -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes' git fetch origin '$TARGET_BRANCH'
|
||||
git reset --hard 'origin/$TARGET_BRANCH'
|
||||
|
||||
docker ps -q --filter publish=$PORT | xargs -r docker rm -f
|
||||
|
||||
docker compose -p '$PROJECT' -f docker-compose.yml -f '$COMPOSE_FILE' down --remove-orphans || true
|
||||
ensure_node_base_image
|
||||
docker compose -p '$PROJECT' -f docker-compose.yml -f '$COMPOSE_FILE' up -d --build
|
||||
"
|
||||
|
||||
for attempt in 1 2 3 4 5; do
|
||||
if curl --fail --silent --show-error --location --max-time 15 "$HEALTH_URL" >/dev/null; then
|
||||
echo "Health check passed on attempt $attempt"
|
||||
exit 0
|
||||
fi
|
||||
echo "Health check failed on attempt $attempt; retrying..."
|
||||
sleep 5
|
||||
done
|
||||
|
||||
echo "Health check failed after retries"
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user