dovetail docker job added
[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 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 colide 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 # If we just want to update the latest_stable image
55 if [[ "$UPDATE_LATEST_STABLE" == "true" ]]; then
56     echo "Pulling $DOCKER_REPO_NAME:$STABLE_TAG ..."
57     docker pull $DOCKER_REPO_NAME:$STABLE_TAG
58     if [[ $? -ne 0 ]]; then
59         echo "ERROR: The image $DOCKER_REPO_NAME with tag $STABLE_TAG does not exist."
60         exit 1
61     fi
62     docker tag -f $DOCKER_REPO_NAME:$STABLE_TAG $DOCKER_REPO_NAME:latest_stable
63     echo "Pushing $DOCKER_REPO_NAME:latest_stable ..."
64     docker push $DOCKER_REPO_NAME:latest_stable
65     exit 0
66 fi
67
68
69 # cd to directory where Dockerfile is located
70 if [[ "$DOCKER_REPO_NAME" == "opnfv/bottlenecks" ]]; then
71     cd $WORKSPACE/ci/docker
72 elif [[ "$DOCKER_REPO_NAME" == "opnfv/cperf" ]]; then
73     cd $WORKSPACE/docker
74 elif [[ "$DOCKER_REPO_NAME" == "opnfv/dovetail" ]]; then
75     cd $WORKSPACE/docker
76 elif [[ "$DOCKER_REPO_NAME" == "opnfv/functest" ]]; then
77     cd $WORKSPACE/docker
78 elif [[ "$DOCKER_REPO_NAME" == "opnfv/qtip" ]]; then
79     cd $WORKSPACE/docker
80 elif [[ "$DOCKER_REPO_NAME" == "opnfv/storperf" ]]; then
81     cd $WORKSPACE/docker
82 elif [[ "$DOCKER_REPO_NAME" == "opnfv/yardstick" ]]; then
83     cd $WORKSPACE/tests/ci/docker/yardstick-ci
84 else
85     echo "ERROR: DOCKER_REPO_NAME parameter not valid: $DOCKER_REPO_NAME"
86     exit 1
87 fi
88
89 # Get tag version
90 branch="${GIT_BRANCH##origin/}"
91 echo "Current branch: $branch"
92
93 if [[ "$branch" == "master" ]]; then
94     DOCKER_TAG="master"
95     DOCKER_BRANCH_TAG="latest"
96 else
97     git clone https://gerrit.opnfv.org/gerrit/releng $WORKSPACE/releng
98
99     DOCKER_TAG=$($WORKSPACE/releng/utils/calculate_version.sh -t docker \
100         -n $DOCKER_REPO_NAME)
101     DOCKER_BRANCH_TAG="stable"
102
103     ret_val=$?
104     if [[ $ret_val -ne 0 ]]; then
105         echo "Error retrieving the version tag."
106         exit 1
107     fi
108 fi
109 echo "Tag version to be build and pushed: $DOCKER_TAG"
110
111
112 # Start the build
113 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG"
114
115 if [[ $DOCKER_REPO_NAME == *"functest"* ]]; then
116     docker build --no-cache -t $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG --build-arg BRANCH=$branch .
117 else
118     docker build --no-cache -t $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG .
119 fi
120
121 echo "Creating tag '$DOCKER_TAG'..."
122 docker tag -f $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG $DOCKER_REPO_NAME:$DOCKER_TAG
123
124 # list the images
125 echo "Available images are:"
126 docker images
127
128 # Push image to Dockerhub
129 if [[ "$PUSH_IMAGE" == "true" ]]; then
130     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
131     echo "--------------------------------------------------------"
132     echo
133     # Push to the Dockerhub repository
134     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG ..."
135     docker push $DOCKER_REPO_NAME:$DOCKER_BRANCH_TAG
136
137     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG ..."
138     docker push $DOCKER_REPO_NAME:$DOCKER_TAG
139 fi