Created
April 13, 2016 09:43
-
-
Save simenbrekken/09bdee301421641f771cb6de0c303e4b to your computer and use it in GitHub Desktop.
Build and deploy docker image to remote hosts via SSH
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 -e | |
get_current_branch() { | |
echo `git symbolic-ref --short HEAD` | |
} | |
get_app_name() { | |
local branch_name=$(get_current_branch) | |
local app_name=$npm_package_name | |
if [ $branch_name != "master" ]; then | |
echo $branch_name.$app_name | |
else | |
echo $app_name | |
fi | |
} | |
get_image_name() { | |
local app_name=$1 | |
local tag=${2:-latest} | |
echo dokku/$app_name:$tag | |
} | |
get_version_tag() { | |
local version=$npm_package_version | |
echo v$version | |
} | |
build_docker_image() { | |
local image_name=$1 | |
local output_path=$2 | |
echo "Building $image_name..." | |
docker build --tag $image_name . | |
docker save $image_name | bzip2 > $output_path | |
} | |
upload_image() { | |
local host=$1 | |
local image_path=$2 | |
echo "Uploading image to $host..." | |
pv $image_path | ssh $host "bunzip2 | docker load" | |
} | |
function deploy_tag() { | |
local host=$1 | |
local app_name=$2 | |
local tag=$3 | |
ssh $host "dokku apps:create $app_name; dokku tags:deploy $app_name $tag" | |
} | |
function copy_tag() { | |
local host=$1 | |
local app_name=$2 | |
local from=$(get_image_name $app_name $3) | |
local to=$(get_image_name $app_name $4) | |
ssh $host "docker tag -f $from $to" | |
} | |
[email protected] | |
APP_NAME=$(get_app_name) | |
VERSION_TAG=$(get_version_tag) | |
IMAGE_PATH=`mktemp` | |
# Build docker image | |
build_docker_image $(get_image_name $APP_NAME $VERSION_TAG) $IMAGE_PATH | |
# Deploy to primary | |
upload_image $PRIMARY_HOST $IMAGE_PATH | |
deploy_tag $PRIMARY_HOST $APP_NAME $VERSION_TAG | |
copy_tag $PRIMARY_HOST $APP_NAME latest previous || true | |
copy_tag $PRIMARY_HOST $APP_NAME $VERSION_TAG latest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment