|
| 1 | +# This workflow build and push a Docker container to Google Artifact Registry |
| 2 | +# and deploy it on Cloud Run when a commit is pushed to the "main" |
| 3 | +# branch. |
| 4 | +# |
| 5 | +# To configure this workflow: |
| 6 | +# |
| 7 | +# 1. Enable the following Google Cloud APIs: |
| 8 | +# |
| 9 | +# - Artifact Registry (artifactregistry.googleapis.com) |
| 10 | +# - Cloud Run (run.googleapis.com) |
| 11 | +# - IAM Credentials API (iamcredentials.googleapis.com) |
| 12 | +# |
| 13 | +# You can learn more about enabling APIs at |
| 14 | +# https://support.google.com/googleapi/answer/6158841. |
| 15 | +# |
| 16 | +# 2. Create and configure a Workload Identity Provider for GitHub: |
| 17 | +# https://github.com/google-github-actions/auth#preferred-direct-workload-identity-federation. |
| 18 | +# |
| 19 | +# Depending on how you authenticate, you will need to grant an IAM principal |
| 20 | +# permissions on Google Cloud: |
| 21 | +# |
| 22 | +# - Artifact Registry Administrator (roles/artifactregistry.admin) |
| 23 | +# - Cloud Run Developer (roles/run.developer) |
| 24 | +# |
| 25 | +# You can learn more about setting IAM permissions at |
| 26 | +# https://cloud.google.com/iam/docs/manage-access-other-resources |
| 27 | +# |
| 28 | +# 3. Change the values in the "env" block to match your values. |
| 29 | + |
| 30 | +name: 'Build and Deploy to Cloud Run' |
| 31 | + |
| 32 | +on: |
| 33 | + push: |
| 34 | + branches: |
| 35 | + - main |
| 36 | + paths: |
| 37 | + - 'screener-api/**' |
| 38 | + |
| 39 | +env: |
| 40 | + PROJECT_ID: 'benefit-decision-toolkit-play' |
| 41 | + REGION: 'us-central1' |
| 42 | + SERVICE: 'benefit-decision-toolkit-play' |
| 43 | + API_NAME: 'screener-api' |
| 44 | + WORKLOAD_IDENTITY_PROVIDER: 'projects/1034049717668/locations/global/workloadIdentityPools/github-actions-google-cloud/providers/github' |
| 45 | + |
| 46 | +jobs: |
| 47 | + deploy: |
| 48 | + runs-on: 'ubuntu-latest' |
| 49 | + |
| 50 | + permissions: |
| 51 | + contents: 'read' |
| 52 | + id-token: 'write' |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: 'Checkout' |
| 56 | + uses: 'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332' # actions/checkout@v4 |
| 57 | + |
| 58 | + |
| 59 | + # Configure Workload Identity Federation and generate an access token. |
| 60 | + # |
| 61 | + # See https://github.com/google-github-actions/auth for more options, |
| 62 | + # including authenticating via a JSON credentials file. |
| 63 | + - id: 'auth' |
| 64 | + name: 'Authenticate to Google Cloud' |
| 65 | + uses: 'google-github-actions/auth@v2' |
| 66 | + with: |
| 67 | + workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}' |
| 68 | + service_account: cicd-build-deploy-api@benefit-decision-toolkit-play.iam.gserviceaccount.com |
| 69 | + project_id: ${{ env.PROJECT_ID }} |
| 70 | + |
| 71 | + - name: 'Set up Cloud SDK' |
| 72 | + uses: 'google-github-actions/setup-gcloud@v2' |
| 73 | + |
| 74 | + # BEGIN - Docker auth and build |
| 75 | + |
| 76 | + # Configure Docker to use gcloud as a credential helper |
| 77 | + - name: 'Configure Docker' |
| 78 | + run: | |
| 79 | + gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev |
| 80 | +
|
| 81 | + # Download Java version |
| 82 | + - name: Set up Java 21 |
| 83 | + uses: actions/setup-java@v3 |
| 84 | + with: |
| 85 | + distribution: temurin |
| 86 | + java-version: 21 |
| 87 | + |
| 88 | + # Build the Quarkus app with Maven |
| 89 | + - name: 'Build Quarkus App' |
| 90 | + working-directory: screener-api |
| 91 | + run: | |
| 92 | + ./mvnw package -DskipTests |
| 93 | +
|
| 94 | + - name: 'Build and Push Container' |
| 95 | + working-directory: screener-api |
| 96 | + run: |- |
| 97 | + DOCKER_TAG="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}/${{ env.API_NAME }}:latest" |
| 98 | + docker build -f src/main/docker/Dockerfile.jvm --tag "${DOCKER_TAG}" . |
| 99 | + docker push "${DOCKER_TAG}" |
| 100 | +
|
| 101 | + - name: 'Deploy to Cloud Run' |
| 102 | + |
| 103 | + # END - Docker auth and build |
| 104 | + |
| 105 | + uses: 'google-github-actions/deploy-cloudrun@33553064113a37d688aa6937bacbdc481580be17' # google-github-actions/deploy-cloudrun@v2 |
| 106 | + with: |
| 107 | + service: '${{ env.API_NAME }}' |
| 108 | + region: '${{ env.REGION }}' |
| 109 | + # NOTE: If using a pre-built image, update the image name below: |
| 110 | + |
| 111 | + image: '${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}/${{ env.API_NAME }}:latest' |
| 112 | + service_account: 'screener-api-service-account@${{ env.PROJECT_ID }}.iam.gserviceaccount.com' |
| 113 | + flags: '--allow-unauthenticated --max-instances=2' |
| 114 | + |
| 115 | + # If required, use the Cloud Run URL output in later steps |
| 116 | + - name: 'Show output' |
| 117 | + run: |2- |
| 118 | +
|
| 119 | + echo ${{ steps.deploy.outputs.url }} |
0 commit comments