Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split script: push_docker.sh #89

Merged
merged 1 commit into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 6 additions & 36 deletions build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ shift "$((OPTIND - 1))"

## Dockerfile directory
DOCKER_BUILD_DIR=dockers/$1
REGISTRY_SERVER=$2
REGISTRY_PORT=$3
REGISTRY_USERNAME=$4
REGISTRY_PASSWD=$5
shift 1

[ -f "$DOCKER_BUILD_DIR"/Dockerfile ] || {
echo "Invalid DOCKER_BUILD_DIR directory" >&2
Expand All @@ -66,16 +63,6 @@ REGISTRY_PASSWD=$5
docker_image_name=$(basename $DOCKER_BUILD_DIR)
}

[ ${BUILD_NUMBER} ] || {
echo "No BUILD_NUMBER found, setting to 0."
BUILD_NUMBER="0"
}

remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$docker_image_tag
timestamp="$(date -u +%Y%m%d)"
build_version="${timestamp}.${BUILD_NUMBER}"
build_remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$build_version

## Copy dependencies
## Note: Dockerfile ADD doesn't support reference files outside the folder, so copy it locally
if ls deps/* 1>/dev/null 2>&1; then
Expand Down Expand Up @@ -112,27 +99,10 @@ if [ "$docker_image_name" = "docker-base" ]; then
docker rmi -f $image_id || true
fi

image_sha=''
if [ -n "$REGISTRY_SERVER" ] && [ -n "$REGISTRY_PORT" ]; then
## Add registry information as tag, so will push as latest
## Add additional tag with build information
## Temporarily add -f option to prevent error message of Docker engine version < 1.10.0
docker tag $docker_image_name $remote_image_name
docker tag $docker_image_name $build_remote_image_name
## Save the docker image in a gz file
mkdir -p target
docker save $docker_image_name | gzip -c > target/$docker_image_name.gz

## Login the docker image registry server
## Note: user name and password are passed from command line
docker login -u $REGISTRY_USERNAME -p "$REGISTRY_PASSWD" $REGISTRY_SERVER:$REGISTRY_PORT

## Push image to registry server
## And get the image digest SHA256
trap_push "docker rmi $remote_image_name || true"
trap_push "docker rmi $build_remote_image_name || true"
image_sha=$(docker push $remote_image_name | sed -n "s/.*: digest: sha256:\([0-9a-f]*\).*/\\1/p")
docker push $build_remote_image_name
if [ -n "$1" ]; then
./push_docker.sh target/$docker_image_name.gz $@ $docker_image_tag
fi

mkdir -p target
rm -f target/$docker_image_name.*.gz
docker save $docker_image_name | gzip -c > target/$docker_image_name.$build_version.gz
echo "Image sha256: $image_sha"
42 changes: 42 additions & 0 deletions push_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
DOCKER_IMAGE_FILE=$1
REGISTRY_SERVER=$2
REGISTRY_PORT=$3
REGISTRY_USERNAME=$4
REGISTRY_PASSWD=$5
DOCKER_IMAGE_TAG=$6

set -e
docker load < $DOCKER_IMAGE_FILE

## Fetch the Jenkins build number if inside it
[ ${BUILD_NUMBER} ] || {
echo "No BUILD_NUMBER found, setting to 0."
BUILD_NUMBER="0"
}

## Prepare tag
docker_image_name=$(basename $DOCKER_IMAGE_FILE | cut -d. -f1)
remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$DOCKER_IMAGE_TAG
timestamp="$(date -u +%Y%m%d)"
build_version="${timestamp}.${BUILD_NUMBER}"
build_remote_image_name=$REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name:$build_version

## Add registry information as tag, so will push as latest
## Add additional tag with build information
## Temporarily add -f option to prevent error message of Docker engine version < 1.10.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this line still necessary? or remove it?

docker tag $docker_image_name $remote_image_name
docker tag $docker_image_name $build_remote_image_name

## Login the docker image registry server
## Note: user name and password are passed from command line
docker login -u $REGISTRY_USERNAME -p "$REGISTRY_PASSWD" $REGISTRY_SERVER:$REGISTRY_PORT

## Push image to registry server
## And get the image digest SHA256
echo "Pushing $remote_image_name"
image_sha=$(docker push $remote_image_name | sed -n "s/.*: digest: sha256:\([0-9a-f]*\).*/\\1/p")
docker rmi $remote_image_name || true
echo "Image sha256: $image_sha"
echo "Pushing $build_remote_image_name"
docker push $build_remote_image_name
docker rmi $build_remote_image_name || true