#!/bin/bash #Setup a cloud-config directory with all of my yamls for each environment source cloud-config/$1/.env #A simple way to increment my version number increment_version () { declare -a part=( ${1//\-/ } ) declare new declare -i carry=1 for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do len=${#part[CNTR]} new=$((part[CNTR]+carry)) [ ${#new} -gt $len ] && carry=1 || carry=0 [ $CNTR -gt 0 ] && part[CNTR]=${new: -len} || part[CNTR]=${new} done new="${part[*]}" newversion="${new// /-}" } #My Verions newversion='' version=$(cat "version.txt") increment_version $version #This is important to break if the docker build breaks set -xe #Determine if I need to create the cluster or update the deployment image export createcluster=${2:-"N"} export TAG=$newversion #Writes out where it is about to go echo Project: $PROJECTID echo Environment: $ENV echo TAG: $TAG # Are YOU SURE?! read -p "Do you wish to continue? [y/N]" -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]] then #Docker Build - I don't use cloud build echo Docker Build docker build --build-arg docker_env=$ENV --build-arg tag=$TAG-$ENV -t my-app . #Docker Tag latest and new version number docker tag my-app:latest gcr.io/$PROJECTID/my-app-$ENV:$TAG docker tag my-app:latest gcr.io/$PROJECTID/my-app-$ENV:latest #Docker Push to gcr repo docker push gcr.io/$PROJECTID/my-app-$ENV:$TAG docker push gcr.io/$PROJECTID/my-app-$ENV:latest #Make sure the project and zone and region are configured correctly gcloud config set project $PROJECTID gcloud config set compute/zone $ZONE gcloud config set compute/region $REGION #If you need to create a new cluster, then TADA if [[ $createcluster =~ [Yy]$ ]] then if [[ $ENV =~ [staging]$ ]] then gcloud container --project "$PROJECTID" clusters create "my-app-cluster-$ENV" --zone "$ZONE" --username "admin" --cluster-version "1.11.7-gke.12" --machine-type "n1-standard-2" --image-type "COS" --disk-type "pd-standard" --disk-size "100" --scopes "https://www.googleapis.com/auth/cloud-platform" --num-nodes "3" --enable-cloud-logging --enable-cloud-monitoring --no-enable-ip-alias --network "projects/$PROJECTID/global/networks/default" --subnetwork "projects/$PROJECTID/regions/$REGION/subnetworks/default" --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair --enable-autoscaling --min-nodes 3 --max-nodes 6 else gcloud container --project "$PROJECTID" clusters create "my-app-cluster-$ENV" --zone "$ZONE" --username "admin" --cluster-version "1.11.7-gke.12" --machine-type "n1-standard-1" --image-type "COS" --disk-type "pd-standard" --disk-size "100" --scopes "https://www.googleapis.com/auth/cloud-platform" --num-nodes "2" --enable-cloud-logging --enable-cloud-monitoring --no-enable-ip-alias --network "projects/$PROJECTID/global/networks/default" --subnetwork "projects/$PROJECTID/regions/$REGION/subnetworks/default" --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autorepair --enable-autoscaling --min-nodes 1 --max-nodes 4 fi curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh chmod 700 get_helm.sh ./get_helm.sh kubectl create namespace $ENV helm init --tiller-namespace $ENV kubectl create serviceaccount --namespace $ENV tiller kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=$ENV:tiller helm init --service-account tiller --tiller-namespace $ENV --upgrade kubectl create -f cloud-config/$ENV/app-deploy.yaml kubectl expose deployment my-app-app-$ENV --port=8087 --namespace $ENV kubectl autoscale deployment my-app-app-$ENV --max 6 --min 2 --cpu-percent 50 -n $ENV kubectl create secret tls tls-secret-$ENV --key your-domain.com.key --cert your-domain.com.crt -n $ENV kubectl apply -f cloud-config/$ENV/ingress-nginx.yaml echo $newversion > version.txt echo waiting for tiller to become live kubectl get pods -n $ENV sleep 20 helm install --name nginx-ingress-my-app-$ENV stable/nginx-ingress --set rbac.create=true --tiller-namespace $ENV kubectl apply -f cloud-config/$ENV/controller-nginx.yaml else # You are only updating with the new deployment gcloud container clusters get-credentials my-app-cluster-$ENV kubectl set image deployment/my-app-app-$ENV my-app-app-$ENV=gcr.io/$PROJECTID/my-app-$ENV:$TAG --namespace $ENV echo $newversion > version.txt fi fi