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