Last active
November 20, 2018 00:05
-
-
Save imvaskii/aeb4c9f73d90b8d182a9c0761791f2a4 to your computer and use it in GitHub Desktop.
AWS cli CodeDeploy github commit
This file contains hidden or 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
#!/usr/bin/env bash | |
# If arg numbers ($#) is 0 display usage message. | |
[[ $# -eq 0 || -z $1 || -z $2 || -z $3 ]] && { | |
# <repo-name> string string Org name followed by repo name e.g. google/coding-with-chrome | |
printf 'Usage: %s <application-name> <deployment-group> <repo-name> <commit-id> | |
<application-name> string CodeDeploy Application name | |
<deployment-group> string CodeDeploy Development Group name | |
<commit-id> string git commit SHA1 | |
\n' "$0" | |
exit 1; | |
} | |
# We can have following var to read from user input | |
APPLICATION_NAME="$1" | |
DEPLOYMENT_GROUP="$2" | |
REGION="ap-southeast-2" | |
REPOSITORY="domain-group/$1" #"organization/repo" | |
GIT_COMMIT_SHA1="$3" | |
# For terminal colors and effects. | |
# http://jafrog.com/2013/11/23/colors-in-terminal.html | |
# https://gist.github.com/bhaskarkc/a50052470cdc19b622b298b843f47704 | |
# https://github.com/mattjj/my-oh-my-zsh/blob/master/spectrum.zsh | |
# Terminal Color codes. | |
blink="\\e[5m" | |
reset="\\e[0m" | |
red="\\e[31m" | |
green="\\e[32m" | |
orange="\\e[1;33m" | |
cyan="\\e[0;36m" | |
purple="\\e[0;35m" | |
blinkstop="\\e[25m" | |
# Bail early if jq is not installed. | |
if [ -z "$(jq --version)" ]; then | |
printf '%b Please install jq (Commandline json parser) in your system \n' "$red" | |
exit 1 | |
fi | |
# Create AWS CodeDeploy deployment. | |
deployment_id=$(aws deploy create-deployment \ | |
--region "$REGION"\ | |
--application-name "$APPLICATION_NAME" \ | |
--deployment-group-name "$DEPLOYMENT_GROUP" \ | |
--github-location commitId="$GIT_COMMIT_SHA1",repository="$REPOSITORY" \ | |
--output json | jq -r '.deploymentId') | |
# if no deployment ID then bail. | |
if [ -z "$deployment_id" ]; then | |
printf '%b Create Deployment exited with code: %s \n' "$red" "$?" | |
exit 1 | |
fi | |
# Default status | |
status="InProgress" | |
# message | |
printf '%b Deployment ID: %s \n' "$orange" "$deployment_id" | |
while [ "$status" = "Created" ] || [ "$status" = "InProgress" ] | |
do | |
# Get deployment status. | |
json_response=$(aws deploy get-deployment --deployment-id "$deployment_id" --region "$REGION" --output json) | |
# Deployment status. | |
status=$(echo "$json_response" | jq -r '.deploymentInfo.status') | |
case $status in | |
"Succeeded") | |
message="$cyan Deployment Status:$reset $green $status" | |
;; | |
"Failed") | |
message="$cyan Deployment Status:$reset $red $status" | |
;; | |
*) | |
message="$cyan Deployment Status: $blink $status $blinkstop" | |
;; | |
esac | |
printf "%b" "$message" | |
printf '\b%.0s' $(seq 1 ${#message}) | |
# Wait 2 Seconds, | |
sleep 2 | |
done | |
echo | |
if [ "$status" = "Succeeded" ]; then | |
exit 0 | |
fi | |
# Print the whole json response. | |
printf '%b AWS Reponse: \n %b %s %b \n\n' "$orange" "$purple" "$json_response" "$reset" | |
# Error so exit 1. | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment