|
| 1 | +# This workflow will deploy source code on Cloud Run when a commit is pushed to the "main" branch |
| 2 | +# |
| 3 | +# Overview: |
| 4 | +# |
| 5 | +# 1. Authenticate to Google Cloud |
| 6 | +# 2. Deploy it to Cloud Run |
| 7 | +# |
| 8 | +# To configure this workflow: |
| 9 | +# |
| 10 | +# 1. Ensure the required Google Cloud APIs are enabled: |
| 11 | +# |
| 12 | +# Cloud Run run.googleapis.com |
| 13 | +# Cloud Build cloudbuild.googleapis.com |
| 14 | +# Artifact Registry artifactregistry.googleapis.com |
| 15 | +# |
| 16 | +# 2. Create and configure Workload Identity Federation for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation) |
| 17 | +# |
| 18 | +# 3. Ensure the required IAM permissions are granted |
| 19 | +# |
| 20 | +# Cloud Run |
| 21 | +# roles/run.admin |
| 22 | +# roles/iam.serviceAccountUser (to act as the Cloud Run runtime service account) |
| 23 | +# |
| 24 | +# Cloud Build |
| 25 | +# roles/cloudbuild.builds.editor |
| 26 | +# |
| 27 | +# Cloud Storage |
| 28 | +# roles/storage.objectAdmin |
| 29 | +# |
| 30 | +# Artifact Registry |
| 31 | +# roles/artifactregistry.admin (project or repository level) |
| 32 | +# |
| 33 | +# NOTE: You should always follow the principle of least privilege when assigning IAM roles |
| 34 | +# |
| 35 | +# 4. Create GitHub secrets for WIF_PROVIDER and WIF_SERVICE_ACCOUNT |
| 36 | +# |
| 37 | +# 5. Change the values for the SERVICE and REGION environment variables (below). |
| 38 | +# |
| 39 | +# For more support on how to run this workflow, please visit https://github.com/marketplace/actions/deploy-to-cloud-run |
| 40 | +# |
| 41 | +# Further reading: |
| 42 | +# Cloud Run runtime service account - https://cloud.google.com/run/docs/securing/service-identity |
| 43 | +# Cloud Run IAM permissions - https://cloud.google.com/run/docs/deploying-source-code#permissions_required_to_deploy |
| 44 | +# Cloud Run builds from source - https://cloud.google.com/run/docs/deploying-source-code |
| 45 | +# Principle of least privilege - https://cloud.google.com/blog/products/identity-security/dont-get-pwned-practicing-the-principle-of-least-privilege |
| 46 | + |
| 47 | +name: Deploy to Cloud Run from Source |
| 48 | + |
| 49 | +on: |
| 50 | + push: |
| 51 | + branches: [ "main" ] |
| 52 | + |
| 53 | +env: |
| 54 | + PROJECT_ID: dev-project-305220 # TODO: update Google Cloud project id |
| 55 | + SERVICE: helloworld # TODO: update Cloud Run service name |
| 56 | + REGION: us-central1 # TODO: update Cloud Run service region |
| 57 | + |
| 58 | +jobs: |
| 59 | + deploy: |
| 60 | + # Add 'id-token' with the intended permissions for workload identity federation |
| 61 | + permissions: |
| 62 | + contents: 'read' |
| 63 | + id-token: 'write' |
| 64 | + |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@v2 |
| 69 | + |
| 70 | + - name: Google Auth |
| 71 | + id: auth |
| 72 | + uses: 'google-github-actions/auth@v0' |
| 73 | + with: |
| 74 | + workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider |
| 75 | + service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - my-service-account@my-project.iam.gserviceaccount.com |
| 76 | + |
| 77 | + # NOTE: Alternative option - authentication via credentials json |
| 78 | + # - name: Google Auth |
| 79 | + # id: auth |
| 80 | + # uses: 'google-github-actions/auth@v0' |
| 81 | + # with: |
| 82 | + # credentials_json: '${{ secrets.GCP_CREDENTIALS }}' |
| 83 | + |
| 84 | + - name: Deploy to Cloud Run |
| 85 | + id: deploy |
| 86 | + uses: google-github-actions/deploy-cloudrun@v0 |
| 87 | + with: |
| 88 | + service: ${{ env.SERVICE }} |
| 89 | + region: ${{ env.REGION }} |
| 90 | + # NOTE: If required, update to the appropriate source folder |
| 91 | + source: run/hello-world/ |
| 92 | + |
| 93 | + # If required, use the Cloud Run url output in later steps |
| 94 | + - name: Show Output |
| 95 | + run: echo ${{ steps.deploy.outputs.url }} |
0 commit comments