8f44268e6e24475f227ab7226a219303918938fe
[releng.git] / jjb / opnfv / 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 # Remove previous running containers if exist
21 if [[ -n "$(docker ps -a | grep $DOCKER_REPO_NAME)" ]]; then
22     echo "Removing existing $DOCKER_REPO_NAME containers..."
23     docker ps -a | grep $DOCKER_REPO_NAME | awk '{print $1}' | xargs docker rm -f
24     t=60
25     # Wait max 60 sec for containers to be removed
26     while [[ $t -gt 0 ]] && [[ -n "$(docker ps| grep $DOCKER_REPO_NAME)" ]]; do
27         sleep 1
28         let t=t-1
29     done
30 fi
31
32
33 # Remove existing images if exist
34 if [[ -n "$(docker images | grep $DOCKER_REPO_NAME)" ]]; then
35     echo "Docker images to remove:"
36     docker images | head -1 && docker images | grep $DOCKER_REPO_NAME
37     image_tags=($(docker images | grep $DOCKER_REPO_NAME | awk '{print $2}'))
38     for tag in "${image_tags[@]}"; do
39         if [[ -n "$(docker images|grep $DOCKER_REPO_NAME|grep $tag)" ]]; then
40             echo "Removing docker image $DOCKER_REPO_NAME:$tag..."
41             docker rmi -f $DOCKER_REPO_NAME:$tag
42         fi
43     done
44 fi
45
46 # If we just want to update the latest_stable image
47 if [[ "$UPDATE_LATEST_STABLE" == "true" ]]; then
48     echo "Pulling $DOCKER_REPO_NAME:$STABLE_TAG ..."
49     docker pull $DOCKER_REPO_NAME:$STABLE_TAG
50     if [[ $? -ne 0 ]]; then
51         echo "ERROR: The image $DOCKER_REPO_NAME with tag $STABLE_TAG does not exist."
52         exit 1
53     fi
54     docker tag -f $DOCKER_REPO_NAME:$STABLE_TAG $DOCKER_REPO_NAME:latest_stable
55     echo "Pushing $DOCKER_REPO_NAME:latest_stable ..."
56     docker push $DOCKER_REPO_NAME:latest_stable
57     exit 0
58 fi
59
60
61 # cd to directory where Dockerfile is located
62 if [[ "$DOCKER_REPO_NAME" == "opnfv/functest" ]]; then
63     cd $WORKSPACE/docker
64 elif [[ "$DOCKER_REPO_NAME" == "opnfv/yardstick" ]]; then
65     cd $WORKSPACE/ci/docker/yardstick-ci
66 elif [[ "$DOCKER_REPO_NAME" == "opnfv/storperf" ]]; then
67     cd $WORKSPACE/docker
68 elif [[ "$DOCKER_REPO_NAME" == "opnfv/qtip" ]]; then
69     cd $WORKSPACE/docker
70 elif [[ "$DOCKER_REPO_NAME" == "opnfv/bottlenecks" ]]; then
71     cd $WORKSPACE/ci/docker
72 else
73     echo "ERROR: DOCKER_REPO_NAME parameter not valid: $DOCKER_REPO_NAME"
74     exit 1
75 fi
76
77 # Get tag version
78 branch="${GIT_BRANCH##origin/}"
79 echo "Current branch: $branch"
80
81 if [[ "$branch" == "master" ]]; then
82     DOCKER_TAG="master"
83     DOCKER_BRANCH_TAG="latest"
84 else
85     git clone https://gerrit.opnfv.org/gerrit/releng $WORKSPACE/releng
86
87     DOCKER_TAG=$($WORKSPACE/releng/utils/calculate_version.sh -t docker \
88         -n $DOCKER_REPO_NAME)
89     DOCKER_BRANCH_TAG="stable"
90
91     ret_val=$?
92     if [[ $ret_val -ne 0 ]]; then
93         echo "Error retrieving the version tag."
94         exit 1
95     fi
96 fi
97 echo "Tag version to be build and pushed: $DOCKER_TAG"
98
99
100 # Start the build
101 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG"
102
103 docker build --no-cache -t $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG .
104 echo "Creating tag '$DOCKER_TAG'..."
105 docker tag -f $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG $DOCKER_REPO_NAME:$DOCKER_TAG
106
107 # list the images
108 echo "Available images are:"
109 docker images
110
111 # Push image to Dockerhub
112 if [[ "$PUSH_IMAGE" == "true" ]]; then
113     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
114     echo "--------------------------------------------------------"
115     echo
116     # Push to the Dockerhub repository
117     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG ..."
118     docker push $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG
119
120     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG ..."
121     docker push $DOCKER_REPO_NAME:$DOCKER_TAG
122 fi