|
| 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 "dev" |
| 3 | +# branch. |
| 4 | + |
| 5 | +name: 'Build and Deploy to Cloud Run' |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - '"dev"' |
| 11 | + |
| 12 | +env: |
| 13 | + PROJECT_ID: 'my-project' # TODO: update to your Google Cloud project ID |
| 14 | + REGION: 'us-central1' # TODO: update to your region |
| 15 | + SERVICE: 'my-service' # TODO: update to your service name |
| 16 | + WORKLOAD_IDENTITY_PROVIDER: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' # TODO: update to your workload identity provider |
| 17 | + |
| 18 | +jobs: |
| 19 | + deploy: |
| 20 | + runs-on: 'ubuntu-latest' |
| 21 | + |
| 22 | + permissions: |
| 23 | + contents: 'read' |
| 24 | + id-token: 'write' |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: 'Checkout' |
| 28 | + uses: 'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332' # actions/checkout@v4 |
| 29 | + |
| 30 | + # Configure Workload Identity Federation and generate an access token. |
| 31 | + # |
| 32 | + # See https://github.com/google-github-actions/auth for more options, |
| 33 | + # including authenticating via a JSON credentials file. |
| 34 | + - id: 'auth' |
| 35 | + name: 'Authenticate to Google Cloud' |
| 36 | + uses: 'google-github-actions/auth@f112390a2df9932162083945e46d439060d66ec2' # google-github-actions/auth@v2 |
| 37 | + with: |
| 38 | + workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}' |
| 39 | + |
| 40 | + # BEGIN - Docker auth and build |
| 41 | + # |
| 42 | + # If you already have a container image, you can omit these steps. |
| 43 | + - name: 'Docker Auth' |
| 44 | + uses: 'docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567' # docker/login-action@v3 |
| 45 | + with: |
| 46 | + username: 'oauth2accesstoken' |
| 47 | + password: '${{ steps.auth.outputs.auth_token }}' |
| 48 | + registry: '${{ env.REGION }}-docker.pkg.dev' |
| 49 | + |
| 50 | + - name: 'Build and Push Container' |
| 51 | + run: |- |
| 52 | + DOCKER_TAG="$${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}" |
| 53 | + docker build --tag "${DOCKER_TAG}" . |
| 54 | + docker push "${DOCKER_TAG}" |
| 55 | + - name: 'Deploy to Cloud Run' |
| 56 | + |
| 57 | + # END - Docker auth and build |
| 58 | + |
| 59 | + uses: 'google-github-actions/deploy-cloudrun@33553064113a37d688aa6937bacbdc481580be17' # google-github-actions/deploy-cloudrun@v2 |
| 60 | + with: |
| 61 | + service: '${{ env.SERVICE }}' |
| 62 | + region: '${{ env.REGION }}' |
| 63 | + # NOTE: If using a pre-built image, update the image name below: |
| 64 | + |
| 65 | + image: '${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}' |
| 66 | + # If required, use the Cloud Run URL output in later steps |
| 67 | + - name: 'Show output' |
| 68 | + run: |2- |
| 69 | +
|
| 70 | + echo ${{ steps.deploy.outputs.url }} |
0 commit comments