Skip to content

Commit

Permalink
ft(examples): add example usage script
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyambati Thomas committed Jun 27, 2018
1 parent 01cd876 commit 0e8571c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
55 changes: 55 additions & 0 deletions examples/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

# The setup below is a sample of how this K8s deploy scripts can be used. Modification may be required to fit your specific purpose.
set -eo pipefail

DEPLOY_SCRIPT_PATH="${HOME}/deploy"

curl -o $DEPLOY_SCRIPT_PATH https://raw.githubusercontent.com/AndelaOSP/bash-helper-modules/master/k8s/deploy

source $DEPLOY_SCRIPT_PATH
# add the following environment variables to your ci. Change them according to your cluster requirements
# PRODUCTION_GOOGLE_COMPUTE_ZONE
# STAGING_GOOGLE_COMPUTE_ZONE
# STAGING_CLUSTER_NAME
# PRODUCTION_CLUSTER_NAME
# PROJECT_NAME
# GOOGLE_PROJECT_ID
# GCLOUD_SERVICE_KEY
# DOCKER_REGISTRY


GCLOUD_SERVICE_KEY_NAME=gcloud-service-key.json
ALLOWED_DEPLOY_ENVIRONMENTS=('staging', 'production')

require 'PRODUCTION_GOOGLE_COMPUTE_ZONE' $PRODUCTION_GOOGLE_COMPUTE_ZONE
require 'STAGING_GOOGLE_COMPUTE_ZONE' $STAGING_GOOGLE_COMPUTE_ZONE
require 'STAGING_CLUSTER_NAME' $STAGING_CLUSTER_NAME
require 'PRODUCTION_CLUSTER_NAME' $PRODUCTION_CLUSTER_NAME
require 'PROJECT_NAME' $PROJECT_NAME
require 'GOOGLE_PROJECT_ID' $GOOGLE_PROJECT_ID
require 'DOCKER_REGISTRY' $DOCKER_REGISTRY
require 'GCLOUD_SERVICE_KEY' $GCLOUD_SERVICE_KEY

BRANCH_NAME=$CIRCLE_BRANCH
setEnvironment $BRANCH_NAME
isAllowedDeployEnvironment $ENVIRONMENT

getDeploymentName DEPLOYMENT_NAME
IMAGE_TAG=$(getImageTag $(getCommitHash))
IMAGE_NAME=$(getImageName)

main() {
installGoogleCloudSdk
authWithServiceAccount
configureGoogleCloudSdk
loginToContainerRegistry _json_key
buildAndTagDockerImage -f docker/Dockerfile .
publishDockerImage
logoutContainerRegistry $DOCKER_REGISTRY
deployToKubernetesCluster backend
}

main


12 changes: 7 additions & 5 deletions k8s/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ installGoogleCloudSdk(){
echo "deb http://packages.cloud.google.com/apt cloud-sdk-jessie main" | sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install kubectl google-cloud-sdk
is_success "Gcloud and Kubecl has been installed successfully"
}

getCommitHash() {
Expand Down Expand Up @@ -91,7 +92,6 @@ getDeploymentName() {

local __depoymentNameVariable=$1
local __deploymentName="${ENVIRONMENT}-${PROJECT_NAME}"

if [[ "$__depoymentNameVariable" ]]; then
eval $__depoymentNameVariable="'$__deploymentName'"
else
Expand Down Expand Up @@ -144,7 +144,7 @@ getImageName() {
# buildAndTagDockerImage args
buildAndTagDockerImage() {
require "IMAGE_NAME" $IMAGE_NAME
info "Building image with tag $IMAGE_NAME"
info "Building image with tag $IMAGE_NAME ....."
docker build -t $IMAGE_NAME $@
}

Expand Down Expand Up @@ -175,8 +175,9 @@ deployToKubernetesCluster() {
require "GOOGLE_CLUSTER_NAME" $GOOGLE_CLUSTER_NAME

info "Deploying image $IMAGE to $ENVIRONMENT environment on $GOOGLE_CLUSTER_NAME cluster"
CMD="kubectl set image deployment/$DEPLOYMENT_NAME $__containerName=$IMAGE_NAME --namespace $NAMESPACE"
echo $CMD

kubectl set image deployment/$DEPLOYMENT_NAME $__containerName=$IMAGE_NAME --namespace $NAMESPACE

if [ "$?" == "0" ]; then
success "Image ${IMAGE_NAME} was successfuly deployed to ${ENVIRONMENT} environment"
exit 0
Expand All @@ -198,6 +199,7 @@ authWithServiceAccount() {
require 'GCLOUD_SERVICE_KEY' $GCLOUD_SERVICE_KEY
echo $GCLOUD_SERVICE_KEY | base64 --decode > $__serviceKeyPath
gcloud auth activate-service-account --key-file $__serviceKeyPath
is_success "Service account activated successfuly"
}

configureGoogleCloudSdk() {
Expand All @@ -220,6 +222,6 @@ configureGoogleCloudSdk() {
isAllowedDeployEnvironment() {
local __environment=$1
require 'ALLOWED_DEPLOY_ENVIRONMENTS' $ALLOWED_DEPLOY_ENVIRONMENTS
[ -z $(echo $ALLOWED_DEPLOY_ENVIRONMENTS | grep -o $__environment) ] && error "$__environment is not an allowed deployment environment"
[ -z $(echo ${ALLOWED_DEPLOY_ENVIRONMENTS[@]} | grep -o $__environment) ] && error "$__environment is not an allowed deployment environment"
success "Setting up deployments for $__environment environment"
}

0 comments on commit 0e8571c

Please sign in to comment.