google: update workflow versions and instructions (#2478)

* google: update workflow versions and instructions

* Pin hashes
This commit is contained in:
Seth Vargo
2024-08-21 11:21:42 -07:00
committed by GitHub
parent 91fe144014
commit ae01bb2a2f
3 changed files with 197 additions and 210 deletions
+60 -78
View File
@@ -1,113 +1,95 @@
# 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 $default-branch branch
#
# Overview:
#
# 1. Authenticate to Google Cloud
# 2. Authenticate Docker to Artifact Registry
# 3. Build a docker container
# 4. Publish it to Google Artifact Registry
# 5. Deploy it to Cloud Run
# 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 $default-branch
# branch.
#
# To configure this workflow:
#
# 1. Ensure the required Google Cloud APIs are enabled:
# 1. Enable the following Google Cloud APIs:
#
# Cloud Run run.googleapis.com
# Artifact Registry artifactregistry.googleapis.com
# - Artifact Registry (artifactregistry.googleapis.com)
# - Cloud Run (run.googleapis.com)
# - IAM Credentials API (iamcredentials.googleapis.com)
#
# 2. Create and configure Workload Identity Federation for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
# You can learn more about enabling APIs at
# https://support.google.com/googleapi/answer/6158841.
#
# 3. Ensure the required IAM permissions are granted
# 2. Create and configure a Workload Identity Provider for GitHub:
# https://github.com/google-github-actions/auth#preferred-direct-workload-identity-federation.
#
# Cloud Run
# roles/run.admin
# roles/iam.serviceAccountUser (to act as the Cloud Run runtime service account)
# Depending on how you authenticate, you will need to grant an IAM principal
# permissions on Google Cloud:
#
# Artifact Registry
# roles/artifactregistry.admin (project or repository level)
# - Artifact Registry Administrator (roles/artifactregistry.admin)
# - Cloud Run Developer (roles/run.developer)
#
# NOTE: You should always follow the principle of least privilege when assigning IAM roles
# You can learn more about setting IAM permissions at
# https://cloud.google.com/iam/docs/manage-access-other-resources
#
# 4. Create GitHub secrets for WIF_PROVIDER and WIF_SERVICE_ACCOUNT
#
# 5. Change the values for the GAR_LOCATION, SERVICE and REGION environment variables (below).
#
# NOTE: To use Google Container Registry instead, replace ${{ env.GAR_LOCATION }}-docker.pkg.dev with gcr.io
#
# For more support on how to run this workflow, please visit https://github.com/marketplace/actions/deploy-to-cloud-run
#
# Further reading:
# Cloud Run IAM permissions - https://cloud.google.com/run/docs/deploying
# Artifact Registry IAM permissions - https://cloud.google.com/artifact-registry/docs/access-control#roles
# Container Registry vs Artifact Registry - https://cloud.google.com/blog/products/application-development/understanding-artifact-registry-vs-container-registry
# Principle of least privilege - https://cloud.google.com/blog/products/identity-security/dont-get-pwned-practicing-the-principle-of-least-privilege
# 3. Change the values in the "env" block to match your values.
name: Build and Deploy to Cloud Run
name: 'Build and Deploy to Cloud Run'
on:
push:
branches: [ $default-branch ]
branches:
- '$default-branch'
env:
PROJECT_ID: YOUR_PROJECT_ID # TODO: update Google Cloud project id
GAR_LOCATION: YOUR_GAR_LOCATION # TODO: update Artifact Registry location
SERVICE: YOUR_SERVICE_NAME # TODO: update Cloud Run service name
REGION: YOUR_SERVICE_REGION # TODO: update Cloud Run service region
PROJECT_ID: 'my-project' # TODO: update to your Google Cloud project ID
REGION: 'us-central1' # TODO: update to your region
SERVICE: 'my-service' # TODO: update to your service name
WORKLOAD_IDENTITY_PROVIDER: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' # TODO: update to your workload identity provider
jobs:
deploy:
# Add 'id-token' with the intended permissions for workload identity federation
runs-on: 'ubuntu-latest'
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: 'Checkout'
uses: 'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332' # actions/checkout@v4
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
# Configure Workload Identity Federation and generate an access token.
#
# See https://github.com/google-github-actions/auth for more options,
# including authenticating via a JSON credentials file.
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@f112390a2df9932162083945e46d439060d66ec2' # google-github-actions/auth@v2
with:
token_format: 'access_token'
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - my-service-account@my-project.iam.gserviceaccount.com
workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}'
# NOTE: Alternative option - authentication via credentials json
# - name: Google Auth
# id: auth
# uses: 'google-github-actions/auth@v0'
# with:
# credentials_json: '${{ secrets.GCP_CREDENTIALS }}''
# BEGIN - Docker auth and build (NOTE: If you already have a container image, these Docker steps can be omitted)
# Authenticate Docker to Google Cloud Artifact Registry
- name: Docker Auth
id: docker-auth
uses: 'docker/login-action@v1'
# BEGIN - Docker auth and build
#
# If you already have a container image, you can omit these steps.
- name: 'Docker Auth'
uses: 'docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567' # docker/login-action@v3
with:
username: 'oauth2accesstoken'
password: '${{ steps.auth.outputs.access_token }}'
registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev'
password: '${{ steps.auth.outputs.auth_token }}'
registry: '${{ env.REGION }}-docker.pkg.dev'
- name: Build and Push Container
- name: 'Build and Push Container'
run: |-
docker build -t "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}" ./
docker push "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}"
DOCKER_TAG="$${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}"
docker build --tag "${DOCKER_TAG}" .
docker push "${DOCKER_TAG}"
- name: 'Deploy to Cloud Run'
# END - Docker auth and build
# END - Docker auth and build
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v0
uses: 'google-github-actions/deploy-cloudrun@33553064113a37d688aa6937bacbdc481580be17' # google-github-actions/deploy-cloudrun@v2
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
# NOTE: If using a pre-built image, update the image name here
image: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}
service: '${{ env.SERVICE }}'
region: '${{ env.REGION }}'
# NOTE: If using a pre-built image, update the image name below:
# If required, use the Cloud Run url output in later steps
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
image: '${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}'
# If required, use the Cloud Run URL output in later steps
- name: 'Show output'
run: |2-
echo ${{ steps.deploy.outputs.url }}
+46 -66
View File
@@ -1,95 +1,75 @@
# This workflow will deploy source code on Cloud Run when a commit is pushed to the $default-branch branch
#
# Overview:
#
# 1. Authenticate to Google Cloud
# 2. Deploy it to Cloud Run
# This workflow will deploy source code on Cloud Run when a commit is pushed to
# the $default-branch branch.
#
# To configure this workflow:
#
# 1. Ensure the required Google Cloud APIs are enabled:
# 1. Enable the following Google Cloud APIs:
#
# Cloud Run run.googleapis.com
# Cloud Build cloudbuild.googleapis.com
# Artifact Registry artifactregistry.googleapis.com
# - Artifact Registry (artifactregistry.googleapis.com)
# - Cloud Build (cloudbuild.googleapis.com)
# - Cloud Run (run.googleapis.com)
# - IAM Credentials API (iamcredentials.googleapis.com)
#
# 2. Create and configure Workload Identity Federation for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
# You can learn more about enabling APIs at
# https://support.google.com/googleapi/answer/6158841.
#
# 3. Ensure the required IAM permissions are granted
# 2. Create and configure a Workload Identity Provider for GitHub:
# https://github.com/google-github-actions/auth#preferred-direct-workload-identity-federation.
#
# Cloud Run
# roles/run.admin
# roles/iam.serviceAccountUser (to act as the Cloud Run runtime service account)
# Depending on how you authenticate, you will need to grant an IAM principal
# permissions on Google Cloud:
#
# Cloud Build
# roles/cloudbuild.builds.editor
# - Artifact Registry Administrator (roles/artifactregistry.admin)
# - Cloud Run Source Developer (roles/run.sourceDeveloper)
#
# Cloud Storage
# roles/storage.objectAdmin
# You can learn more about setting IAM permissions at
# https://cloud.google.com/iam/docs/manage-access-other-resources.
#
# Artifact Registry
# roles/artifactregistry.admin (project or repository level)
#
# NOTE: You should always follow the principle of least privilege when assigning IAM roles
#
# 4. Create GitHub secrets for WIF_PROVIDER and WIF_SERVICE_ACCOUNT
#
# 5. Change the values for the SERVICE and REGION environment variables (below).
#
# For more support on how to run this workflow, please visit https://github.com/marketplace/actions/deploy-to-cloud-run
#
# Further reading:
# Cloud Run runtime service account - https://cloud.google.com/run/docs/securing/service-identity
# Cloud Run IAM permissions - https://cloud.google.com/run/docs/deploying-source-code#permissions_required_to_deploy
# Cloud Run builds from source - https://cloud.google.com/run/docs/deploying-source-code
# Principle of least privilege - https://cloud.google.com/blog/products/identity-security/dont-get-pwned-practicing-the-principle-of-least-privilege
# 3. Change the values in the "env" block to match your values.
name: Deploy to Cloud Run from Source
name: 'Deploy to Cloud Run from Source'
on:
push:
branches: [ $default-branch ]
branches:
- '$default-branch'
env:
PROJECT_ID: YOUR_PROJECT_ID # TODO: update Google Cloud project id
SERVICE: YOUR_SERVICE_NAME # TODO: update Cloud Run service name
REGION: YOUR_SERVICE_REGION # TODO: update Cloud Run service region
PROJECT_ID: 'my-project' # TODO: update to your Google Cloud project ID
REGION: 'us-central1' # TODO: update to your region
SERVICE: 'my-service' # TODO: update to your service name
jobs:
deploy:
# Add 'id-token' with the intended permissions for workload identity federation
runs-on: 'ubuntu-latest'
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: 'Checkout'
uses: 'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332' # actions/checkout@v4
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
# Configure Workload Identity Federation and generate an access token.
#
# See https://github.com/google-github-actions/auth for more options,
# including authenticating via a JSON credentials file.
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@f112390a2df9932162083945e46d439060d66ec2' # google-github-actions/auth@v2
with:
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - my-service-account@my-project.iam.gserviceaccount.com
workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' # TODO: replace with your workload identity provider
# NOTE: Alternative option - authentication via credentials json
# - name: Google Auth
# id: auth
# uses: 'google-github-actions/auth@v0'
# with:
# credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v0
- name: 'Deploy to Cloud Run'
uses: 'google-github-actions/deploy-cloudrun@33553064113a37d688aa6937bacbdc481580be17' # google-github-actions/deploy-cloudrun@v2
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
# NOTE: If required, update to the appropriate source folder
source: ./
service: '${{ env.SERVICE }}'
region: '${{ env.REGION }}'
# NOTE: If using a different source folder, update the image name below:
source: './'
# If required, use the Cloud Run url output in later steps
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
# If required, use the Cloud Run URL output in later steps
- name: 'Show output'
run: |-
echo ${{ steps.deploy.outputs.url }}
+91 -66
View File
@@ -1,91 +1,116 @@
# This workflow will build a docker container, publish it to Google Container Registry, and deploy it to GKE when there is a push to the $default-branch branch.
# This workflow will build a docker container, publish it to Google Container
# Registry, and deploy it to GKE when there is a push to the $default-branch
# branch.
#
# To configure this workflow:
#
# 1. Ensure that your repository contains the necessary configuration for your Google Kubernetes Engine cluster, including deployment.yml, kustomization.yml, service.yml, etc.
# 1. Enable the following Google Cloud APIs:
#
# 2. Create and configure a Workload Identity Provider for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
# - Artifact Registry (artifactregistry.googleapis.com)
# - Google Kubernetes Engine (container.googleapis.com)
# - IAM Credentials API (iamcredentials.googleapis.com)
#
# 3. Change the values for the GAR_LOCATION, GKE_ZONE, GKE_CLUSTER, IMAGE, REPOSITORY and DEPLOYMENT_NAME environment variables (below).
# You can learn more about enabling APIs at
# https://support.google.com/googleapi/answer/6158841.
#
# For more support on how to run the workflow, please visit https://github.com/google-github-actions/setup-gcloud/tree/master/example-workflows/gke-kustomize
# 2. Ensure that your repository contains the necessary configuration for your
# Google Kubernetes Engine cluster, including deployment.yml,
# kustomization.yml, service.yml, etc.
#
# 3. Create and configure a Workload Identity Provider for GitHub:
# https://github.com/google-github-actions/auth#preferred-direct-workload-identity-federation.
#
# Depending on how you authenticate, you will need to grant an IAM principal
# permissions on Google Cloud:
#
# - Artifact Registry Administrator (roles/artifactregistry.admin)
# - Kubernetes Engine Developer (roles/container.developer)
#
# You can learn more about setting IAM permissions at
# https://cloud.google.com/iam/docs/manage-access-other-resources
#
# 5. Change the values in the "env" block to match your values.
name: Build and Deploy to GKE
name: 'Build and Deploy to GKE'
on:
push:
branches: [ $default-branch ]
branches:
- '$default-branch'
env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GAR_LOCATION: us-central1 # TODO: update region of the Artifact Registry
GKE_CLUSTER: cluster-1 # TODO: update to cluster name
GKE_ZONE: us-central1-c # TODO: update to cluster zone
DEPLOYMENT_NAME: gke-test # TODO: update to deployment name
REPOSITORY: samples # TODO: update to Artifact Registry docker repository
IMAGE: static-site
PROJECT_ID: 'my-project' # TODO: update to your Google Cloud project ID
GAR_LOCATION: 'us-central1' # TODO: update to your region
GKE_CLUSTER: 'cluster-1' # TODO: update to your cluster name
GKE_ZONE: 'us-central1-c' # TODO: update to your cluster zone
DEPLOYMENT_NAME: 'gke-test' # TODO: update to your deployment name
REPOSITORY: 'samples' # TODO: update to your Artifact Registry docker repository name
IMAGE: 'static-site'
WORKLOAD_IDENTITY_PROVIDER: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' # TODO: update to your workload identity provider
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
environment: production
name: 'Setup, Build, Publish, and Deploy'
runs-on: 'ubuntu-latest'
environment: 'production'
permissions:
contents: 'read'
id-token: 'write'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: 'Checkout'
uses: 'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332' # actions/checkout@v4
# Configure Workload Identity Federation and generate an access token.
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v0'
with:
token_format: 'access_token'
workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
service_account: 'my-service-account@my-project.iam.gserviceaccount.com'
# Configure Workload Identity Federation and generate an access token.
#
# See https://github.com/google-github-actions/auth for more options,
# including authenticating via a JSON credentials file.
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@f112390a2df9932162083945e46d439060d66ec2' # google-github-actions/auth@v2
with:
workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}'
# Alternative option - authentication via credentials json
# - id: 'auth'
# uses: 'google-github-actions/auth@v0'
# with:
# credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
# Authenticate Docker to Google Cloud Artifact Registry
- name: 'Docker Auth'
uses: 'docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567' # docker/login-action@v3
with:
username: 'oauth2accesstoken'
password: '${{ steps.auth.outputs.auth_token }}'
registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev'
- name: Docker configuration
run: |-
echo ${{steps.auth.outputs.access_token}} | docker login -u oauth2accesstoken --password-stdin https://$GAR_LOCATION-docker.pkg.dev
# Get the GKE credentials so we can deploy to the cluster
- name: Set up GKE credentials
uses: google-github-actions/get-gke-credentials@v0
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
# Get the GKE credentials so we can deploy to the cluster
- name: 'Set up GKE credentials'
uses: 'google-github-actions/get-gke-credentials@6051de21ad50fbb1767bc93c11357a49082ad116' # google-github-actions/get-gke-credentials@v2
with:
cluster_name: '${{ env.GKE_CLUSTER }}'
location: '${{ env.GKE_ZONE }}'
# 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"
# Set up kustomize
- name: Set up Kustomize
run: |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
chmod u+x ./kustomize
# Deploy the Docker image to the GKE cluster
- name: Deploy
run: |-
# replacing the image name in the k8s template
./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA
./kustomize build . | kubectl apply -f -
kubectl rollout status deployment/$DEPLOYMENT_NAME
kubectl get services -o wide
# Build the Docker image
- name: 'Build and push Docker container'
run: |-
DOCKER_TAG="${GAR_LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/${IMAGE}:${GITHUB_SHA}"
docker build \
--tag "${DOCKER_TAG}" \
--build-arg GITHUB_SHA="${GITHUB_SHA}" \
--build-arg GITHUB_REF="${GITHUB_REF}" \
.
docker push "${DOCKER_TAG}"
# Set up kustomize
- name: 'Set up Kustomize'
run: |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.4.3/kustomize_v5.4.3_linux_amd64.tar.gz
chmod u+x ./kustomize
# Deploy the Docker image to the GKE cluster
- name: 'Deploy to GKE'
run: |-
# replacing the image name in the k8s template
./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA
./kustomize build . | kubectl apply -f -
kubectl rollout status deployment/$DEPLOYMENT_NAME
kubectl get services -o wide