blob: 8647a0b6f986b0e94f0712479fcce41b7cd69d93 [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
BjornMagnussonXA6ab531b2023-05-15 11:23:35 +020021# Optionally push to external image repo
BjornMagnussonXAc5655db2023-03-17 14:55:16 +010022
23print_usage() {
24 echo "Usage: build.sh no-push|<docker-hub-repo-name> [<image-tag>]"
25 exit 1
26}
27
BjornMagnussonXA6ab531b2023-05-15 11:23:35 +020028if [ $# -lt 1 ] || [ $# -gt 2 ]; then
BjornMagnussonXAc5655db2023-03-17 14:55:16 +010029 print_usage
30fi
31
32IMAGE_NAME="pm-https-server"
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
BjornMagnussonXA6ab531b2023-05-15 11:23:35 +020042shift
43while [ $# -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
56done
57
58./gen-cert.sh
59
60echo ""
61echo "Certs generated"
BjornMagnussonXAc5655db2023-03-17 14:55:16 +010062
63IMAGE=$IMAGE_NAME:$IMAGE_TAG
BjornMagnussonXA6ab531b2023-05-15 11:23:35 +020064
65export DOCKER_DEFAULT_PLATFORM=linux/amd64
66CURRENT_PLATFORM=$(docker system info --format '{{.OSType}}/{{.Architecture}}')
67if [ $CURRENT_PLATFORM != $DOCKER_DEFAULT_PLATFORM ]; then
68 echo "Image may not work on the current platform: $CURRENT_PLATFORM, only platform $DOCKER_DEFAULT_PLATFORM supported"
69fi
70
71echo "Building image: $IMAGE with architecture: $DOCKER_DEFAULT_PLATFORM"
72
BjornMagnussonXAc5655db2023-03-17 14:55:16 +010073docker build -t $IMAGE_NAME:$IMAGE_TAG .
BjornMagnussonXA6ab531b2023-05-15 11:23:35 +020074
BjornMagnussonXAc5655db2023-03-17 14:55:16 +010075if [ $? -ne 0 ]; then
76 echo "BUILD FAILED"
77 exit 1
78fi
79echo "BUILD OK"
80
81if [ "$REPO" != "" ]; then
82 echo "Tagging image"
83 NEW_IMAGE=$REPO/$IMAGE_NAME:$IMAGE_TAG
84 docker tag $IMAGE $NEW_IMAGE
85 if [ $? -ne 0 ]; then
86 echo "RE-TAGGING FAILED"
87 exit 1
88 fi
89 echo "RE-TAG OK"
90
91 echo "Pushing image $NEW_IMAGE"
92 docker push $NEW_IMAGE
93 if [ $? -ne 0 ]; then
94 echo "PUSHED FAILED"
95 echo " Perhaps not logged into docker-hub repo $REPO?"
96 exit 1
97 fi
98 IMAGE=$NEW_IMAGE
99 echo "PUSH OK"
100fi
101
102echo "IMAGE OK: $IMAGE"
103echo "DONE"