Hello World service for NONRTRIC - Unit Tests Added
[nonrtric.git] / test / servicestub / build.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2023: OpenInfra Foundation Europe.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 # Build image from Dockerfile with/without custom image tag
21 # Optionally push to external image repo
22
23 print_usage() {
24     echo "Usage: build.sh no-push|<docker-hub-repo-name> [<image-tag>]"
25     exit 1
26 }
27
28 if [ $# -ne 1 ] && [ $# -ne 2 ]; then
29     print_usage
30 fi
31
32 IMAGE_NAME="nonrtric-hello-world"
33 IMAGE_TAG="latest"
34 REPO=""
35 if [ $1 == "no-push" ]; then
36     echo "Only local image build"
37 else
38     REPO=$1
39     echo "Attempt to push built image to: "$REPO
40 fi
41
42 shift
43 while [ $# -ne 0 ]; do
44     if [ $1 == "--tag" ]; then
45         shift
46         if [ -z "$1" ]; then
47             print_usage
48         fi
49         IMAGE_TAG=$1
50         echo "Setting image tag to: "$IMAGE_TAG
51         shift
52     else
53         echo "Unknown parameter: $1"
54         print_usage
55     fi
56 done
57
58 IMAGE=$IMAGE_NAME:$IMAGE_TAG
59
60 export DOCKER_DEFAULT_PLATFORM=linux/amd64
61 CURRENT_PLATFORM=$(docker system info --format '{{.OSType}}/{{.Architecture}}')
62 if [ $CURRENT_PLATFORM != $DOCKER_DEFAULT_PLATFORM ]; then
63     echo "Image may not work on the current platform: $CURRENT_PLATFORM, only platform $DOCKER_DEFAULT_PLATFORM supported"
64 fi
65
66 echo "Building image $IMAGE"
67 docker build -t $IMAGE_NAME:$IMAGE_TAG .
68 if [ $? -ne 0 ]; then
69     echo "BUILD FAILED"
70     exit 1
71 fi
72 echo "BUILD OK"
73
74 if [ "$REPO" != "" ]; then
75     echo "Tagging image"
76     NEW_IMAGE=$REPO/$IMAGE_NAME:$IMAGE_TAG
77     docker tag $IMAGE $NEW_IMAGE
78     if [ $? -ne 0 ]; then
79         echo "RE-TAGGING FAILED"
80         exit 1
81     fi
82     echo "RE-TAG OK"
83
84     echo "Pushing image $NEW_IMAGE"
85     docker push $NEW_IMAGE
86     if [ $? -ne 0 ]; then
87         echo "PUSHED FAILED"
88         echo " Perhaps not logged into docker-hub repo $REPO?"
89         exit 1
90     fi
91     IMAGE=$NEW_IMAGE
92     echo "PUSH OK"
93 fi
94
95 echo "IMAGE OK: $IMAGE"
96 echo "DONE"