Fix docker build error
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / deployment / src / main / resources / build_image.sh
1 #!/bin/bash
2 #
3 # Copyright 2017, Nokia Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 if [ "a$1" != "abuildDocker" ] ; then
19   echo "Skipping building Docker image"
20   echo "If you would like to build the docker image by script execute $0 buildDocker" 
21   echo "If you would like to build and push the docker image by script execute $0 buildDocker pushImage" 
22   echo "If you would like to build the docker image by maven execute mvn package -Dexec.args=\"buildDocker\"" 
23   echo "If you would like to push the docker image by maven execute mvn package -Dexec.args=\"buildDocker pushImage\""
24   exit
25 fi 
26
27 DIRNAME=`dirname $0`
28 DOCKER_BUILD_DIR=`cd $DIRNAME/; pwd`
29 echo "----- Build directory ${DOCKER_BUILD_DIR}"
30 cd ${DOCKER_BUILD_DIR}
31
32 #VERSION=`xmlstarlet sel -t -v "/_:project/_:version" ../../../pom.xml | sed 's/-SNAPSHOT//g'`
33 VERSION=`xmlstarlet sel -t -v "/_:project/_:version" ../../../pom.xml`
34 echo "------ Detected version: $VERSION"
35
36 PROJECT="vfc"
37 IMAGE="nfvo/svnfm/nokiav2"
38 DOCKER_REPOSITORY="nexus3.onap.org:10003"
39 ORG="onap"
40 BUILD_ARGS="--no-cache"
41 # it looks like that ONAP jenkins does not support squash
42 #BUILD_ARGS="--no-cache --squash"
43
44 IMAGE_NAME="${DOCKER_REPOSITORY}/${ORG}/${PROJECT}/${IMAGE}"
45 TIMESTAMP=$(date +"%Y%m%dT%H%M%S")
46
47 if [ $HTTP_PROXY ]; then
48     echo "----- Using HTTP proxy ${HTTP_PROXY}"
49     BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
50 fi
51
52 if [ $HTTPS_PROXY ]; then
53     echo "----- Using HTTPS proxy ${HTTPS_PROXY}"
54     BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
55 fi
56
57 function build_image {
58     echo "Start build docker image: ${IMAGE_NAME}"
59     echo "docker build --build-arg VERSION=${VERSION} ${BUILD_ARGS} -t ${IMAGE_NAME}:latest ."
60     docker build --build-arg VERSION=${VERSION} ${BUILD_ARGS} -t ${IMAGE_NAME}:latest .
61 }
62
63 function push_image_tag {
64     TAG_NAME=$1
65     echo "Start push ${TAG_NAME}"
66     docker tag ${IMAGE_NAME}:latest ${TAG_NAME}
67     docker push ${TAG_NAME}
68 }
69
70 function push_image {
71     echo "Start push ${IMAGE_NAME}:latest"
72     docker push ${IMAGE_NAME}:latest
73     
74     push_image_tag ${IMAGE_NAME}:${VERSION}-SNAPSHOT-latest
75     push_image_tag ${IMAGE_NAME}:${VERSION}-STAGING-latest
76     push_image_tag ${IMAGE_NAME}:${VERSION}-STAGING-${TIMESTAMP}
77 }
78
79 build_image
80
81 if [ "a$2" != "apushImage" ]; then
82    echo "Skipping image pushing"
83    echo "If you would like to push the docker image by maven execute mvn package -Dexec.args=\"buildDocker pushImage\""
84 else
85    echo "Pushing image"
86    push_image
87 fi