2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Ericsson AB and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
15 echo "Using Docker $(docker --version) on $NODE_NAME"
16 echo "Starting Docker build for $DOCKER_REPO_NAME ..."
17 echo "--------------------------------------------------------"
20 function remove_containers_images()
22 # Remove previous running containers if exist
25 # CONTAINER ID IMAGE COMMAND ...
26 # 6a796ed40b8e opnfv/example-tasks:latest "/bin/bash" ...
27 # 99fcb59f4787 opnfv/example-tasks-base:latest "/bin/bash" ...
28 # cc5eee16b995 opnfv/example-tasks-k8s "/bin/bash" ...
30 # Cut image name by leading space and ending space or colon(tag)
31 if [[ -n "$(docker ps -a | grep " $DOCKER_REPO_NAME[ :]")" ]]; then
32 echo "Removing existing $DOCKER_REPO_NAME containers..."
33 docker ps -a | grep " $DOCKER_REPO_NAME[ :]" | awk '{print $1}' | xargs docker rm -f
35 # Wait max 60 sec for containers to be removed
36 while [[ $t -gt 0 ]] && [[ -n "$(docker ps| grep " $DOCKER_REPO_NAME[ :]")" ]]; do
43 # Remove existing images if exist
46 # REPOSITORY TAG IMAGE ID ...
47 # opnfv/example-tasks latest 6501569fd328 ...
48 # opnfv/example-tasks-base latest 8764fe29c434 ...
49 # opnfv/example-tasks-k8s latest 61094cac9e65 ...
51 # Cut image name by start of line and ending space
52 if [[ -n "$(docker images | grep "^$DOCKER_REPO_NAME ")" ]]; then
53 echo "Docker images to remove:"
54 docker images | head -1 && docker images | grep "^$DOCKER_REPO_NAME "
55 image_ids=($(docker images | grep "^$DOCKER_REPO_NAME " | awk '{print $3}'))
56 for id in "${image_ids[@]}"; do
57 if [[ -n "$(docker images|grep "^$DOCKER_REPO_NAME "|grep $id)" ]]; then
58 echo "Removing docker image $DOCKER_REPO_NAME:$id..."
66 count=30 # docker build jobs might take up to ~30 min
67 while [[ -n `ps -ef| grep 'docker build' | grep $DOCKER_REPO_NAME | grep -v grep` ]]; do
68 echo "Build or cleanup of $DOCKER_REPO_NAME in progress. Waiting..."
70 count=$(( $count - 1 ))
71 if [ $count -eq 0 ]; then
72 echo "Timeout. Aborting..."
77 # Remove the existing containers and images before building
78 remove_containers_images
80 DOCKER_PATH=$WORKSPACE/$DOCKER_DIR
82 cd $DOCKER_PATH || exit 1
83 HOST_ARCH="$(uname -m)"
84 #If there is a patch for other arch then x86, apply the patch and
85 #replace Dockerfile file
86 dockerfile_patch="Dockerfile.${HOST_ARCH}.patch"
87 if [[ -f "${dockerfile_patch}" ]]; then
88 patch -f Dockerfile -p1 < "${dockerfile_patch}"
92 echo "Current branch: $BRANCH"
96 GERRIT_REFNAME=${GERRIT_REFNAME:-''}
97 RELEASE_VERSION=${GERRIT_REFNAME/refs\/tags\/}
99 # If we're being triggered by a comment-added job, then extract the tag
100 # from the comment and use that as the release version.
101 # Expected comment format: retag opnfv-x.y.z
102 if [[ "${GERRIT_EVENT_TYPE:-}" == "comment-added" ]]; then
103 RELEASE_VERSION=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'retag' | awk '{print $2}')
106 if [[ "$BRANCH" == "master" ]]; then
110 if [[ "$BRANCH" =~ "stable" ]]; then
114 if [[ -n "${RELEASE_VERSION-}" ]]; then
115 DOCKER_TAG=${RELEASE_VERSION}
116 if git checkout ${RELEASE_VERSION}; then
117 echo "Successfully checked out the git tag ${RELEASE_VERSION}"
119 echo "The tag ${RELEASE_VERSION} doesn't exist in the repository. Existing tags are:"
125 if [[ -n "${COMMIT_ID-}" && -n "${RELEASE_VERSION-}" ]]; then
126 DOCKER_TAG=$RELEASE_VERSION
127 BUILD_BRANCH=$COMMIT_ID
131 ARCH_TAG=${ARCH_TAG:-}
132 if [[ -n "${ARCH_TAG}" ]]; then
133 DOCKER_TAG=${ARCH_TAG}-${DOCKER_TAG}
134 ARCH_BUILD_ARG="--build-arg ARCH=${ARCH_TAG}"
137 EXTRA_BUILD_ARGS=${EXTRA_BUILD_ARGS:-}
138 if [ -n "${EXTRA_BUILD_ARGS}" ]; then
139 EXTRA_BUILD_ARGS=" "$(echo ${EXTRA_BUILD_ARGS})
140 EXTRA_BUILD_ARGS=${EXTRA_BUILD_ARGS// / --build-arg }
144 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_TAG"
145 echo "--------------------------------------------------------"
147 cmd="docker build --pull=true --no-cache -t $DOCKER_REPO_NAME:$DOCKER_TAG --build-arg BRANCH=$BUILD_BRANCH
148 $ARCH_BUILD_ARG $EXTRA_BUILD_ARGS
149 -f $DOCKERFILE $DOCKER_PATH"
156 echo "Available images are:"
159 # Push image to Dockerhub
160 if [[ "$PUSH_IMAGE" == "true" ]]; then
161 echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
162 echo "--------------------------------------------------------"
164 docker push $DOCKER_REPO_NAME:$DOCKER_TAG
167 # Remove the existing containers and images after building
168 remove_containers_images