Fix Docker image to use the nexus artifacts
[optf/osdf.git] / docker / build_image.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 #  ├── docker
9 #  │   ├── Dockerfile
10 #  │   └── build_image.sh   <--- THIS SCRIPT is here
11 #  ├── docs
12 #  ├── osdf
13 #  ├── pom.xml
14 #  ├── test
15 #  └── version.properties   <--- Version information here
16
17 set -e
18
19 # Folder settings
20 DOCKER_REPOSITORY=nexus3.onap.org:10003
21 ORG=onap
22 PROJECT=optf-osdf
23 IMAGE_NAME=$DOCKER_REPOSITORY/$ORG/$PROJECT
24
25 # Version properties
26 source version.properties
27 VERSION=$release_version
28 SNAPSHOT=$snapshot_version
29 STAGING=${release_version}-STAGING
30 TIMESTAMP=$(date +"%Y%m%dT%H%M%S")Z
31
32 BUILD_ARGS="--no-cache"
33 if [ $HTTP_PROXY ]; then
34     BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
35 fi
36 if [ $HTTPS_PROXY ]; then
37     BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
38 fi
39
40 function log_ts() {  # Log message with timestamp
41     echo [DEBUG LOG at $(date -u +%Y%m%d:%H%M%S)] "$@"
42 }
43
44 function get_artifact_version() {
45     log_ts Get Maven Artifact version from pom.xml
46     MVN_ARTIFACT_VERSION=`echo -e "setns x=http://maven.apache.org/POM/4.0.0 \n  xpath /x:project/x:version/text() "| xmllint --shell pom.xml | grep content | sed 's/.*content=//'`
47     log_ts Maven artifact version for OSDF is $MVN_ARTIFACT_VERSION
48     if [[ "$MVN_ARTIFACT_VERSION" =~ SNAPSHOT ]]; then
49         log_ts "REPO is snapshots";
50         REPO=snapshots
51     else
52         log_ts "REPO is releases";
53         REPO=releases
54     fi
55     BUILD_ARGS+=" --build-arg REPO=${REPO}"
56     BUILD_ARGS+=" --build-arg MVN_ARTIFACT_VERSION=${MVN_ARTIFACT_VERSION}"
57 }
58
59 function build_image() {
60     log_ts Building Image in folder: $PWD with build arguments ${BUILD_ARGS}
61     docker build ${BUILD_ARGS} -t ${IMAGE_NAME}:latest .
62     log_ts ... Built
63 }
64
65 function tag_image() {
66     log_ts Tagging images: ${IMAGE_NAME}:\{$SNAPSHOT-${TIMESTAMP},$STAGING-${TIMESTAMP},latest\}
67     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
68     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
69     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:latest
70     log_ts ... Tagged images
71 }
72
73 function push_image(){
74     log_ts Pushing images: ${IMAGE_NAME}:\{$SNAPSHOT-${TIMESTAMP},$STAGING-${TIMESTAMP},latest\}
75     docker push ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
76     docker push ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
77     docker push ${IMAGE_NAME}:latest
78     log_ts ... Pushed images
79 }
80
81 (
82     get_artifact_version
83     # Switch to docker build directory
84     cd $(dirname $0)
85     build_image
86     tag_image
87     push_image
88 )