Merge pull request #36 from FranciZhong/documentation #25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'CD' | |
on: | |
push: | |
branches: | |
- 'main' | |
env: | |
PROJECT_ID: ${{ secrets.PROJECT_ID }} | |
GAR_LOCATION: ${{ secrets.GAR_LOCATION }} | |
GKE_CLUSTER: ${{ secrets.GKE_CLUSTER }} | |
GKE_ZONE: ${{ secrets.GKE_ZONE }} | |
DEPLOYMENT_NAME: ${{ secrets.DEPLOYMENT_NAME }} | |
REPOSITORY: ${{ secrets.REPOSITORY }} | |
IMAGE: ${{ secrets.IMAGE }} | |
jobs: | |
setup-build-publish-deploy: | |
name: 'Setup, Build, Publish, and Deploy' | |
runs-on: 'ubuntu-latest' | |
permissions: | |
contents: 'read' | |
id-token: 'write' | |
steps: | |
- name: 'Checkout' | |
uses: 'actions/checkout@v4' | |
# 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@v2' | |
with: | |
project_id: '${{ env.PROJECT_ID }}' | |
workload_identity_provider: '${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}' | |
service_account: ${{ secrets.SERVICE_ACCOUNT }} | |
# Authenticate Docker to Google Cloud Artifact Registry | |
- name: 'Docker Auth' | |
uses: 'docker/login-action@v3' | |
with: | |
username: 'oauth2accesstoken' | |
password: '${{ steps.auth.outputs.auth_token }}' | |
registry: '${{ env.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@v2' | |
with: | |
cluster_name: '${{ env.GKE_CLUSTER }}' | |
location: '${{ env.GKE_ZONE }}' | |
# Build the Docker image | |
- name: 'Build and push Docker container' | |
id: build-image | |
run: |- | |
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://${GAR_LOCATION}-docker.pkg.dev | |
DOCKER_TAG="${GAR_LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/${IMAGE}:${GITHUB_SHA}" | |
LATEST_DOCKER_TAG="${GAR_LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/${IMAGE}:latest" | |
docker build \ | |
--tag "${DOCKER_TAG}" \ | |
--tag "${LATEST_DOCKER_TAG}" \ | |
--build-arg GITHUB_SHA="${GITHUB_SHA}" \ | |
--build-arg GITHUB_REF="${GITHUB_REF}" \ | |
. | |
docker push "${DOCKER_TAG}" | |
docker push "${LATEST_DOCKER_TAG}" | |
echo "::set-output name=image::${DOCKER_TAG}" | |
# Update the image in the GKE deployment | |
- name: 'Update Image in GKE' | |
run: |- | |
kubectl set image deployment/${{ env.DEPLOYMENT_NAME }} ${{env.IMAGE}}=${{ steps.build-image.outputs.image }} | |
kubectl rollout status deployment/${{ env.DEPLOYMENT_NAME }} | |
kubectl get services -o wide |