Update gradle.yml #69
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD with Gradle, Docker Image & Docker Compose | ||
| on: | ||
| push: | ||
| branches: [ "main" ] | ||
| pull_request: | ||
| branches: [ "main" ] | ||
| permissions: | ||
| contents: read | ||
| jobs: | ||
| build-and-push-image: | ||
| name: Build & Push Docker Image | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v3 | ||
| - name: Set up JDK 17 | ||
| uses: actions/setup-java@v3 | ||
| with: | ||
| java-version: '17' | ||
| distribution: 'temurin' | ||
| - name: Grant execute permission for Gradle | ||
| run: chmod +x ./gradlew | ||
| - name: Build with Gradle | ||
| run: ./gradlew clean bootJar | ||
| - name: Build Docker Image | ||
| run: docker build -t ${{ secrets.DOCKER_USERNAME }}/fossistant:latest . | ||
| - name: Docker Hub Login | ||
| uses: docker/login-action@v2 | ||
| with: | ||
| username: ${{ secrets.DOCKER_USERNAME }} | ||
| password: ${{ secrets.DOCKER_PASSWORD }} | ||
| - name: Push Docker Image to Docker Hub | ||
| run: docker push ${{ secrets.DOCKER_USERNAME }}/fossistant:latest | ||
| deploy-with-compose: | ||
| name: Deploy to Azure VM via Docker Compose | ||
| runs-on: ubuntu-latest | ||
| needs: build-and-push-image | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v3 | ||
| - name: Setup SSH private key | ||
| run: | | ||
| echo "${{ secrets.SSH_KEY }}" > private_key.pem | ||
| chmod 600 private_key.pem | ||
| - name: Debug SSH connection | ||
| run: | | ||
| ssh -o StrictHostKeyChecking=no -i private_key.pem \ | ||
| ${{ secrets.AZURE_VM_USER }}@${{ secrets.AZURE_VM_HOST }} \ | ||
| "echo ✅ SSH 접속 성공!" | ||
| - name: Generate .env for Docker Compose | ||
| run: | | ||
| cat <<EOF > .env | ||
| AZURE_DB_USERNAME=${{ secrets.AZURE_DB_USERNAME }} | ||
| AZURE_DB_PASSWORD=${{ secrets.AZURE_DB_PASSWORD }} | ||
| GEMINI_KEY=${{ secrets.GEMINI_KEY }} | ||
| GITHUB_TOKEN=${{ secrets.HUB_TOKEN }} | ||
| JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }} | ||
| JWT_ACCESS_TOKEN_TIME=${{ secrets.JWT_ACCESS_TOKEN_TIME }} | ||
| JWT_REFRESH_TOKEN_TIME=${{ secrets.JWT_REFRESH_TOKEN_TIME }} | ||
| GITHUB_CLIENT_ID=${{ secrets.CLIENT_ID }} | ||
| GITHUB_CLIENT_SECRET=${{ secrets.CLIENT_SECRET }} | ||
| EOF | ||
| - name: Upload docker-compose.yml to Azure VM | ||
| run: | | ||
| scp -i private_key.pem -o StrictHostKeyChecking=no \ | ||
| docker-compose.yml \ | ||
| ${{ secrets.AZURE_VM_USER }}@${{ secrets.AZURE_VM_HOST }}:/home/ubuntu/fossistant/docker-compose.yml | ||
| - name: Upload .env to Azure VM | ||
| run: | | ||
| scp -i private_key.pem -o StrictHostKeyChecking=no \ | ||
| .env \ | ||
| ${{ secrets.AZURE_VM_USER }}@${{ secrets.AZURE_VM_HOST }}:/home/ubuntu/fossistant/.env | ||
| - name: SSH to Azure VM and Deploy with Compose | ||
| uses: appleboy/ssh-action@v0.1.3 | ||
| with: | ||
| host: ${{ secrets.AZURE_VM_HOST }} | ||
| username: ${{ secrets.AZURE_VM_USER }} | ||
| key: ${{ secrets.SSH_KEY }} | ||
| port: 22 | ||
| script: | | ||
| set -eux | ||
| cd /home/ubuntu/fossistant | ||
| echo "🛑 Stopping host's Redis service if running..." | ||
| sudo systemctl stop redis-server || true | ||
| echo "🧼 Killing any process listening on port 6379..." | ||
| sudo fuser -k 6379/tcp || true | ||
| echo "🧼 Forcibly removing any existing containers named 'redis' or 'fossistant'..." | ||
| docker rm -f redis fossistant || true | ||
| echo "🧹 Stopping old containers and removing orphans..." | ||
| sudo docker compose down --remove-orphans || true | ||
| sudo docker compose rm -f || true | ||
| echo "✅ Pulling latest images from Docker Hub..." | ||
| sudo docker compose pull | ||
| echo "🚀 Starting new containers with Docker Compose..." | ||
| sudo docker compose up -d --build | ||
| echo "🎉 Deployment complete! Current containers:" | ||
| docker ps | ||