Merge "Add cFW scripts folder"
[multicloud/k8s.git] / deployments / build.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 Intel Corporation
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
11 set -o nounset
12 set -o pipefail
13
14 k8s_path="$(git rev-parse --show-toplevel)"
15 export GOPATH=$k8s_path
16
17 VERSION="0.1.0-SNAPSHOT"
18 export IMAGE_NAME="nexus3.onap.org:10003/onap/multicloud/k8s"
19
20 function _compile_src {
21     echo "Compiling source code"
22     pushd $k8s_path/src/k8splugin/
23     make
24     popd
25 }
26
27 function _move_bin {
28     echo "Moving binaries"
29     rm -f k8plugin *so
30     mv $k8s_path/src/k8splugin/k8plugin .
31     mv $k8s_path/src/k8splugin/plugins/*.so .
32 }
33
34 function _cleanup {
35     echo "Cleaning previous execution"
36     docker-compose kill
37     image=$(grep "image.*k8plugin" docker-compose.yml)
38     if [[ -n ${image} ]]; then
39         docker images ${image#*:} -q | xargs docker rmi -f
40     fi
41     docker ps -a --filter "status=exited" -q | xargs docker rm
42 }
43
44 function _build_docker {
45     echo "Building docker image"
46     docker-compose build --no-cache
47 }
48
49 function _push_image {
50     local tag_name=${IMAGE_NAME}:${1:-latest}
51
52     echo "Start push {$tag_name}"
53     docker push ${IMAGE_NAME}:latest
54     docker tag ${IMAGE_NAME}:latest ${tag_name}
55     docker push ${tag_name}
56 }
57
58 if [[ -n "${JENKINS_HOME+x}" ]]; then
59     set -o xtrace
60     _compile_src
61     _move_bin
62     _build_docker
63     _push_image $VERSION
64 else
65     source /etc/environment
66
67     _compile_src
68     _move_bin
69     _cleanup
70     _build_docker
71 fi