Fix Docker image to use the nexus artifacts
[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     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=//'`
37     log_ts Maven artifact version for HAS is $MVN_ARTIFACT_VERSION
38     if [[ "$MVN_ARTIFACT_VERSION" =~ SNAPSHOT ]]; then
39         log_ts "REPO is snapshots";
40         REPO=snapshots
41     else
42         log_ts "REPO is releases";
43         REPO=releases
44     fi
45     BUILD_ARGS+=" --build-arg REPO=${REPO}"
46     BUILD_ARGS+=" --build-arg MVN_ARTIFACT_VERSION=${MVN_ARTIFACT_VERSION}"
47 }
48
49 function build_image() {
50     log_ts Building Image in folder: $PWD with build arguments ${BUILD_ARGS}
51     docker build ${BUILD_ARGS} -t ${IMAGE_NAME}:latest conductor/docker
52     log_ts ... Built
53 }
54
55 function tag_image() {
56     log_ts Tagging images: ${IMAGE_NAME}:\{$SNAPSHOT-${TIMESTAMP},$STAGING-${TIMESTAMP},latest\}
57     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
58     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
59     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:latest
60     log_ts ... Tagged images
61 }
62
63 function push_image(){
64     log_ts Pushing images: ${IMAGE_NAME}:\{$SNAPSHOT-${TIMESTAMP},$STAGING-${TIMESTAMP},latest\}
65     docker push ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
66     docker push ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
67     docker push ${IMAGE_NAME}:latest
68     log_ts ... Pushed images
69 }
70
71 (
72     get_artifact_version
73     build_image
74     tag_image
75     push_image
76 )