diff --git a/examples/deploy b/examples/deploy new file mode 100644 index 0000000..feb1528 --- /dev/null +++ b/examples/deploy @@ -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 + + diff --git a/k8s/deploy b/k8s/deploy index a05949c..302c677 100644 --- a/k8s/deploy +++ b/k8s/deploy @@ -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() { @@ -91,7 +92,6 @@ getDeploymentName() { local __depoymentNameVariable=$1 local __deploymentName="${ENVIRONMENT}-${PROJECT_NAME}" - if [[ "$__depoymentNameVariable" ]]; then eval $__depoymentNameVariable="'$__deploymentName'" else @@ -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 $@ } @@ -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 @@ -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() { @@ -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" }