Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#158013889 Add slack configuration #1

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b15d78e
ft(scripts): setup k8s helper scritps
May 29, 2018
29e481d
ft(examples): add example usage script
May 29, 2018
96ffa24
pull updates
scott45 May 30, 2018
2cf88b3
add command
scott45 May 30, 2018
57b3368
add command
scott45 May 30, 2018
6672289
add command
scott45 May 30, 2018
ca1cacd
add command
scott45 May 30, 2018
a38295e
integrate channel require in example
scott45 Jun 4, 2018
075b361
add slack integration in deploy script
scott45 Jun 4, 2018
ad7d18c
add slack configuration
scott45 Jun 4, 2018
e40fa93
add slack configuration
scott45 Jun 4, 2018
7678ac6
make changes to slack function
scott45 Jun 13, 2018
6cd67cd
make changes to slack function
scott45 Jun 13, 2018
7446273
make changes to slack function
scott45 Jun 13, 2018
8f31f35
make changes to slack function
scott45 Jun 13, 2018
d559beb
make changes to slack function
scott45 Jun 13, 2018
5f755e8
make changes to slack function
scott45 Jun 13, 2018
69c4f8b
make changes to slack function
scott45 Jun 13, 2018
6163a1b
add required env vars
scott45 Jun 13, 2018
1a75469
add required env vars
scott45 Jun 13, 2018
749fe4e
add required env vars
scott45 Jun 13, 2018
04d564f
add required env vars
scott45 Jun 13, 2018
a010363
Merge branch 'master' into add-slack-integration
scott45 Jun 13, 2018
d9a0b97
Merge pull request #1 from Beracah-Group/add-slack-integration
scott45 Jun 13, 2018
dfd517b
Update README.md
scott45 Aug 6, 2018
a84b104
Update README.md
scott45 Sep 14, 2018
3ee0497
Update README.md
scott45 Sep 14, 2018
e7cb2de
Update README.md
scott45 Sep 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions examples/deploy
Original file line number Diff line number Diff line change
@@ -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


26 changes: 23 additions & 3 deletions k8s/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add $USERNAME, $BRANCH_NAME, $CIRCLE_SHA1 as required variables for this function.

# 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}
}