|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set_latest=false |
| 4 | + |
| 5 | +function error() { |
| 6 | + >&2 echo "${1}" |
| 7 | + exit 1 |
| 8 | +} |
| 9 | + |
| 10 | +function usage() { |
| 11 | + echo "usage: [sudo] build.sh [latest]" |
| 12 | + exit 0 |
| 13 | +} |
| 14 | + |
| 15 | +[[ $# -lt 2 ]] || usage |
| 16 | +if [[ $# -eq 1 ]]; then |
| 17 | + if [[ "${1}" == "latest" ]]; then |
| 18 | + set_latest=true |
| 19 | + else |
| 20 | + usage |
| 21 | + fi |
| 22 | +fi |
| 23 | + |
| 24 | +# figure out the app name (the dirname above this one) |
| 25 | +app_name=$(basename $(dirname $(pwd))) |
| 26 | + |
| 27 | +[[ -f "Dockerfile" ]] || error "Dockerfile not found in this directory" |
| 28 | +maintainer=$(cat Dockerfile | grep MAINTAINER | \ |
| 29 | + sed 's/^MAINTAINER[[:space:]]*\([^[[:space:]]*\)[[:space:]]*$/\1/') |
| 30 | + |
| 31 | +# the Docker repository to use for the generated image |
| 32 | +docker_repo="${maintainer}/${app_name}" |
| 33 | + |
| 34 | +function output_result() { |
| 35 | + if [[ "${1}" -eq 0 ]]; then |
| 36 | + echo -e "\e[32mOK\e[0m" |
| 37 | + else |
| 38 | + echo -e "\e[31mFailed!\e[0m" |
| 39 | + fi |
| 40 | + return "${1}" |
| 41 | +} |
| 42 | + |
| 43 | +function cleanup() { |
| 44 | + echo -n "Cleaning up... " |
| 45 | + rm -f "tmp_install.sh" |
| 46 | + output_result "${?}" |
| 47 | +} |
| 48 | + |
| 49 | +[[ "$(id -u)" -eq "0" ]] || error "Script must be run as root" |
| 50 | + |
| 51 | +docker=$(which docker) || error "Docker is needed" |
| 52 | +git=$(which git) || error "Git is needed" |
| 53 | + |
| 54 | +temp_dir=$(mktemp -d) |
| 55 | +trap 'cleanup' EXIT |
| 56 | + |
| 57 | +echo -n "Looking for last tag... " |
| 58 | +last_tag=$(git describe --abbrev=0 --tags 2> /dev/null) |
| 59 | +if [[ "${?}" -eq 0 ]]; then |
| 60 | + echo -e "\e[32m${last_tag}\e[0m" |
| 61 | +else |
| 62 | + echo -e "\e[31mFailed!\e[0m" |
| 63 | + error "Git repository must have at least one tag" |
| 64 | +fi |
| 65 | + |
| 66 | +echo -n "Creating temporary install script... " |
| 67 | +sed "s/{{check-tag}}/${last_tag}/" _install.sh > tmp_install.sh |
| 68 | +output_result "${?}" || exit |
| 69 | + |
| 70 | +echo "Running docker build:" |
| 71 | +docker build -t="${docker_repo}:${last_tag}" . |
| 72 | +if [[ "${?}" -ne 0 ]]; then |
| 73 | + echo -e "\n\n\e[31m***\e[0m Docker execution Failed!\n" |
| 74 | +fi |
| 75 | + |
| 76 | +if [[ "${set_latest}" == "true" ]]; then |
| 77 | + echo -n "Retrieving image ID... " |
| 78 | + image_id=$(docker images | grep "${docker_repo}" | grep "${last_tag}" | sed 's/[[:space:]]\+/ /g' | cut -d' ' -f 3) |
| 79 | + if [[ "${?}" -eq 0 ]]; then |
| 80 | + echo -e "\e[32m${image_id}\e[0m" |
| 81 | + echo -n "Setting latest tag... " |
| 82 | + docker tag "${image_id}" "${docker_repo}:latest" |
| 83 | + output_result "${?}" |
| 84 | + else |
| 85 | + echo -e "\e[31mFailed!\e[0m" |
| 86 | + error "Git repository must have at least one tag" |
| 87 | + fi |
| 88 | +else |
| 89 | + echo "Skip tagging image as latest" |
| 90 | +fi |
| 91 | + |
| 92 | +# cleanup does this, but still, let's be nice |
| 93 | +echo -n "Deleting temporary install script... " |
| 94 | +rm -f tmp_install.sh |
| 95 | +output_result "${?}" || exit |
0 commit comments