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 ##############################################################################
16 echo "Starting opnfv-docker for $DOCKER_REPO_NAME ..."
17 echo "--------------------------------------------------------"
20 function remove_containers_images()
22 # Remove previous running containers if exist
23 if [[ -n "$(docker ps -a | grep $DOCKER_REPO_NAME)" ]]; then
24 echo "Removing existing $DOCKER_REPO_NAME containers..."
25 docker ps -a | grep $DOCKER_REPO_NAME | awk '{print $1}' | xargs docker rm -f
27 # Wait max 60 sec for containers to be removed
28 while [[ $t -gt 0 ]] && [[ -n "$(docker ps| grep $DOCKER_REPO_NAME)" ]]; do
35 # Remove existing images if exist
36 if [[ -n "$(docker images | grep $DOCKER_REPO_NAME)" ]]; then
37 echo "Docker images to remove:"
38 docker images | head -1 && docker images | grep $DOCKER_REPO_NAME
39 image_ids=($(docker images | grep $DOCKER_REPO_NAME | awk '{print $3}'))
40 for id in "${image_ids[@]}"; do
41 if [[ -n "$(docker images|grep $DOCKER_REPO_NAME|grep $id)" ]]; then
42 echo "Removing docker image $DOCKER_REPO_NAME:$id..."
50 count=30 # docker build jobs might take up to ~30 min
51 while [[ -n `ps -ef| grep 'docker build' | grep $DOCKER_REPO_NAME | grep -v grep` ]]; do
52 echo "Build or cleanup of $DOCKER_REPO_NAME in progress. Waiting..."
54 count=$(( $count - 1 ))
55 if [ $count -eq 0 ]; then
56 echo "Timeout. Aborting..."
61 # Remove the existing containers and images before building
62 remove_containers_images
64 cd "$WORKSPACE/$DOCKER_DIR" || exit 1
65 HOST_ARCH="$(uname -m)"
66 #If there is a patch for other arch then x86, apply the patch and
67 #replace Dockerfile file
68 dockerfile_patch="Dockerfile.${HOST_ARCH}.patch"
69 if [[ -f "${dockerfile_patch}" ]]; then
70 patch -f Dockerfile -p1 < "${dockerfile_patch}"
74 echo "Current branch: $BRANCH"
78 if [[ "$BRANCH" == "master" ]]; then
80 elif [[ -n "${RELEASE_VERSION-}" ]]; then
81 DOCKER_TAG=${RELEASE_VERSION}
82 if git checkout ${RELEASE_VERSION}; then
83 echo "Successfully checked out the git tag ${RELEASE_VERSION}"
85 echo "The tag ${RELEASE_VERSION} doesn't exist in the repository. Existing tags are:"
93 if [[ -n "${COMMIT_ID-}" && -n "${RELEASE_VERSION-}" ]]; then
94 DOCKER_TAG=$RELEASE_VERSION
95 BUILD_BRANCH=$COMMIT_ID
99 ARCH_TAG=${ARCH_TAG:-}
100 if [[ -n "${ARCH_TAG}" ]]; then
101 DOCKER_TAG=${ARCH_TAG}-${DOCKER_TAG}
102 ARCH_BUILD_ARG="--build-arg ARCH=${ARCH_TAG}"
106 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_TAG"
107 echo "--------------------------------------------------------"
109 cmd="docker build --no-cache -t $DOCKER_REPO_NAME:$DOCKER_TAG --build-arg BRANCH=$BUILD_BRANCH
117 echo "Available images are:"
120 # Push image to Dockerhub
121 if [[ "$PUSH_IMAGE" == "true" ]]; then
122 echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
123 echo "--------------------------------------------------------"
125 docker push $DOCKER_REPO_NAME:$DOCKER_TAG
128 # Remove the existing containers and images after building
129 remove_containers_images