add python compatibility module
[optf/has.git] / build-dockers.sh
1 #!/bin/bash
2
3 # The script starts in the root folder of the repo, which has the following outline
4 # We fetch the version information from version.properties, build docker files and
5 # do a docker push. Since the job will be run under Jenkins, it will have the Nexus
6 # credentials
7
8 set -e
9
10 BUILD_ARGS="--no-cache"
11 ORG="onap"
12 PROJECT="optf-has"
13 DOCKER_REPOSITORY="nexus3.onap.org:10003"
14 IMAGE_NAME="${DOCKER_REPOSITORY}/${ORG}/${PROJECT}"
15
16 # Version properties
17 source version.properties
18 VERSION=$release_version
19 SNAPSHOT=$snapshot_version
20 STAGING=${release_version}-STAGING
21 TIMESTAMP=$(date +"%Y%m%dT%H%M%S")Z
22 REPO=""
23
24 if [ $HTTP_PROXY ]; then
25     BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
26 fi
27 if [ $HTTPS_PROXY ]; then
28     BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
29 fi
30
31 function log_ts() {  # Log message with timestamp
32     echo [DEBUG LOG at $(date -u +%Y%m%d:%H%M%S)] "$@"
33 }
34
35 function get_artifact_version() {
36     log_ts Get Maven Artifact version from pom.xml
37     MVN_ARTIFACT_VERSION=`echo -e "setns x=http://maven.apache.org/POM/4.0.0 \n  xpath /x:project/x:version/text() "| xmllint --shell conductor/pom.xml | grep content | sed 's/.*content=//'`
38     log_ts Maven artifact version for HAS is $MVN_ARTIFACT_VERSION
39     if [[ "$MVN_ARTIFACT_VERSION" =~ SNAPSHOT ]]; then
40         log_ts "REPO is snapshots";
41         REPO=snapshots
42     else
43         log_ts "REPO is releases";
44         REPO=releases
45     fi
46     BUILD_ARGS+=" --build-arg REPO=${REPO}"
47     BUILD_ARGS+=" --build-arg MVN_ARTIFACT_VERSION=${MVN_ARTIFACT_VERSION}"
48 }
49
50 function build_image() {
51     log_ts Building Image in folder: $PWD with build arguments ${BUILD_ARGS}
52     docker build ${BUILD_ARGS} -t ${IMAGE_NAME}:latest conductor/docker
53     log_ts ... Built
54 }
55
56 function push_image() {
57     if [[ "$REPO" == snapshots ]]; then
58         push_snapshot_image
59     else
60         push_staging_image
61     fi
62 }
63
64 function push_snapshot_image(){
65     log_ts Tagging images: ${IMAGE_NAME}:\{${SNAPSHOT}-${TIMESTAMP},${SNAPSHOT}-latest\}
66     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
67     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-latest
68     log_ts ... Tagged images
69
70     log_ts Pushing images: ${IMAGE_NAME}:\{${SNAPSHOT}-${TIMESTAMP},${SNAPSHOT}-latest\}
71     docker push ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
72     docker push ${IMAGE_NAME}:${SNAPSHOT}-latest
73     log_ts ... Pushed images
74 }
75
76 function push_staging_image(){
77     log_ts Tagging images: ${IMAGE_NAME}:\{${STAGING}-${TIMESTAMP},${STAGING}-latest\}
78     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
79     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-latest
80     log_ts ... Tagged images
81
82     log_ts Pushing images: ${IMAGE_NAME}:\{${STAGING}-${TIMESTAMP},${STAGING}-latest\}
83     docker push ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
84     docker push ${IMAGE_NAME}:${STAGING}-latest
85     log_ts ... Pushed images
86 }
87
88 (
89     get_artifact_version
90     build_image
91     push_image
92 )