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 # ========================= build: name: Build & Push Docker needs: test runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Login to Gitea Registry run: | echo "${{ secrets.TOKEN_GITEA }}" \ | docker login $REGISTRY \ -u "${{ github.actor }}" \ --password-stdin - name: Build & Push image run: | IMAGE=$REGISTRY/$IMAGE_NAME docker build -t "$IMAGE:${{ github.sha }}" -t "$IMAGE:latest" . docker push "$IMAGE:${{ github.sha }}" docker push "$IMAGE:latest" # ========================= # DEPLOY (SIMPLE) # ========================= deploy: name: Deploy needs: build runs-on: ubuntu-latest steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1.0.3 env: IMAGE_TAG: ${{ github.sha }} REGISTRY: gitea.leonmorival.com IMAGE_NAME: daily_meal/bemeal-api with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} envs: IMAGE_TAG,REGISTRY,IMAGE_NAME script: | set -eu APP_DIR="/opt/bemeal" cd "$APP_DIR" COMPOSE="docker compose \ --project-directory $APP_DIR \ --env-file $APP_DIR/.env.prod \ -f $APP_DIR/docker-compose.prod.yml" IMAGE="$REGISTRY/$IMAGE_NAME:$IMAGE_TAG" echo "Pull image" docker pull "$IMAGE" echo "Start infra services" $COMPOSE up -d pgsql redis meilisearch echo "Deploy API (blue/green handled by Caddy)" IMAGE="$IMAGE" $COMPOSE up -d --no-deps --force-recreate bemeal-api-blue IMAGE="$IMAGE" $COMPOSE up -d --no-deps --force-recreate bemeal-api-green echo "Deployment done"