Fix Docker Container Build Tagging
[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 "Using Docker $(docker --version) on $NODE_NAME"
16 echo "Starting Docker build for $DOCKER_REPO_NAME ..."
17 echo "--------------------------------------------------------"
18 echo
19
20 function remove_containers_images()
21 {
22     # Remove previous running containers if exist
23     if [[ -n "$(docker ps -a | grep $DOCKER_REPO_NAME)" ]]; then
24         echo "Removing existing $DOCKER_REPO_NAME containers..."
25         docker ps -a | grep $DOCKER_REPO_NAME | awk '{print $1}' | xargs docker rm -f
26         t=60
27         # Wait max 60 sec for containers to be removed
28         while [[ $t -gt 0 ]] && [[ -n "$(docker ps| grep $DOCKER_REPO_NAME)" ]]; do
29             sleep 1
30             let t=t-1
31         done
32     fi
33
34
35     # Remove existing images if exist
36     if [[ -n "$(docker images | grep $DOCKER_REPO_NAME)" ]]; then
37         echo "Docker images to remove:"
38         docker images | head -1 && docker images | grep $DOCKER_REPO_NAME
39         image_ids=($(docker images | grep $DOCKER_REPO_NAME | awk '{print $3}'))
40         for id in "${image_ids[@]}"; do
41             if [[ -n "$(docker images|grep $DOCKER_REPO_NAME|grep $id)" ]]; then
42                 echo "Removing docker image $DOCKER_REPO_NAME:$id..."
43                 docker rmi -f $id
44             fi
45         done
46     fi
47 }
48
49
50 count=30 # docker build jobs might take up to ~30 min
51 while [[ -n `ps -ef| grep 'docker build' | grep $DOCKER_REPO_NAME | grep -v grep` ]]; do
52     echo "Build or cleanup of $DOCKER_REPO_NAME in progress. Waiting..."
53     sleep 60
54     count=$(( $count - 1 ))
55     if [ $count -eq 0 ]; then
56         echo "Timeout. Aborting..."
57         exit 1
58     fi
59 done
60
61 # Remove the existing containers and images before building
62 remove_containers_images
63
64 DOCKER_PATH=$WORKSPACE/$DOCKER_DIR
65
66 cd $DOCKER_PATH || exit 1
67 HOST_ARCH="$(uname -m)"
68 #If there is a patch for other arch then x86, apply the patch and
69 #replace Dockerfile file
70 dockerfile_patch="Dockerfile.${HOST_ARCH}.patch"
71 if [[ -f "${dockerfile_patch}" ]]; then
72         patch -f Dockerfile -p1 < "${dockerfile_patch}"
73 fi
74
75 # Get tag version
76 echo "Current branch: $BRANCH"
77
78 BUILD_BRANCH=$BRANCH
79
80 GERRIT_REFNAME=${GERRIT_REFNAME:-''}
81 RELEASE_VERSION=${GERRIT_REFNAME/refs\/tags\/}
82
83 # If we're being triggered by a comment-added job, then extract the tag
84 # from the comment and use that as the release version.
85 # Expected comment format: retag opnfv-x.y.z
86 if [[ "$GERRIT_EVENT_TYPE" == "comment-added" ]]; then
87     RELEASE_VERSION=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'retag' | awk '{print $2}')
88 fi
89
90 if [[ "$BRANCH" == "master" ]]; then
91     DOCKER_TAG="latest"
92 elif [[ -n "${RELEASE_VERSION-}" ]]; then
93     DOCKER_TAG=${RELEASE_VERSION}
94     if git checkout ${RELEASE_VERSION}; then
95         echo "Successfully checked out the git tag ${RELEASE_VERSION}"
96     else
97         echo "The tag ${RELEASE_VERSION} doesn't exist in the repository. Existing tags are:"
98         git tag
99         exit 1
100     fi
101 else
102     DOCKER_TAG="stable"
103 fi
104
105 if [[ -n "${COMMIT_ID-}" && -n "${RELEASE_VERSION-}" ]]; then
106     DOCKER_TAG=$RELEASE_VERSION
107     BUILD_BRANCH=$COMMIT_ID
108 fi
109
110 ARCH_BUILD_ARG=""
111 ARCH_TAG=${ARCH_TAG:-}
112 if [[ -n "${ARCH_TAG}" ]]; then
113     DOCKER_TAG=${ARCH_TAG}-${DOCKER_TAG}
114     ARCH_BUILD_ARG="--build-arg ARCH=${ARCH_TAG}"
115 fi
116
117 # Start the build
118 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_TAG"
119 echo "--------------------------------------------------------"
120 echo
121 cmd="docker build --pull=true --no-cache -t $DOCKER_REPO_NAME:$DOCKER_TAG --build-arg BRANCH=$BUILD_BRANCH
122     $ARCH_BUILD_ARG
123     -f $DOCKERFILE $DOCKER_PATH"
124
125 echo ${cmd}
126 ${cmd}
127
128
129 # list the images
130 echo "Available images are:"
131 docker images
132
133 # Push image to Dockerhub
134 if [[ "$PUSH_IMAGE" == "true" ]]; then
135     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
136     echo "--------------------------------------------------------"
137     echo
138     docker push $DOCKER_REPO_NAME:$DOCKER_TAG
139 fi
140
141 # Remove the existing containers and images after building
142 remove_containers_images