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