generated from hjess/PythonTemplateProject
150 lines
4.1 KiB
YAML
150 lines
4.1 KiB
YAML
name: Build and Deploy LifeFAQ
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
build-image:
|
|
runs-on: debian-host
|
|
|
|
env:
|
|
PATH: /usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:/snap/bin
|
|
DOCKER_HOST: unix:///var/run/docker.sock
|
|
BUILDX_CONFIG: /tmp/buildx
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: System info
|
|
run: |
|
|
uname -a
|
|
whoami
|
|
|
|
- name: Set up Docker Context for Buildx
|
|
id: buildx-context
|
|
run: |
|
|
export DOCKER_HOST=tcp://docker:2376/
|
|
export DOCKER_TLS_VERIFY=0
|
|
docker context rm builders || true
|
|
docker context create builders
|
|
|
|
- name: Verify Docker
|
|
run: docker --version
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
env:
|
|
PATH: /usr/bin:/usr/local/bin:/bin:/sbin:/usr/sbin
|
|
|
|
- name: Log in to Docker Registry
|
|
run: |
|
|
echo "${{ secrets.HARBOR_ROBOT_TOKEN }}" | docker login registry.i80.dk -u "robot\$gitserver" --password-stdin
|
|
env:
|
|
PATH: /usr/bin:/usr/local/bin:/bin:/sbin:/usr/sbin
|
|
|
|
- name: Check for changes
|
|
id: changes
|
|
uses: dorny/paths-filter@v2
|
|
with:
|
|
filters: |
|
|
docker:
|
|
- 'Dockerfile'
|
|
- 'app/**'
|
|
- 'requirements.txt'
|
|
|
|
- name: Build and push Docker image
|
|
if: steps.changes.outputs.docker == 'true'
|
|
uses: docker/build-push-action@v5
|
|
env:
|
|
PATH: /usr/bin:/usr/local/bin:/bin:/sbin:/usr/sbin
|
|
with:
|
|
context: .
|
|
file: ./Dockerfile
|
|
push: true
|
|
tags: |
|
|
registry.i80.dk/gitea/lifefaq:latest
|
|
|
|
- name: Test container health
|
|
run: |
|
|
echo "=== Starting container for health check ==="
|
|
|
|
docker pull registry.i80.dk/gitea/lifefaq:latest
|
|
|
|
CONTAINER_ID=$(docker run -d \
|
|
-p 8000:8000 \
|
|
-e PORT=8000 \
|
|
-e APP_ENV=production \
|
|
--name lifefaq-test \
|
|
registry.i80.dk/gitea/lifefaq:latest)
|
|
|
|
echo "Container started: ${CONTAINER_ID}"
|
|
|
|
echo "Waiting for /health endpoint..."
|
|
SUCCESS=false
|
|
for i in {1..90}; do
|
|
if curl -f -s http://localhost:8000/health > /dev/null 2>&1; then
|
|
echo "✓ Health check passed after ${i} seconds"
|
|
curl -s http://localhost:8000/health | jq '.' || echo "Health endpoint returned OK"
|
|
SUCCESS=true
|
|
break
|
|
fi
|
|
echo "Attempt ${i}/90 - waiting..."
|
|
sleep 1
|
|
done
|
|
|
|
echo "=== Container Logs ==="
|
|
docker logs lifefaq-test
|
|
|
|
docker stop lifefaq-test
|
|
docker rm lifefaq-test
|
|
|
|
if [ "$SUCCESS" = false ]; then
|
|
echo "✗ Health check failed after 90 seconds"
|
|
exit 1
|
|
fi
|
|
|
|
echo "✓ Container health check passed - safe to deploy"
|
|
env:
|
|
PATH: /usr/bin:/usr/local/bin:/bin:/sbin:/usr/sbin
|
|
|
|
- name: Deploy to Nomad
|
|
run: |
|
|
nomad job validate lifefaq.nomad
|
|
nomad job run lifefaq.nomad
|
|
env:
|
|
NOMAD_ADDR: "https://nomad.i80.dk:4646"
|
|
|
|
- name: Wait for deployment
|
|
run: |
|
|
echo "Checking deployment status..."
|
|
nomad job status lifefaq
|
|
|
|
echo "=== Allocation Details ==="
|
|
nomad job allocs lifefaq
|
|
|
|
echo "=== Getting logs from allocations ==="
|
|
for alloc in $(nomad job allocs -all lifefaq | tail -n +2 | awk '{print $1}'); do
|
|
echo "Logs for allocation $alloc:"
|
|
|
|
timeout=250
|
|
SECONDS=0
|
|
until nomad alloc logs "$alloc" 2>/dev/null || [ $SECONDS -gt $timeout ]; do
|
|
echo "Waiting for allocation to start... ($SECONDS/$timeout seconds)"
|
|
sleep 5
|
|
done
|
|
|
|
[ $SECONDS -gt $timeout ] && echo "Timeout for $alloc"
|
|
echo "---"
|
|
done
|
|
env:
|
|
NOMAD_ADDR: "https://nomad.i80.dk:4646"
|
|
|
|
- name: Notify deployment status
|
|
run: |
|
|
echo "✅ Deployment completed!"
|
|
echo "LifeFAQ should be available at: https://lifefaq.i80.dk"
|
|
echo "Health check endpoint: https://lifefaq.i80.dk/health" |