From 8bcc47e4603a2e66212b26e944f02913c8192b49 Mon Sep 17 00:00:00 2001 From: Soeun Uhm Date: Thu, 22 Feb 2024 20:23:04 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat=20:=20add=20github=20actions=20fo?= =?UTF-8?q?r=20cloud=20run?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/google-cloud-run.yml | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 .github/workflows/google-cloud-run.yml diff --git a/.github/workflows/google-cloud-run.yml b/.github/workflows/google-cloud-run.yml new file mode 100644 index 0000000..aec42c6 --- /dev/null +++ b/.github/workflows/google-cloud-run.yml @@ -0,0 +1,63 @@ +name: Build and Deploy to GKE + +on: + push: + branches: [ "main" ] + +env: + PROJECT_ID: ${{ secrets.GKE_PROJECT }} + GAR_LOCATION: asia-northeast3 + SERVICE_NAME: lookncook-service + REPOSITORY: gcr.io/solution-challenge-lookncook + IMAGE: lookncook-app-server +jobs: + setup-build-publish-deploy: + name: Setup, Build, Publish, and Deploy + runs-on: ubuntu-latest + environment: production + + permissions: + contents: 'read' + id-token: 'write' + + steps: + - name: Checkout + uses: actions/checkout@v3 + + # Configure Workload Identity Federation and generate an access token. + - id: 'auth' + name: 'Authenticate to Google Cloud' + uses: 'google-github-actions/auth@v0' + with: + credentials_json: '${{ secrets.GCP_CREDENTIALS }}' + + + - name: Docker configuration + run: |- + echo ${{steps.auth.outputs.access_token}} | docker login -u oauth2accesstoken --password-stdin https://$GAR_LOCATION-docker.pkg.dev + + + # Build the Docker image + - name: Build + run: |- + docker build \ + --tag "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" \ + --build-arg GITHUB_SHA="$GITHUB_SHA" \ + --build-arg GITHUB_REF="$GITHUB_REF" \ + . + # Push the Docker image to Google Artifact Registry + - name: Publish + run: |- + docker push "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" + + # Deploy the Docker image to Google Cloud Run + - name: Deploy to Cloud Run + run: |- + gcloud run deploy $SERVICE_NAME \ + --image="$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" \ + --region="$GAR_LOCATION" \ + --platform=managed \ + --allow-unauthenticated + env: + CLOUDSDK_CORE_PROJECT: ${{ env.PROJECT_ID }} + GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_SA_KEY }}