Setting the python oparent to 3.0.0 for osdf
[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 REPO=""
32
33 BUILD_ARGS="--no-cache"
34 if [ $HTTP_PROXY ]; then
35     BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
36 fi
37 if [ $HTTPS_PROXY ]; then
38     BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
39 fi
40
41 function log_ts() {  # Log message with timestamp
42     echo [DEBUG LOG at $(date -u +%Y%m%d:%H%M%S)] "$@"
43 }
44
45 function get_artifact_version() {
46     log_ts Get Maven Artifact version from pom.xml
47     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=//'`
48     log_ts Maven artifact version for OSDF is $MVN_ARTIFACT_VERSION
49     if [[ "$MVN_ARTIFACT_VERSION" =~ SNAPSHOT ]]; then
50         log_ts "REPO is snapshots";
51         REPO=snapshots
52     else
53         log_ts "REPO is releases";
54         REPO=releases
55     fi
56     BUILD_ARGS+=" --build-arg REPO=${REPO}"
57     BUILD_ARGS+=" --build-arg MVN_ARTIFACT_VERSION=${MVN_ARTIFACT_VERSION}"
58 }
59
60 function build_image() {
61     log_ts Building Image in folder: $PWD with build arguments ${BUILD_ARGS}
62     docker build ${BUILD_ARGS} -t ${IMAGE_NAME}:latest .
63     log_ts ... Built
64 }
65
66 function push_image() {
67     if [[ "$REPO" == snapshots ]]; then
68         push_snapshot_image
69     else
70         push_staging_image
71     fi
72 }
73
74
75 function push_snapshot_image(){
76     log_ts Tagging images: ${IMAGE_NAME}:\{${SNAPSHOT}-${TIMESTAMP},${SNAPSHOT}-latest\}
77     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
78     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${SNAPSHOT}-latest
79     log_ts ... Tagged images
80
81     log_ts Pushing images: ${IMAGE_NAME}:\{${SNAPSHOT}-${TIMESTAMP},${SNAPSHOT}-latest\}
82     docker push ${IMAGE_NAME}:${SNAPSHOT}-${TIMESTAMP}
83     docker push ${IMAGE_NAME}:${SNAPSHOT}-latest
84     log_ts ... Pushed images
85 }
86
87 function push_staging_image(){
88     log_ts Tagging images: ${IMAGE_NAME}:\{${STAGING}-${TIMESTAMP},${STAGING}-latest\}
89     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
90     docker tag ${IMAGE_NAME}:latest ${IMAGE_NAME}:${STAGING}-latest
91     log_ts ... Tagged images
92
93     log_ts Pushing images: ${IMAGE_NAME}:\{${STAGING}-${TIMESTAMP},${STAGING}-latest\}
94     docker push ${IMAGE_NAME}:${STAGING}-${TIMESTAMP}
95     docker push ${IMAGE_NAME}:${STAGING}-latest
96     log_ts ... Pushed images
97 }
98
99 (
100     get_artifact_version
101     # Switch to docker build directory
102     cd $(dirname $0)
103     build_image
104     push_image
105 )