Skip to content

Instantly share code, notes, and snippets.

@poudelmadhav
Last active October 23, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poudelmadhav/775551e92951e7a6e9f41234998019c8 to your computer and use it in GitHub Desktop.
Save poudelmadhav/775551e92951e7a6e9f41234998019c8 to your computer and use it in GitHub Desktop.
sysc dist/ to aws s3 and create invalidations in cloudfront with notifying at slack
#!/bin/sh
target_env=$1
profile=$2
REPO="https://gitlab.com/namespace-inc/corporate"
SLACK_URL="https://hooks.slack.com/services/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"
CHANNEL="#corporate"
BOTNAME="deployer"
EMOJI=":namespace:"
STAGING_BUCKET="staging.namespace.jp"
STAGING_DIST_ID="E1DP37WHPAD3NE"
PRODUCTION_BUCKET="namespace.jp"
PRODUCTION_DIST_ID="E3GF68XNTZ2TD4"
if [ $target_env != "prod" ] && [ $target_env != "staging" ]; then
echo "Specify stage. prod or staging"
exit 1
fi
if [ -z $2 ]; then
echo "Specity your aws-cli profile."
exit 1
fi
if [ $target_env = "prod" ]; then
read -p "Type 'yes' to confirm to deploy in production stage: " confirm
case "$confirm" in
yes ) echo "Deploy started for production stage";;
* )
echo "User interrupted."
exit 1
;;
esac
fi
notify_slack() {
person=$(whoami)
branch=$(git branch | grep \* | cut -d ' ' -f2)
branch_link=$(echo "$branch" | sed -r 's/#/%23/g')
commit_head=$(git rev-parse --verify HEAD)
commit_msg=$(git reflog -1 | sed 's/^.*: //')
SLACK_PAYLOAD_DATA=$(cat <<- EOF
{
"channel": "$CHANNEL",
"username": "$BOTNAME",
"icon_emoji": "$EMOJI",
"attachments": [
{
"fallback": "$1",
"text": "$2",
"color": "$3",
"fields": [
{
"title": "User",
"value": "$person",
"short": "true"
},
{
"title": "Branch",
"value": "<$REPO/tree/$branch_link|$branch>",
"short": "true"
},
{
"title": "Stage",
"value": "$target_env",
"short": "true"
},
{
"title": "Commit",
"value": "<$REPO/commit/$commit_head|$commit_msg>",
"short": "true"
}
]
}
]
}
EOF
)
curl -X POST --data-urlencode "payload=$SLACK_PAYLOAD_DATA" $SLACK_URL
}
case "$1" in
"prod")
dist_id=$PRODUCTION_DIST_ID
bucket=$PRODUCTION_BUCKET
;;
"staging")
dist_id=$STAGING_DIST_ID
bucket=$STAGING_BUCKET
;;
esac
text="Deploy starting... $bucket :slightly_smiling_face:"
notify_slack "Deploy starting... $bucket" "$text" "warning"
# Build for envs.
echo "\n\nBUILD for $target_env"
rm -rf build/
npm run build
# Deploy /builds.
echo "DEPLOY for $target_env"
aws s3 sync build/ s3://$bucket --profile $profile
# Remove cache.
echo "REMOVE CACHE..."
aws cloudfront create-invalidation --distribution-id $dist_id --paths '/*' --profile $profile
text="Deploy succeeded! :tada:"
notify_slack "Deploy Suceeded!" "$text" "good"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment