From 8145dba1576256b2e659d6419cfada087b748a59 Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Tue, 29 Jun 2021 17:47:37 +0100 Subject: [PATCH] chore(docker): add tools for generating docker images --- .dockerignore | 2 - .schmersion.yaml | 14 +++++++ Dockerfile | 41 +++++++++++-------- Makefile | 98 ++++++++++++++++++++++++++++++++++++++++++++++ PotamusFile | 2 - docker/wait-for.sh | 60 ++++++++++++++++++++++++++++ 6 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 .schmersion.yaml create mode 100644 Makefile delete mode 100644 PotamusFile create mode 100755 docker/wait-for.sh diff --git a/.dockerignore b/.dockerignore index 9fb0b66..9d3991d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -24,5 +24,3 @@ node_modules Dockerfile vendor/bundle .cache -docker/* -PotamusFile diff --git a/.schmersion.yaml b/.schmersion.yaml new file mode 100644 index 0000000..794e7c9 --- /dev/null +++ b/.schmersion.yaml @@ -0,0 +1,14 @@ +exports: + - name: CHANGELOG.md + formatter: markdown + options: + title: CHANGELOG + description: This file contains all the latest changes and updates to Postal. + sections: + - title: Features + types: [feat] + - title: Bug Fixes + types: [fix] +# +# There are additional options you can define, check out the README for details. +# https://github.com/krystal/schmersion diff --git a/Dockerfile b/Dockerfile index a73a1d1..b0225b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,19 @@ -FROM ruby:2.6 +FROM ruby:2.6 AS base -RUN apt-get update -RUN apt-get install software-properties-common -y - -# Setup additional repositories -RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 -RUN add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.coreix.net/mariadb/repo/10.1/ubuntu xenial main' -RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - -RUN apt-get update +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + software-properties-common \ + && apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 \ + && add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirrors.coreix.net/mariadb/repo/10.1/ubuntu xenial main' \ + && (curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -) \ + && (echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list) \ + && (curl -sL https://deb.nodesource.com/setup_12.x | bash -) \ + && rm -rf /var/lib/apt/lists/* # Install main dependencies -RUN apt-get install -y \ +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ build-essential \ curl \ libmariadbclient-dev \ @@ -24,9 +27,7 @@ RUN mkdir -p /opt/postal/app /opt/postal/config WORKDIR /opt/postal/app # Install bundler -RUN gem install bundler --no-doc -RUN bundle config frozen 1 -RUN bundle config build.sassc --disable-march-tune-native +RUN gem install bundler -v 2.1.4 --no-doc # Install the latest and active gem dependencies and re-run # the appropriate commands to handle installs. @@ -34,15 +35,21 @@ COPY Gemfile Gemfile.lock ./ RUN bundle install -j 4 # Copy the application (and set permissions) +COPY ./docker/wait-for.sh /docker-entrypoint.sh COPY --chown=postal . . +# Export the version +ARG VERSION=unspecified +RUN echo $VERSION > VERSION + +# Set the CMD +ENTRYPOINT [ "/docker-entrypoint.sh" ] +CMD ["bundle", "exec"] + +FROM base AS prod # Copy temporary configuration file which can be used for # running the asset precompilation. COPY --chown=postal config/postal.defaults.yml /opt/postal/config/postal.yml -# Precompile assets RUN POSTAL_SKIP_CONFIG_CHECK=1 RAILS_GROUPS=assets bundle exec rake assets:precompile RUN touch /opt/postal/app/public/assets/.prebuilt - -# Set the CMD -CMD ["bundle", "exec"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..753c1ad --- /dev/null +++ b/Makefile @@ -0,0 +1,98 @@ +# ============================================================================== +# Welcome to the Makefile +# ============================================================================== +# +# This Makefile contains a series of tasks which can help with building, testing +# and working with the app. The following tasks are available to you: +# +# Image building & releasing actions: +# +# make docker-build - Builds a production Docker image. +# make docker-ci-build - Builds an image without any asset compilation, +# ideal for running test suites and similar tasks. +# make docker-image - Builds a production Docker image and tags it as +# appropriate based on the current branch & tag. +# make docker-release - Builds a production Docker image and uploads to the +# registry. +# + +# ============================================================================== +# Configuration +# ============================================================================== + +# Docker image name to release the production image as. +DOCKER_IMAGE := postalhq/postal + +# Path to bundle config +BUNDLE_CONFIG ?= $(HOME)/.bundle/config + +# Detect if a tty is available +TTY := $(shell [ -t 0 ] && echo 1) + +# Tag names +DOCKER_TAG_NAME ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null) +DOCKER_TAG_VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null) + +ifeq ($(DOCKER_TAG_NAME),master) + DOCKER_TAG_NAME = latest +endif + +ifeq ($(DOCKER_TAG_NAME),main) + DOCKER_TAG_NAME = latest +endif + +# Version string to use +VERSION ?= $(DOCKER_TAG_VERSION) +ifeq ($(VERSION),) + VERSION = $(shell git describe --tags 2>/dev/null) +endif + +ifeq ($(VERSION),) + VERSION = 0.0.0-dev +endif + +# ============================================================================== +# Image Building +# ============================================================================== + +DOCKER_BUILD_CMD = DOCKER_BUILDKIT=1 docker \ + build $(if $(TTY),,--progress plain) \ + --build-arg VERSION=$(VERSION) \ + --secret "id=bundle_config,src=$(BUNDLE_CONFIG)" \ + . + +DOCKER_CI_BUILD_CMD = $(DOCKER_BUILD_CMD) --target=ci + +.PHONY: docker-build +docker-build: + $(DOCKER_BUILD_CMD) + +.PHONY: docker-ci-build +docker-ci-build: + $(DOCKER_CI_BUILD_CMD) + +# ============================================================================== +# Image Tagging +# ============================================================================== + +.PHONY: docker-image +docker-image: docker-build + $(eval IMAGE := $(shell $(DOCKER_BUILD_CMD) -q)) +ifeq ($(DOCKER_TAG_NAME),latest) + docker tag "$(IMAGE)" "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" +endif + +# ============================================================================== +# Image Releasing +# ============================================================================== + +.PHONY: docker-release +docker-release: docker-image +ifeq ($(DOCKER_TAG_NAME),latest) + docker push "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" +endif +ifneq ($(DOCKER_TAG_VERSION),) + docker tag "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" \ + "$(DOCKER_IMAGE):$(DOCKER_TAG_VERSION)" && \ + docker push "$(DOCKER_IMAGE):$(DOCKER_TAG_VERSION)" +endif diff --git a/PotamusFile b/PotamusFile deleted file mode 100644 index c6e9573..0000000 --- a/PotamusFile +++ /dev/null @@ -1,2 +0,0 @@ -image_name: adamcooke/postal - diff --git a/docker/wait-for.sh b/docker/wait-for.sh new file mode 100755 index 0000000..ff11c85 --- /dev/null +++ b/docker/wait-for.sh @@ -0,0 +1,60 @@ +#!/bin/sh +[ -n "$DEBUG" ] && set -x + +check_http() { + wget -T 1 -S -q -O - "$1" 2>&1 | head -1 | + head -1 | grep -E 'HTTP.+\s2\d{2}' >/dev/null 2>&1 + return $? +} + +check_tcp() { + host="$(echo "$1" | cut -d: -f1)" + port="$(echo "$1" | cut -d: -f2)" + if [ -z "${host}" ] || [ -z "${port}" ]; then + echo "TCP target ${1} is not in \":\" format" >&2 + exit 2 + fi + + nc -z -w1 "$host" "$port" >/dev/null 2>&1 + return $? +} + +wait_for() { + type="$1" + uri="$2" + timeout="${3:-30}" + + seconds=0 + while [ "$seconds" -lt "$timeout" ] && ! "check_${type}" "$uri"; do + if [ "$seconds" -lt "1" ]; then + printf "Waiting for %s ." "$uri" + else + printf . + fi + seconds=$((seconds + 1)) + sleep 1 + done + + if [ "$seconds" -lt "$timeout" ]; then + if [ "$seconds" -gt "0" ]; then + echo " up!" + fi + else + echo " FAIL" + echo "ERROR: unable to connect to: $uri" >&2 + exit 1 + fi +} + +if [ -n "$WAIT_FOR_TARGETS" ]; then + uris="$(echo "$WAIT_FOR_TARGETS" | sed -e 's/\s+/\n/g' | uniq)" + for uri in $uris; do + if echo "$uri" | grep -E '^https?://.*' >/dev/null 2>&1; then + wait_for "http" "$uri" "$WAIT_FOR_TIMEOUT" + else + wait_for "tcp" "$uri" "$WAIT_FOR_TIMEOUT" + fi + done +fi + +exec "$@"