Added missing sudo privileges
[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
23 if [ $HTTP_PROXY ]; then
24     BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
25 fi
26 if [ $HTTPS_PROXY ]; then
27     BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
28 fi
29
30 function log_ts() {  # Log message with timestamp
31     echo [DEBUG LOG at $(date -u +%Y%m%d:%H%M%S)] "$@"
32 }
33
34 function get_artifact_version() {
35     log_ts Get Maven Artifact version from pom.xml
36     sudo apt-get install libxml2-utils
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 tag_image() {
57     log_ts Tagging images: ${IMAGE_NAME}:\{$SNAPSHOT-${TIMESTAMP},$STAGING-${TIMESTAMP},latest\}
58     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
59     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
60     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:latest
61     log_ts ... Tagged images
62 }
63
64 function push_image(){
65     log_ts Pushing images: ${IMAGE_NAME}:\{$SNAPSHOT-${TIMESTAMP},$STAGING-${TIMESTAMP},latest\}
66     docker push ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
67     docker push ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
68     docker push ${IMAGE_NAME}:latest
69     log_ts ... Pushed images
70 }
71
72 (
73     get_artifact_version
74     build_image
75     tag_image
76     push_image
77 )