-
Notifications
You must be signed in to change notification settings - Fork 0
123 lines (109 loc) · 4.43 KB
/
Copy pathdeploy.yml
File metadata and controls
123 lines (109 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
name: Deploy API
# Disparadores:
# - push a main: despliegue automatico
# - workflow_dispatch: permite lanzar el despliegue manualmente desde la UI
# de GitHub (util para forzar redeploy sin commitear nada)
on:
push:
branches: [main]
paths:
- 'GrowTogetherAPI/src/**'
- 'GrowTogetherAPI/build.gradle.kts'
- 'GrowTogetherAPI/Dockerfile'
- 'GrowTogetherAPI/.dockerignore'
- 'GrowTogetherAPI/src/main/resources/**'
- '.github/workflows/deploy.yml'
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: devpatuel/growtogetherapi
jobs:
# ---------------------------------------------------------------------------
# Job 1: build de la imagen Docker y push a GHCR.
# GHCR acepta el GITHUB_TOKEN automatico para push si tiene permiso
# packages:write. No hay que crear PAT para el push.
# ---------------------------------------------------------------------------
build-and-push:
name: Build y push a GHCR
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
# Buildx es la version moderna de docker build, soporta cache distribuido
# y multi-arquitectura. Lo necesitamos para cache-from/cache-to.
- name: Configurar Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login en GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build + push en un solo paso. Generamos dos tags:
# - :latest para que el deploy SSH pueda hacer pull simple
# - :sha-{commit} para versionado y rollback (queda permanente)
- name: Build y push imagen
uses: docker/build-push-action@v5
with:
context: ./GrowTogetherAPI
file: ./GrowTogetherAPI/Dockerfile
push: true
platforms: linux/amd64
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
# Cache distribuido en GitHub Actions: la primera build tarda 2-3 min,
# las siguientes solo segundos si no cambia el codigo.
cache-from: type=gha
cache-to: type=gha,mode=max
# ---------------------------------------------------------------------------
# Job 2: deploy a la EC2 via SSH. Solo se ejecuta si el job anterior pasa.
# ---------------------------------------------------------------------------
deploy:
name: Deploy a EC2
runs-on: ubuntu-latest
needs: build-and-push
steps:
- name: SSH a EC2 y redeploy
uses: appleboy/ssh-action@v1.2.0
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
set -e
cd /opt/growtogether
echo "===== PULL DE LA NUEVA IMAGEN ====="
docker pull ghcr.io/devpatuel/growtogetherapi:latest
echo "===== UP -D (recreate si la imagen es nueva) ====="
docker compose up -d
echo "===== ESPERANDO HEALTHCHECK ====="
for i in $(seq 1 30); do
status=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:8081/actuator/health || echo "000")
if [ "$status" = "200" ]; then
echo "Deploy OK tras ${i} intentos ($((i * 2))s)"
exit 0
fi
sleep 2
done
echo "Deploy FALLO: health no respondio 200 en 60s"
docker compose logs --tail=40 api
exit 1
- name: Limpiar imagenes antiguas en EC2 (mantener ultimas 3)
uses: appleboy/ssh-action@v1.2.0
# Esto se ejecuta aunque falle el deploy, para no acumular basura
# si hay deploys repetidos fallidos.
if: always()
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
# Borra imagenes dangling (huerfanas) y las mas antiguas de
# growtogetherapi conservando las 3 ultimas. Importante en
# t3.micro con disco al 79%.
docker image prune -f
docker images ghcr.io/devpatuel/growtogetherapi --format "{{.ID}} {{.CreatedAt}}" | sort -k2 -r | tail -n +4 | awk '{print $1}' | xargs -r docker rmi -f || true