name: Deploy Webzine on: push: branches: - main - dev jobs: # ───────────────────────────────────────────── # BUILD — 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 }} - 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 }} # ───────────────────────────────────────────── # DEPLOY — Machine de PRODUCTION (branche main) # ───────────────────────────────────────────── deploy-production: name: Deploy to Production needs: build if: gitea.ref_name == 'main' runs-on: ubuntu-latest steps: - name: Deploy via SSH to PRODUCTION server uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.PROD_SSH_HOST }} username: ${{ secrets.PROD_SSH_USER }} key: ${{ secrets.PROD_SSH_KEY }} port: ${{ secrets.PROD_SSH_PORT || 22 }} script: | set -e echo "=== [PROD] Pulling image ===" docker login ${{ vars.REGISTRY_URL }} \ -u ${{ secrets.REGISTRY_USERNAME }} \ -p ${{ secrets.REGISTRY_PASSWORD }} docker pull ${{ vars.REGISTRY_URL }}/webzine/webzine:latest echo "=== [PROD] Stopping old container ===" docker stop webzine-prod 2>/dev/null || true docker rm webzine-prod 2>/dev/null || true echo "=== [PROD] Starting new container ===" 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] Cleaning up old images ===" docker image prune -f echo "=== [PROD] Deployment complete ===" # ───────────────────────────────────────────── # DEPLOY — Machine de DÉVELOPPEMENT (branche dev) # ───────────────────────────────────────────── deploy-development: name: Deploy to Development needs: build if: gitea.ref_name == 'dev' runs-on: ubuntu-latest steps: - name: Deploy via SSH to DEVELOPMENT server uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.DEV_SSH_HOST }} username: ${{ secrets.DEV_SSH_USER }} key: ${{ secrets.DEV_SSH_KEY }} port: ${{ secrets.DEV_SSH_PORT || 22 }} script: | set -e echo "=== [DEV] Pulling image ===" docker login ${{ vars.REGISTRY_URL }} \ -u ${{ secrets.REGISTRY_USERNAME }} \ -p ${{ secrets.REGISTRY_PASSWORD }} docker pull ${{ vars.REGISTRY_URL }}/webzine/webzine:dev echo "=== [DEV] Stopping old container ===" docker stop webzine-dev 2>/dev/null || true docker rm webzine-dev 2>/dev/null || true echo "=== [DEV] Starting new container ===" 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] Cleaning up old images ===" docker image prune -f echo "=== [DEV] Deployment complete ==="