github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/build/build.sh (about)

     1  #!/bin/bash
     2  
     3  # ----------------------------------------------------------------------
     4  # A bash script to parametrically build and tag Docker images for Latte.
     5  # Written by: Raphael Reyna
     6  # ----------------------------------------------------------------------
     7  
     8  DOCKERFILE="\
     9  # Build Stage
    10  FROM golang:1.16 AS build-stage
    11  ADD ./ /latte
    12  RUN cd /latte && env GOOS=linux GOARCH=amd64 go build {BUILD_TAGS} ./cmd/latte
    13  
    14  # Final Stage
    15  FROM ubuntu:21.04
    16  MAINTAINER Raphael Reyna <raphaelreyna@protonmail.com>
    17  RUN apt update -q \\
    18    && env DEBIAN_FRONTEND=noninteractive \\
    19    apt install -qy {TEXLIVE_PACKAGES} \\
    20    && rm -rf /var/lib/apt/lists/*
    21  COPY --from=build-stage /latte/latte /bin/latte
    22  CMD [\"latte\", \"server\"]\
    23  "
    24  
    25  IMAGE_TAG=""
    26  PACKAGES=""
    27  TAGS=""
    28  SKIP=""
    29  NO_CONFIRM=""
    30  ME=`basename "$0"`
    31  DESCRIPTOR=""
    32  USERNAME="raphaelreyna"
    33  HOSTNAME=""
    34  
    35  function usage {
    36      echo "\
    37  Usage: ${ME} [-h] [-s] [-b build_tag] [-p latex_package] [-t image_tag] \
    38  [-d descriptor] [-H host_name] [-u user_name]
    39  
    40  Description: ${ME} parametrically builds and tags Docker images for Latte.
    41               The tag used for the image follows the template bellow:
    42                   host_name/user_name/image_name:image_tag-descriptor
    43  
    44  Flags:
    45    -b Build tags to be passed to the Go compiler.
    46  
    47    -d Descriptor to be used when tagging the built image.
    48  
    49    -h Show this help text.
    50  
    51    -p LaTeX package to install, must be available in default Ubuntu repos.
    52       (default: texlive-base)
    53  
    54    -s Skip the image building stage. The generated Dockerfile will be sent to std out.
    55  
    56    -t Tag the Docker image.
    57       The image will be tagged with the current git tag if this flag is omitted.
    58       If no git tag is found, we default to using 'latest' as the image tag.
    59  
    60    -u Username to be used when tagging the built image.
    61       (default: raphaelreyna)
    62  
    63    -H Hostname to be used when tagging the built image.
    64  
    65    -y Do not ask to confirm image tab before building image.\
    66  "
    67      exit 0
    68  }
    69  
    70  function generate_dockerfile {
    71      TMP_DOCKERFILE="Dockerfile.tmp"
    72      [[ "${TAGS}" != "" ]] && TAGS="-tags ${TAGS:1}"
    73      # Make sure we have at least a minimal tex installation
    74      [[ "${PACKAGES}" == "" ]] && PACKAGES="texlive-base"
    75      echo "${DOCKERFILE}" | \
    76          sed "s/{BUILD_TAGS}/${TAGS}/g;s/{TEXLIVE_PACKAGES}/${PACKAGES}/g" > "${TMP_DOCKERFILE}"
    77  }
    78  
    79  function build_image {
    80      # Make sure we have an image build tag
    81      if [ "${IMAGE_TAG}" == "" ]; then
    82          GIT_TAG=`git describe --tags --abbrev=0 2> /dev/null`
    83          if [ "${GIT_TAG}" = "" ]; then
    84              IMAGE_TAG="latest"
    85          else
    86              IMAGE_TAG="${GIT_TAG}"
    87          fi
    88      fi
    89      [[ "${HOSTNAME}" != "" ]] && HOSTNAME="${HOSTNAME}/"
    90      [[ "${DESCRIPTOR}" != "" ]] && DESCRIPTOR="-${DESCRIPTOR}"
    91      IMAGE_NAME="${HOSTNAME}${USERNAME}/latte:${IMAGE_TAG}${DESCRIPTOR}"
    92      if [ "${NO_CONFIRM}" != "T" ]; then
    93          PROMPT="${ME}: tag image as ${IMAGE_NAME} (y/N)? "
    94          read -p "${PROMPT}" userconfirmation
    95          if [ "$userconfirmation" != "y" ]; then
    96              clean_up
    97              exit 0
    98          fi
    99      fi
   100      docker build --tag "${IMAGE_NAME}" -f "${TMP_DOCKERFILE}" ..
   101  }
   102  
   103  function clean_up {
   104      rm "${TMP_DOCKERFILE}"
   105  }
   106  
   107  while getopts 'b:d:t:p:syu:H:' flag; do
   108      case "${flag}" in
   109          b) TAGS="${TAGS},${OPTARG}" ;;
   110          d) DESCRIPTOR="${OPTARG}" ;;
   111          H) HOSTNAME="${OPTARG}" ;;
   112          p) PACKAGES="${PACKAGES} ${OPTARG}" ;;
   113          t) IMAGE_TAG="${OPTARG}" ;;
   114          s) SKIP="T" ;;
   115          u) USERNAME="${OPTARG}" ;;
   116          y) NO_CONFIRM="T" ;;
   117          \?) usage ;;
   118      esac
   119  done
   120  
   121  generate_dockerfile
   122  if [ "${SKIP}" == "T" ]; then
   123      cat "${TMP_DOCKERFILE}"
   124      clean_up
   125      exit 0
   126  fi
   127  build_image
   128  clean_up