diff --git a/README.md b/README.md index f58aada..0673c46 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # bash-helper-modules -Contains Bash scripts helper functions for setup and deployments. +Contains Bash scripts helper functions for k8s & CircleCI setup and deployments. + +file structure + +> example (file) +- deploy + +> docker(image) +- dockerfile + +> k8s (deploy) +- deploy diff --git a/examples/deploy b/examples/deploy new file mode 100644 index 0000000..13c6233 --- /dev/null +++ b/examples/deploy @@ -0,0 +1,69 @@ +#!/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') + +# uncomment require-channel below to enable sending of slack notifications +# require 'CHANNEL' $CHANNEL +# require 'SLACK_HOOK' $SLACK_HOOK +# require 'EMOJI_1' $EMOJI_1 +# require 'EMOJI_2' $EMOJI_2 +# require 'EMOJI_3' $EMOJI_3 +# require 'EMOJI_4' $EMOJI_4 +# require 'EMOJI_5' $EMOJI_5 +# require 'EMOJI_6' $EMOJI_6 +# require 'EMOJI_7' $EMOJI_7 +# require 'EMOJI_8' $EMOJI_8 +# require 'EMOJI_9' $EMOJI_9 +# require 'EMOJI_10' $EMOJI_10 +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 + # configureSlackNotifications() +} + +main + + diff --git a/k8s/deploy b/k8s/deploy index a05949c..9e66c68 100644 --- a/k8s/deploy +++ b/k8s/deploy @@ -38,7 +38,6 @@ require () { if [ -z ${2+x} ]; then error "Required variable ${1} has not been set"; fi } - installGoogleCloudSdk(){ info "Installing google cloud sdk" echo "deb http://packages.cloud.google.com/apt cloud-sdk-jessie main" | sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list @@ -175,8 +174,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 +198,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() { @@ -223,3 +224,22 @@ isAllowedDeployEnvironment() { [ -z $(echo $ALLOWED_DEPLOY_ENVIRONMENTS | grep -o $__environment) ] && error "$__environment is not an allowed deployment environment" success "Setting up deployments for $__environment environment" } + +configureSlackNotifications() { + # declare any 10 emojis + # for example; EMOJIS=(":celebrate:" ":party_dinosaur:" ":hammer-time:" ":andela:" ":victory-danch:" ":aw-yeah:" ":carlton-dance:" ":partyparrot:" ":dancing-penguin:" ":aww-yeah-remix:" ) + EMOJIS=(${EMOJI_1} ${EMOJI_2} ${EMOJI_3} ${EMOJI_4} ${EMOJI_5} ${EMOJI_6} ${EMOJI_7} ${EMOJI_8} ${EMOJI_9} ${EMOJI_10} ) + + RANDOM=$$$(date +%s) + EMOJI=${EMOJIS[$RANDOM % ${#EMOJIS[@]} ]} + COMMIT_LINK="https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BRANCH}/commit/${CIRCLE_SHA1}" + DEPLOYMENT_TEXT="Tag: ${IMAGE_NAME} has just been deployed to ${ENVIRONMENT} " + SLACK_DEPLOYMENT_TEXT="Tag: ${IMAGE_NAME} has just been deployed to *${ENVIRONMENT}* ${EMOJI}" + + + # send deploy data to slack + echo "sending deployment notification to configured slack channel" + curl -X POST --data-urlencode \ + "payload={\"channel\": \"${CHANNEL}\", \"username\": \"DeploymentNotification\", \"text\": \"${SLACK_DEPLOYMENT_TEXT}\", \"icon_emoji\": \":airplane:\"}" \ + ${SLACK_HOOK} +} \ No newline at end of file