Skip to content

Instantly share code, notes, and snippets.

@moosh3
Last active March 2, 2019 22:10
Show Gist options
  • Save moosh3/4c7d4f4a41e785601f24c4a1a1bdbef3 to your computer and use it in GitHub Desktop.
Save moosh3/4c7d4f4a41e785601f24c4a1a1bdbef3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Recieves POST'ed attributes via a webhook
# WORKDIR : /home/${SERVICE}/${SERVICE}
# USER : ${SERVICE}
# $1 : head_commit.id
# $2 : pusher.name
# $3 : pusher.email
set +e
REGISTRY=reg.integracredit.com
SERVICE=cobra
ENVIRONMENT=staging
WEBHOOK_URL=
CURWD=/home/${SERVICE}/
DATE=$(date +%F)
# [LOG]
if [ -f ${CURWD}/deploy-log-$DATE.log ] || touch ${CURWD}/deploy-log-$DATE.log
exec > >(tee "$CURWD/deploy-log-$DATE.log") 2>&1
# [ALERT] ${SERVICE} is being deployed!
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"${2} is deploying ${SERVICE} to ${ENVIRONMENT}"}' \
${WEBHOOK_URL}
# [EXEC] Ensure the repository is on clean master
git fetch --all
git reset --hard origin/master
# [EXEC] Build the newest image from the code
docker build -f Dockerfile.prod -t ${REGISTRY}/${SERVICE}:${1} .
# [EXEC] Restart docker-compose with new image
docker-compose -f docker-compose.${ENVIRONMENT}.yml down
sed -i 's/IMAGE_TAG=*/IMAGE_TAG=${1}/g' .env
docker-compose -f docker-compose.${ENVIRONMENT}.yml up -d
# [ALERT] ${SERVICE} was successfully deployed!
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"${SERVICE} was successfully deployed!"}' \
${WEBHOOK_URL}
- id: webhook
execute-command: /home/cobra/deploy-go-webhook.sh
command-working-directory: /home/cobra/cobra
response-message: Starting deployment
response-headers:
- name: Access-Control-Allow-Origin
value: '*'
pass-arguments-to-command:
- source: payload
name: head_commit.id
- source: payload
name: pusher.name
- source: payload
name: pusher.email
trigger-rule:
and:
- match:
type: payload-hash-sha1
secret: mysecret
parameter:
source: header
name: X-Hub-Signature
- match:
type: value
value: refs/heads/master
parameter:
source: payload
name: ref
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment