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