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