name: Deploy Webzine on: push: branches: - main - dev jobs: # ───────────────────────────────────────────── # COMPILATION — commun aux deux branches # ───────────────────────────────────────────── build: name: Build & Push Docker Image runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 # Le tag d'image dépend de la branche : # main → webzine:latest # dev → webzine:dev - name: Set image tag id: vars run: | if [ "${{ gitea.ref_name }}" = "main" ]; then echo "IMAGE_TAG=latest" >> $GITHUB_OUTPUT echo "ENV_LABEL=production" >> $GITHUB_OUTPUT else echo "IMAGE_TAG=dev" >> $GITHUB_OUTPUT echo "ENV_LABEL=development" >> $GITHUB_OUTPUT fi - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 # Connexion au registry Gitea intégré - name: Log in to Gitea Container Registry uses: docker/login-action@v3 with: registry: ${{ vars.REGISTRY_URL }} username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_PASSWORD }} # Construction et publication de l'image Docker - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . file: ./Webzine.WebApplication/Dockerfile push: true tags: ${{ vars.REGISTRY_URL }}/webzine/webzine:${{ steps.vars.outputs.IMAGE_TAG }} cache-from: type=registry,ref=${{ vars.REGISTRY_URL }}/webzine/webzine:buildcache-${{ steps.vars.outputs.IMAGE_TAG }} cache-to: type=registry,ref=${{ vars.REGISTRY_URL }}/webzine/webzine:buildcache-${{ steps.vars.outputs.IMAGE_TAG }},mode=max outputs: image_tag: ${{ steps.vars.outputs.IMAGE_TAG }} env_label: ${{ steps.vars.outputs.ENV_LABEL }} # ───────────────────────────────────────────── # DÉPLOIEMENT — Serveur de PRODUCTION (branche main) # ───────────────────────────────────────────── deploy-production: name: Deploy to Production needs: build if: gitea.ref_name == 'main' runs-on: self-hosted steps: - name: Deploy on PRODUCTION server run: | set -e echo "=== [PROD] Récupération de l'image ===" docker login ${{ vars.REGISTRY_URL }} \ -u ${{ secrets.REGISTRY_USERNAME }} \ -p ${{ secrets.REGISTRY_PASSWORD }} docker pull ${{ vars.REGISTRY_URL }}/webzine/webzine:latest echo "=== [PROD] Arrêt de l'ancien conteneur ===" docker stop webzine-prod 2>/dev/null || true docker rm webzine-prod 2>/dev/null || true echo "=== [PROD] Démarrage du nouveau conteneur ===" docker run -d \ --name webzine-prod \ --restart unless-stopped \ -p 80:8080 \ -p 443:8081 \ -v /opt/webzine/prod/data:/app/Data \ -v /opt/webzine/prod/logs:/Logs \ -e ASPNETCORE_ENVIRONMENT=Production \ ${{ vars.REGISTRY_URL }}/webzine/webzine:latest echo "=== [PROD] Nettoyage des anciennes images ===" docker image prune -f echo "=== [PROD] Déploiement terminé ===" # ───────────────────────────────────────────── # DÉPLOIEMENT — Serveur de DÉVELOPPEMENT (branche dev) # ───────────────────────────────────────────── deploy-development: name: Deploy to Development needs: build if: gitea.ref_name == 'dev' runs-on: self-hosted steps: - name: Deploy on DEVELOPMENT server run: | set -e echo "=== [DEV] Récupération de l'image ===" docker login ${{ vars.REGISTRY_URL }} \ -u ${{ secrets.REGISTRY_USERNAME }} \ -p ${{ secrets.REGISTRY_PASSWORD }} docker pull ${{ vars.REGISTRY_URL }}/webzine/webzine:dev echo "=== [DEV] Arrêt de l'ancien conteneur ===" docker stop webzine-dev 2>/dev/null || true docker rm webzine-dev 2>/dev/null || true echo "=== [DEV] Démarrage du nouveau conteneur ===" docker run -d \ --name webzine-dev \ --restart unless-stopped \ -p 8080:8080 \ -v /opt/webzine/dev/data:/app/Data \ -v /opt/webzine/dev/logs:/Logs \ -e ASPNETCORE_ENVIRONMENT=Development \ ${{ vars.REGISTRY_URL }}/webzine/webzine:dev echo "=== [DEV] Nettoyage des anciennes images ===" docker image prune -f echo "=== [DEV] Déploiement terminé ==="