-
Notifications
You must be signed in to change notification settings - Fork 0
126 lines (104 loc) · 4.85 KB
/
gc.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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 "main" 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.
#
# 2. Create and configure a Workload Identity Provider for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation)
#
# 3. Change the values for the GAR_LOCATION, GKE_ZONE, GKE_CLUSTER, IMAGE, REPOSITORY and DEPLOYMENT_NAME environment variables (below).
#
# 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
# TUTORIAL: https://docs.github.com/en/actions/use-cases-and-examples/deploying/deploying-to-google-kubernetes-engine
name: Build and Deploy to GKE
on:
push:
branches: [ "main" ]
env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
# GAR_LOCATION: us-central1 # TODO: update region of the Artifact Registry
GAR_LOCATION: ${{ secrets.GKE_GAR_LOCATION }}
# GKE_CLUSTER: cluster-1 # TODO: update to cluster name
GKE_CLUSTER: my-cluster # TODO: update to cluster name
# GKE_ZONE: us-central1-c # TODO: update to cluster zone
GKE_ZONE: ${{ secrets.GKE_ZONE }} # TODO: update to cluster zone
# DEPLOYMENT_NAME: gke-test # TODO: update to deployment name
DEPLOYMENT_NAME: gke-deployment # TODO: update to deployment name
# REPOSITORY: samples # TODO: update to Artifact Registry docker repository
REPOSITORY: kubernetes-ci-cd-github-actions # TODO: update to Artifact Registry docker repository
# IMAGE: static-site
IMAGE: kubernetes-ci-cd-github-actions
jobs:
setup-build-publish-deploy:
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
# 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: '[email protected]'
# Alternative option - authentication via credentials json
- name: Google Cloud auth
id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
with:
project_id: '${{ env.GKE_PROJECT }}'
- name: 'Docker auth'
run: |-
gcloud auth configure-docker ${{ 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 }}
# 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: |-
# Enter to the k8s directory (because kustomize cannot get the correct path)
cd ./k8s
# Download kustomize
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 to GKE
run: |-
# Enter to the k8s directory (because kustomize cannot get the correct path)
cd ./k8s
# Replace the image name in the k8s template
./kustomize edit set image $GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA
# Build and apply the kustomization
./kustomize build | kubectl apply -f -
# Check the rollout status
kubectl rollout status deployment/$DEPLOYMENT_NAME
# Get services information
kubectl get services -o wide