Created
April 28, 2016 17:39
-
-
Save halloffame/224d436297e953fdc065655dbf6b239b to your computer and use it in GitHub Desktop.
Drop this guy into /usr/local/bin folder for command line deployment through slack through rundeck.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Drop this guy into /usr/local/bin folder for command line deployment through slack through rundeck. | |
# exit on failure | |
set -e | |
usage() { | |
cat << EOF | |
Usage: $0 -t <token> -r <room id> -e <environment> -b <branch> | |
or: $0 <environment> <branch> | |
or: $0 <environment> | |
This script will tell jimbot on slack to deploy your code | |
OPTIONS: | |
-h Show this message | |
-t <token> API token | |
-c <channel> Channel name | |
-a <app name> The app you want to deploy. Defaults to current folder. | |
-e <environment> staging or prod. Defaults to staging. | |
-b <branch> Branch to release. Defaults to current branch. | |
EOF | |
} | |
# Load default settings from ~/.slack_deploy | |
test -f ~/.slack_deploy && . ~/.slack_deploy | |
APP_NAME=${PWD##*/} # get name of current directory | |
ENVIRONMENT='staging' | |
BRANCH=`git symbolic-ref --short HEAD` # default to current branch unless you specify | |
# Accept two parameters, first will be environment, second will be branch | |
if [ ! -z $1 ] | |
then | |
if [[ $1 == 'prod' ]] || [[ $1 == 'staging' ]] | |
then | |
ENVIRONMENT=$1 | |
else | |
echo "!!! Not a valid environment. Either choose staging or prod." | |
exit; | |
fi | |
else | |
usage; exit; | |
fi | |
if [ ! -z $2 ] | |
then | |
BRANCH=$2 | |
fi | |
while getopts “ht:r:o:a:e:b:n” OPTION; do | |
case $OPTION in | |
h) usage; exit 1;; | |
k) DEPLOY_KEY=$OPTARG;; | |
t) AUTH_TOKEN=$OPTARG;; | |
c) CHANNEL=$OPTARG;; | |
a) APP_NAME=$OPTARG;; | |
e) ENVIRONMENT=$OPTARG;; | |
b) BRANCH=$OPTARG;; | |
[?]) usage; exit;; | |
esac | |
done | |
if [[ -z $DEPLOY_KEY ]] || [[ -z $AUTH_TOKEN ]] || [[ -z $CHANNEL ]] | |
then | |
echo "DEPLOY_KEY, AUTH_TOKEN, and CHANNEL must either be passed in or set in ~/.slack_deploy" | |
exit; | |
fi | |
MESSAGE="$DEPLOY_KEY $APP_NAME $ENVIRONMENT $BRANCH" | |
URL="https://slack.com/api/chat.postMessage" | |
echo "$MESSAGE" | |
# the -sS makes curl only show response if there is an error | |
curl -sS \ | |
-X POST \ | |
-d "token=$AUTH_TOKEN" \ | |
-d "channel=$CHANNEL" \ | |
-d "text=$MESSAGE" \ | |
-d "as_user=true" \ | |
-d "pretty=1" \ | |
$URL | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment