google-cloudrun-docker.yml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # This workflow build and push a Docker container to Google Artifact Registry and deploy it on Cloud Run when a commit is pushed to the master branch
  2. #
  3. # Overview:
  4. #
  5. # 1. Authenticate to Google Cloud
  6. # 2. Authenticate Docker to Artifact Registry
  7. # 3. Build a docker container
  8. # 4. Publish it to Google Artifact Registry
  9. # 5. Deploy it to Cloud Run
  10. #
  11. # To configure this workflow:
  12. #
  13. # 1. Ensure the required Google Cloud APIs are enabled:
  14. #
  15. # Cloud Run run.googleapis.com
  16. # Artifact Registry artifactregistry.googleapis.com
  17. #
  18. # 2. Create and configure Workload Identity Federation for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
  19. #
  20. # 3. Ensure the required IAM permissions are granted
  21. #
  22. # Cloud Run
  23. # roles/run.admin
  24. # roles/iam.serviceAccountUser (to act as the Cloud Run runtime service account)
  25. #
  26. # Artifact Registry
  27. # roles/artifactregistry.admin (project or repository level)
  28. #
  29. # NOTE: You should always follow the principle of least privilege when assigning IAM roles
  30. #
  31. # 4. Create GitHub secrets for WIF_PROVIDER and WIF_SERVICE_ACCOUNT
  32. #
  33. # 5. Change the values for the GAR_LOCATION, SERVICE and REGION environment variables (below).
  34. #
  35. # NOTE: To use Google Container Registry instead, replace ${{ env.GAR_LOCATION }}-docker.pkg.dev with gcr.io
  36. #
  37. # For more support on how to run this workflow, please visit https://github.com/marketplace/actions/deploy-to-cloud-run
  38. #
  39. # Further reading:
  40. # Cloud Run IAM permissions - https://cloud.google.com/run/docs/deploying
  41. # Artifact Registry IAM permissions - https://cloud.google.com/artifact-registry/docs/access-control#roles
  42. # Container Registry vs Artifact Registry - https://cloud.google.com/blog/products/application-development/understanding-artifact-registry-vs-container-registry
  43. # Principle of least privilege - https://cloud.google.com/blog/products/identity-security/dont-get-pwned-practicing-the-principle-of-least-privilege
  44. name: Build and Deploy to Cloud Run
  45. on:
  46. push:
  47. branches:
  48. - master
  49. env:
  50. PROJECT_ID: sunfmin
  51. GAR_LOCATION: gcr.io
  52. SERVICE: goplaid-docs
  53. REGION: us-central1
  54. jobs:
  55. deploy:
  56. # Add 'id-token' with the intended permissions for workload identity federation
  57. permissions:
  58. contents: 'read'
  59. id-token: 'write'
  60. runs-on: ubuntu-latest
  61. steps:
  62. - name: Checkout
  63. uses: actions/checkout@v2
  64. - name: Google Auth
  65. id: auth
  66. uses: 'google-github-actions/auth@v0'
  67. with:
  68. token_format: 'access_token'
  69. workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
  70. service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - my-service-account@my-project.iam.gserviceaccount.com
  71. # NOTE: Alternative option - authentication via credentials json
  72. # - name: Google Auth
  73. # id: auth
  74. # uses: 'google-github-actions/auth@v0'
  75. # with:
  76. # credentials_json: '${{ secrets.GCP_CREDENTIALS }}''
  77. # BEGIN - Docker auth and build (NOTE: If you already have a container image, these Docker steps can be omitted)
  78. # Authenticate Docker to Google Cloud Artifact Registry
  79. - name: Docker Auth
  80. id: docker-auth
  81. uses: 'docker/login-action@v1'
  82. with:
  83. username: 'oauth2accesstoken'
  84. password: '${{ steps.auth.outputs.access_token }}'
  85. registry: 'gcr.io'
  86. - name: Build and Push Container
  87. run: |-
  88. docker build -t "gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}" -f ./docs/Dockerfile .
  89. docker push "gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}"
  90. # END - Docker auth and build
  91. - name: Deploy to Cloud Run
  92. id: deploy
  93. uses: google-github-actions/deploy-cloudrun@v0
  94. with:
  95. service: ${{ env.SERVICE }}
  96. region: ${{ env.REGION }}
  97. # NOTE: If using a pre-built image, update the image name here
  98. image: gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}
  99. # If required, use the Cloud Run url output in later steps
  100. - name: Show Output
  101. run: echo ${{ steps.deploy.outputs.url }}