feat: deploy backend through gcp #25
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: Build and Deploy to GCP Compute Engine | |
| on: | |
| push: | |
| branches: [ "main", "cicdtest" ] | |
| env: | |
| GCP_PROJECT_ID: ${{ vars.GCP_PROJECT_ID }} | |
| GCP_REGION: "asia-northeast3" | |
| ARTIFACT_REGISTRY_REPO: "backend-server-repo" | |
| IMAGE_NAME: "spring-app" | |
| COMPUTE_INSTANCE: "lesson-backend" | |
| COMPUTE_ZONE: "asia-northeast3-a" | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - name: Code Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: "21" | |
| distribution: "temurin" | |
| cache: gradle | |
| - name: Build Spring Boot Application | |
| run: ./gradlew build -x test | |
| - name: Authenticate to Google Cloud | |
| uses: google-github-actions/auth@v2 | |
| with: | |
| workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }} | |
| service_account: ${{ vars.GCP_SERVICE_ACCOUNT }} | |
| - name: Configure Docker for GCP | |
| run: gcloud auth configure-docker $GCP_REGION-docker.pkg.dev | |
| - name: Build and Push Docker Image | |
| run: | | |
| docker build -t $GCP_REGION-docker.pkg.dev/$GCP_PROJECT_ID/$ARTIFACT_REGISTRY_REPO/$IMAGE_NAME:latest . | |
| docker push $GCP_REGION-docker.pkg.dev/$GCP_PROJECT_ID/$ARTIFACT_REGISTRY_REPO/$IMAGE_NAME:latest | |
| - name: Deploy to GCP Compute Engine | |
| run: | | |
| gcloud compute instances reset $COMPUTE_INSTANCE --zone=$COMPUTE_ZONE |