name: Docker Build & Deploy on: push: branches: [ main, master ] tags: - 'v*' pull_request: branches: [ main, master ] jobs: build-and-test: name: Build Docker Images runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Build API image uses: docker/build-push-action@v5 with: context: . file: ./packages/api/Dockerfile push: false tags: basil-api:test cache-from: type=gha cache-to: type=gha,mode=max - name: Build Web image uses: docker/build-push-action@v5 with: context: . file: ./packages/web/Dockerfile push: false tags: basil-web:test cache-from: type=gha cache-to: type=gha,mode=max - name: Test Docker Compose run: | docker-compose -f docker-compose.yml config echo "✅ Docker Compose configuration is valid" push-images: name: Push Docker Images runs-on: ubuntu-latest needs: build-and-test if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')) steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to Container Registry uses: docker/login-action@v3 with: registry: ${{ secrets.DOCKER_REGISTRY }} username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Extract metadata id: meta run: | if [[ $GITHUB_REF == refs/tags/* ]]; then VERSION=${GITHUB_REF#refs/tags/} else VERSION=latest fi echo "version=$VERSION" >> $GITHUB_OUTPUT echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT - name: Build and push API image uses: docker/build-push-action@v5 with: context: . file: ./packages/api/Dockerfile push: true tags: | ${{ secrets.DOCKER_REGISTRY }}/basil-api:${{ steps.meta.outputs.version }} ${{ secrets.DOCKER_REGISTRY }}/basil-api:latest labels: | org.opencontainers.image.created=${{ steps.meta.outputs.date }} org.opencontainers.image.version=${{ steps.meta.outputs.version }} cache-from: type=gha cache-to: type=gha,mode=max - name: Build and push Web image uses: docker/build-push-action@v5 with: context: . file: ./packages/web/Dockerfile push: true tags: | ${{ secrets.DOCKER_REGISTRY }}/basil-web:${{ steps.meta.outputs.version }} ${{ secrets.DOCKER_REGISTRY }}/basil-web:latest labels: | org.opencontainers.image.created=${{ steps.meta.outputs.date }} org.opencontainers.image.version=${{ steps.meta.outputs.version }} cache-from: type=gha cache-to: type=gha,mode=max - name: Image digest run: echo "Images have been built and pushed successfully" deploy-staging: name: Deploy to Staging runs-on: ubuntu-latest needs: push-images if: github.ref == 'refs/heads/develop' environment: name: staging url: https://staging.basil.example.com steps: - name: Deploy to staging run: | echo "Deploying to staging environment..." echo "This is a placeholder for actual deployment steps." echo "Examples: SSH to server, run docker-compose pull, restart services, etc." deploy-production: name: Deploy to Production runs-on: ubuntu-latest needs: push-images if: startsWith(github.ref, 'refs/tags/v') environment: name: production url: https://basil.example.com steps: - name: Deploy to production run: | echo "Deploying to production environment..." echo "This is a placeholder for actual deployment steps." echo "Examples: SSH to server, run docker-compose pull, restart services, etc." - name: Create deployment summary run: | echo "# 🚀 Deployment Summary" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Version**: ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY echo "**Environment**: Production" >> $GITHUB_STEP_SUMMARY echo "**Status**: Deployed Successfully ✅" >> $GITHUB_STEP_SUMMARY