blob: 69bcc9a0043954aa91be56d55a4871f823aebd46 [file] [log] [blame]
BjornMagnussonXAc5655db2023-03-17 14:55:16 +01001#!/bin/bash
2
3# ============LICENSE_START===============================================
4# Copyright (C) 2023 Nordix Foundation. All rights reserved.
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 docker hub repo
22
23print_usage() {
24 echo "Usage: build.sh no-push|bm|<docker-hub-repo-name> [<image-tag>]"
25 exit 1
26}
27
28if [ $# -ne 1 ] && [ $# -ne 2 ]; then
29 print_usage
30fi
31
32IMAGE_NAME="kafka-pm-producer"
33IMAGE_TAG="latest"
34REPO=""
35if [ $1 == "no-push" ]; then
36 echo "Only local image build"
37else
38 REPO=$1
39 echo "Attempt to push built image to: "$REPO
40fi
41
42if [ "$2" != "" ]; then
43 IMAGE_TAG=$2
44fi
45 echo "Setting image tag to: "$IMAGE_TAG
46
47IMAGE=$IMAGE_NAME:$IMAGE_TAG
48echo "Building image $IMAGE"
49docker build -t $IMAGE_NAME:$IMAGE_TAG .
50if [ $? -ne 0 ]; then
51 echo "BUILD FAILED"
52 exit 1
53fi
54echo "BUILD OK"
55
56if [ "$REPO" != "" ]; then
57 echo "Tagging image"
58 NEW_IMAGE=$REPO/$IMAGE_NAME:$IMAGE_TAG
59 docker tag $IMAGE $NEW_IMAGE
60 if [ $? -ne 0 ]; then
61 echo "RE-TAGGING FAILED"
62 exit 1
63 fi
64 echo "RE-TAG OK"
65
66 echo "Pushing image $NEW_IMAGE"
67 docker push $NEW_IMAGE
68 if [ $? -ne 0 ]; then
69 echo "PUSHED FAILED"
70 echo " Perhaps not logged into docker-hub repo $REPO?"
71 exit 1
72 fi
73 IMAGE=$NEW_IMAGE
74 echo "PUSH OK"
75fi
76
77echo "IMAGE OK: $IMAGE"
78echo "DONE"