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