From 23ae22e44ef6914cd39981cf6c043370f8b640fd Mon Sep 17 00:00:00 2001 From: Daya Adianto <dayaadianto@cs.ui.ac.id> Date: Thu, 29 Aug 2019 21:09:45 +0700 Subject: [PATCH] Create build script for building container image --- .devcontainer/Dockerfile | 2 +- Dockerfile | 41 ++++++++++++++++++++++++++------------- build.sh | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 build.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 8d87309..425c153 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -41,4 +41,4 @@ RUN wget -O /usr/local/bin/hadolint https://github.com/hadolint/hadolint/release && chmod +x /usr/local/bin/hadolint # Switch back to dialog for any ad-hoc use of apt-get -ENV DEBIAN_FRONTEND= +ENV DEBIAN_FRONTEND='' diff --git a/Dockerfile b/Dockerfile index 106cebe..0d3b1fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,16 +38,31 @@ WORKDIR /home/sonar ENTRYPOINT ["/bin/bash"] # Container image metadata -## Note to editors: metadata values for `created`, `version`, `revision`, and -## `ref.name` keys must be provided during build process, i.e. `docker build` -## invocation -LABEL org.opencontainers.image.created="" -LABEL org.opencontainers.image.authors="Daya Adianto <dayaadianto@cs.ui.ac.id>" -LABEL org.opencontainers.image.source="https://gitlab.cs.ui.ac.id/pmpl/sonar-scanner-cli-image" -LABEL org.opencontainers.image.version="" -LABEL org.opencontainers.image.revision="" -LABEL org.opencontainers.image.vendor="Faculty of Computer Science Universitas Indonesia" -LABEL org.opencontainers.image.licenses="LGPL-3.0" -LABEL org.opencontainers.image.ref.name="" -LABEL org.opencontainers.image.title="Sonar Scanner CLI Image" -LABEL org.opencontainers.image.description="Sonar Scanner CLI bundled in a container image" +## Note to editors: metadata values for `created`, `version`, and `revision` +## keys must be provided during build process, i.e. `docker build` invocation. +## It is also possible to pass other metadata values via build arguments. +ARG IMAGE_CREATED="" +ARG IMAGE_SOURCE="https://gitlab.cs.ui.ac.id/pmpl/sonar-scanner-cli-image" +ARG IMAGE_VERSION="" +ARG IMAGE_REVISION="" +ARG IMAGE_VENDOR="Faculty of Computer Science Universitas Indonesia" +ARG IMAGE_TITLE="Sonar Scanner CLI Image" +ARG IMAGE_DESCRIPTION="Sonar Scanner CLI bundled in a container image." +LABEL org.opencontainers.image.created=${IMAGE_CREATED} \ + org.opencontainers.image.authors="Daya Adianto <dayaadianto@cs.ui.ac.id>" \ + org.opencontainers.image.source=${IMAGE_SOURCE} \ + org.opencontainers.image.version=${IMAGE_VERSION} \ + org.opencontainers.image.revision=${IMAGE_REVISION} \ + org.opencontainers.image.vendor=${IMAGE_VENDOR} \ + org.opencontainers.image.licenses="LGPL-3.0" \ + org.opencontainers.image.title=${IMAGE_TITLE} \ + org.opencontainers.image.description=${IMAGE_DESCRIPTION} + +## Note to editors: The following label assignments are to ensure backward +## compatibility with Label Schema standard +LABEL org.label-schema.build-date=${IMAGE_CREATED} \ + org.label-schema.vcs-url=${IMAGE_SOURCE} \ + org.label-schema.version=${IMAGE_VERSION} \ + org.label-schema.vendor=${IMAGE_VENDOR} \ + org.label-schema.title=${IMAGE_TITLE} \ + org.label-schema.description=${IMAGE_DESCRIPTION} diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..ecb16b5 --- /dev/null +++ b/build.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +# Exit immediately on any failure +set -e +set -o pipefail + +if ! [ -x "$(command -v docker)" ]; then + echo 'Error: docker is not installed.' >&2 + exit 1 +fi + +if ! [ -x "$(command -v git)" ]; then + echo 'Error: git is not installed.' >&2 + exit 1 +fi + +if ! [ -x "$(command -v awk)" ]; then + echo 'Error: awk is not installed.' >&2 + exit 1 +fi + +if ! [ -n "$1" ]; then + echo "Error: missing 1st argument of $0, i.e. namespace of the container image." >&2 + exit 1 +fi + +if ! [ -n "$2" ]; then + echo "Error: missing 2nd argument of $0, i.e. version of the container image." >&2 + exit 1 +fi + +IMAGE_NAMESPACE="$1" +IMAGE_VERSION="$2" + +docker build \ + --build-arg=IMAGE_CREATED="$(date --utc --rfc-3339=seconds)" \ + --build-arg=IMAGE_VERSION="${IMAGE_VERSION}" \ + --build-arg=IMAGE_REVISION="$(git show --pretty=oneline | tac | tail -n 1 | awk '{print $1}')" \ + --tag="${IMAGE_NAMESPACE}/sonar-scanner-cli:${IMAGE_VERSION}" \ + . + +exit 0 -- GitLab