ded743d7e5cc798947a09dec0a9bff33a09b58ee
[releng.git] / jjb / releng / opnfv-docker.sh
1 #!/bin/bash
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 ##############################################################################
10 set -o errexit
11 set -o nounset
12 set -o pipefail
13
14
15
16 echo "Starting opnfv-docker for $DOCKER_REPO_NAME ..."
17 echo "--------------------------------------------------------"
18 echo
19
20
21 if [[ -n $(ps -ef|grep 'docker build'|grep -v grep) ]]; then
22     echo "There is already another build process in progress:"
23     echo $(ps -ef|grep 'docker build'|grep -v grep)
24     # Abort this job since it will collide and might mess up the current one.
25     echo "Aborting..."
26     exit 1
27 fi
28
29 # Remove previous running containers if exist
30 if [[ -n "$(docker ps -a | grep $DOCKER_REPO_NAME)" ]]; then
31     echo "Removing existing $DOCKER_REPO_NAME containers..."
32     docker ps -a | grep $DOCKER_REPO_NAME | awk '{print $1}' | xargs docker rm -f
33     t=60
34     # Wait max 60 sec for containers to be removed
35     while [[ $t -gt 0 ]] && [[ -n "$(docker ps| grep $DOCKER_REPO_NAME)" ]]; do
36         sleep 1
37         let t=t-1
38     done
39 fi
40
41
42 # Remove existing images if exist
43 if [[ -n "$(docker images | grep $DOCKER_REPO_NAME)" ]]; then
44     echo "Docker images to remove:"
45     docker images | head -1 && docker images | grep $DOCKER_REPO_NAME
46     image_ids=($(docker images | grep $DOCKER_REPO_NAME | awk '{print $3}'))
47     for id in "${image_ids[@]}"; do
48         if [[ -n "$(docker images|grep $DOCKER_REPO_NAME|grep $id)" ]]; then
49             echo "Removing docker image $DOCKER_REPO_NAME:$id..."
50             docker rmi -f $id
51         fi
52     done
53 fi
54
55 cd $WORKSPACE/docker
56 if [ ! -f ${DOCKERFILE} ]; then
57     echo "ERROR: Dockerfile not found."
58     exit 1
59 fi
60
61 # Get tag version
62 echo "Current branch: $BRANCH"
63
64 if [[ "$BRANCH" == "master" ]]; then
65     DOCKER_TAG="latest"
66 else
67     if [[ "$RELEASE_VERSION" != "" ]]; then
68         release=${BRANCH##*/}
69         DOCKER_TAG=${release}.${RELEASE_VERSION}
70         # e.g. colorado.1.0, colorado.2.0, colorado.3.0
71     else
72         DOCKER_TAG="stable"
73     fi
74 fi
75
76 # Start the build
77 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_TAG"
78 echo "--------------------------------------------------------"
79 echo
80 if [[ $DOCKER_REPO_NAME == *"dovetail"* ]]; then
81     cmd="docker build --no-cache -t $DOCKER_REPO_NAME:$DOCKER_TAG -f $DOCKERFILE ."
82 else
83     cmd="docker build --no-cache -t $DOCKER_REPO_NAME:$DOCKER_TAG --build-arg BRANCH=$BRANCH
84         -f $DOCKERFILE ."
85 fi
86
87 echo ${cmd}
88 ${cmd}
89
90
91 # list the images
92 echo "Available images are:"
93 docker images
94
95 # Push image to Dockerhub
96 if [[ "$PUSH_IMAGE" == "true" ]]; then
97     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
98     echo "--------------------------------------------------------"
99     echo
100     docker push $DOCKER_REPO_NAME:$DOCKER_TAG
101 fi