api/.gitea/workflows/deploy.yml

97 lines
2.4 KiB
YAML

name: Laravel CI-CD
on:
push:
branches: ["main"]
env:
REGISTRY: gitea.leonmorival.com
IMAGE_NAME: daily_meal/bemeal-api
jobs:
# =========================
# TESTS
# =========================
test:
name: Tests Unitaires
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Tests
uses: docker://laravelsail/php84-composer:latest
env:
APP_ENV: testing
APP_KEY: base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
DB_CONNECTION: sqlite
DB_DATABASE: ":memory:"
with:
args: |
bash -c "composer install --no-interaction --ignore-platform-req=ext-intl && php artisan test"
# =========================
# BUILD & PUSH IMAGE
# =========================
build:
name: Build & Push Docker
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Login to Gitea Registry
run: |
echo "${{ secrets.TOKEN_GITEA }}" \
| docker login $REGISTRY \
-u "${{ github.actor }}" \
--password-stdin
- name: Build Docker image
run: |
IMAGE=$REGISTRY/$IMAGE_NAME:latest
docker build -t $IMAGE .
docker push $IMAGE
# =========================
# DEPLOY
# =========================
deploy:
name: Deploy Blue/Green
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy via SSH (Blue/Green)
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
set -e
echo "📦 Pull latest image"
docker pull gitea.leonmorival.com/daily_meal/bemeal-api:latest
echo "🟢 Start GREEN container"
docker compose up -d bemeal-api-green
echo "⏳ Wait for healthcheck"
sleep 10
curl -f http://localhost/api/health || exit 1
echo "🔁 Switch traffic (MANUAL STEP OR NPM API)"
sed -i 's/blue/green/g' /etc/nginx/conf.d/bemeal.conf
nginx -s reload
echo "🧹 Stop BLUE container"
docker stop bemeal-api-blue || true
docker rm bemeal-api-blue || true