BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 1 | #!/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 |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 21 | #Optionally push to external image repo |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 22 | |
| 23 | print_usage() { |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 24 | echo "Usage: build.sh no-push|<docker-hub-repo-name> [--tag <image-tag>]" |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 25 | exit 1 |
| 26 | } |
| 27 | |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 28 | if [ $# -lt 1 ] || [ $# -gt 2 ]; then |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 29 | print_usage |
| 30 | fi |
| 31 | |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 32 | IMAGE_NAME="pm-file-converter" |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 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 |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 41 | shift |
| 42 | while [ $# -ne 0 ]; do |
| 43 | if [ $1 == "--tag" ]; then |
| 44 | shift |
| 45 | if [ -z "$1" ]; then |
| 46 | print_usage |
| 47 | fi |
| 48 | IMAGE_TAG=$1 |
| 49 | echo "Setting image tag to: "$IMAGE_TAG |
| 50 | shift |
| 51 | else |
| 52 | echo "Unknown parameter: $1" |
| 53 | print_usage |
| 54 | fi |
| 55 | done |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 56 | |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 57 | ./gen-cert.sh |
| 58 | echo "" |
| 59 | echo "Certs generated" |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 60 | |
| 61 | IMAGE=$IMAGE_NAME:$IMAGE_TAG |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 62 | |
| 63 | export DOCKER_DEFAULT_PLATFORM=linux/amd64 |
| 64 | CURRENT_PLATFORM=$(docker system info --format '{{.OSType}}/{{.Architecture}}') |
| 65 | if [ $CURRENT_PLATFORM != $DOCKER_DEFAULT_PLATFORM ]; then |
| 66 | echo "Image may not work on the current platform: $CURRENT_PLATFORM, only platform $DOCKER_DEFAULT_PLATFORM supported" |
| 67 | fi |
| 68 | |
| 69 | echo "Building image: $IMAGE with architecture: $DOCKER_DEFAULT_PLATFORM" |
| 70 | |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 71 | docker build -t $IMAGE_NAME:$IMAGE_TAG . |
BjornMagnussonXA | 6ab531b | 2023-05-15 11:23:35 +0200 | [diff] [blame] | 72 | |
BjornMagnussonXA | c5655db | 2023-03-17 14:55:16 +0100 | [diff] [blame] | 73 | if [ $? -ne 0 ]; then |
| 74 | echo "BUILD FAILED" |
| 75 | exit 1 |
| 76 | fi |
| 77 | echo "BUILD OK" |
| 78 | |
| 79 | if [ "$REPO" != "" ]; then |
| 80 | echo "Tagging image" |
| 81 | NEW_IMAGE=$REPO/$IMAGE_NAME:$IMAGE_TAG |
| 82 | docker tag $IMAGE $NEW_IMAGE |
| 83 | if [ $? -ne 0 ]; then |
| 84 | echo "RE-TAGGING FAILED" |
| 85 | exit 1 |
| 86 | fi |
| 87 | echo "RE-TAG OK" |
| 88 | |
| 89 | echo "Pushing image $NEW_IMAGE" |
| 90 | docker push $NEW_IMAGE |
| 91 | if [ $? -ne 0 ]; then |
| 92 | echo "PUSHED FAILED" |
| 93 | echo " Perhaps not logged into docker-hub repo $REPO?" |
| 94 | exit 1 |
| 95 | fi |
| 96 | IMAGE=$NEW_IMAGE |
| 97 | echo "PUSH OK" |
| 98 | fi |
| 99 | |
| 100 | echo "IMAGE OK: $IMAGE" |
| 101 | echo "DONE" |