Skip to content

Commit

Permalink
✨ Improve CI and update
Browse files Browse the repository at this point in the history
Signed-off-by: mathieu.brunot <[email protected]>
  • Loading branch information
madmath03 committed Feb 5, 2021
1 parent 88f32f4 commit 1ce1871
Show file tree
Hide file tree
Showing 47 changed files with 696 additions and 180 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/hooks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

name: Docker Image CI

on:
push:
branches:
- master
- develop

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
version: ['1.0']
variant: ['debian', 'alpine']

steps:
- name: Check Out Repo
uses: actions/checkout@v2

- name: Execute CI hooks
id: docker_build
run: |
cd ./images/${{ matrix.version }}/${{ matrix.variant }}/
./hooks/run
- name: Display docker images
run: docker images
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ After replacing all variables:

[![License: AGPL v3][uri_license_image]][uri_license]
[![Docs](https://img.shields.io/badge/Docs-Github%20Pages-blue)](https://monogramm.github.io/__app_slug__/)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Monogramm/docker-__app_slug__/Docker%20Image%20CI)](https://github.com/Monogramm/docker-__app_slug__/actions)
[![Build Status](https://travis-ci.org/Monogramm/docker-__app_slug__.svg)](https://travis-ci.org/Monogramm/docker-__app_slug__)
[![Docker Automated buid](https://img.shields.io/docker/cloud/build/monogramm/docker-__app_slug__.svg)](https://hub.docker.com/r/monogramm/docker-__app_slug__/)
[![Docker Pulls](https://img.shields.io/docker/pulls/monogramm/docker-__app_slug__.svg)](https://hub.docker.com/r/monogramm/docker-__app_slug__/)
Expand All @@ -41,8 +42,12 @@ __app_description__

[Dockerhub monogramm/docker-__app_slug__](https://hub.docker.com/r/monogramm/docker-__app_slug__/)

- `alpine` `latest`
- `debian`
<!-- >Docker Tags -->

- 1.0.0-debian 1.0-debian debian (`images/1.0/debian/Dockerfile`)
- 1.0.0-alpine 1.0-alpine alpine 1.0.0 1.0 latest (`images/1.0/alpine/Dockerfile`)

<!-- <Docker Tags -->

## How to run this image

Expand All @@ -61,7 +66,6 @@ See **__app_name__** base image documentation for details.

If you got any questions or problems using the image, please visit our [Github Repository](https://github.com/Monogramm/docker-__app_slug__) and write an issue.


[uri_license]: http://www.gnu.org/licenses/agpl.html

[uri_license_image]: https://img.shields.io/badge/License-AGPL%20v3-blue.svg
Empty file removed images/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions images/1.0/alpine/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Ignore CI docker-compose
docker-compose.yml
.dockertags
1 change: 1 addition & 0 deletions images/1.0/alpine/.dockertags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0-alpine 1.0-alpine alpine 1.0.0 1.0 latest
7 changes: 6 additions & 1 deletion images/1.0/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine
FROM alpine:3.12
# If based on official images
# FROM __app_owner_slug__/__app_slug__:alpine

Expand All @@ -16,6 +16,11 @@ ARG VCS_REF
ARG BUILD_DATE
ARG VERSION=1.0.0

# Keep track of image version
RUN set -ex; \
mkdir -p '/app/src'; \
echo "${VERSION} ${VCS_REF} ${BUILD_DATE}" > '/app/src/.docker-version'

# Container labels (http://label-schema.org/)
# Container annotations (https://github.com/opencontainers/image-spec)
LABEL maintainer="Monogramm maintainers <opensource at monogramm dot io>" \
Expand Down
82 changes: 53 additions & 29 deletions images/1.0/alpine/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
version: "2.3"

volumes:
__app_slug___data:
__app_slug___db:
__app_slug___web:

networks:
__app_uppercase_slug___external_network:
__app_uppercase_slug___internal_network:
__app_slug___external_network:
__app_slug___internal_network:
internal: true

services:
Expand All @@ -19,19 +24,23 @@ services:
__app_slug__-nginx:
condition: service_started
networks:
- __app_uppercase_slug___internal_network
- __app_slug___internal_network
volumes_from:
- __app_slug__
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
# If you want, you can sync time & timezone with host
#volumes:
# - /etc/localtime:/etc/localtime:ro
# - /etc/timezone:/etc/timezone:ro
environment:
- DOCKER_TEST_CONTAINER=__app_slug__
- DOCKER_WEB_CONTAINER=__app_slug__-nginx

__app_slug__:
# For development or CI, tag build from local Dockerfile
build:
context: ./
dockerfile: Dockerfile
args:
args:
- TAG=${TAG}
- VCS_REF=${VCS_REF}
- BUILD_DATE=${BUILD_DATE}
Expand All @@ -45,18 +54,19 @@ services:
links:
- __app_slug__-db
expose:
- '9000'
#ports:
# - "9000:9000"
- "9000"
ports:
- "9090:9000"
networks:
- __app_uppercase_slug___internal_network
- __app_slug___internal_network
volumes:
- $__app_uppercase_slug___HOME/html:/var/www/html
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- __app_slug___data:/var/www/html
# If you want, you can sync time & timezone with host
#- /etc/localtime:/etc/localtime:ro
#- /etc/timezone:/etc/timezone:ro
environment:
- __app_uppercase_slug___DB_TYPE=pgsql
- __app_uppercase_slug___DB_HOST=$__app_slug__-db
- __app_uppercase_slug___DB_HOST=__app_slug__-db
- __app_uppercase_slug___DB_NAME=$__app_uppercase_slug___DB_NAME
- __app_uppercase_slug___DB_USER=$__app_uppercase_slug___DB_USER
- __app_uppercase_slug___DB_PASSWORD=$__app_uppercase_slug___DB_PASSWD
Expand All @@ -65,16 +75,29 @@ services:
image: postgres:latest
container_name: __app_slug__-db
restart: always
healthcheck:
test:
[
"CMD",
"pg_isready",
"-U",
"${__app_uppercase_slug___DB_USER}",
"-d",
"${__app_uppercase_slug___DB_NAME}",
]
interval: 60s
retries: 5
expose:
- "5432"
#ports:
# - "5432:5432"
healthcheck:
test: ["CMD", "pg_isready", "-U", "${__app_uppercase_slug___DB_USER}", "-d", "${__app_uppercase_slug___DB_NAME}"]
networks:
- __app_uppercase_slug___internal_network
- __app_slug___internal_network
volumes:
- $__app_uppercase_slug___HOME/db:/var/lib/postgresql/data
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- __app_slug___db:/var/lib/postgresql/data
# If you want, you can sync time & timezone with host
#- /etc/localtime:/etc/localtime:ro
#- /etc/timezone:/etc/timezone:ro
environment:
- POSTGRES_DB=$__app_uppercase_slug___DB_NAME
- POSTGRES_USER=$__app_uppercase_slug___DB_USER
Expand All @@ -85,26 +108,27 @@ services:
container_name: __app_slug__-nginx
restart: always
ports:
- "80:80"
- "8080:80"
# If you need SSL connection
# - "443:443"
# - "8443:443"
networks:
- __app_uppercase_slug___internal_network
- __app_uppercase_slug___external_network
- __app_slug___internal_network
- __app_slug___external_network
depends_on:
__app_slug__:
condition: service_started
links:
- __app_slug__
volumes:
- $__app_uppercase_slug___HOME/html:/var/www/html
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- __app_slug___data:/var/www/html
# If you want, you can sync time & timezone with host
#- /etc/localtime:/etc/localtime:ro
#- /etc/timezone:/etc/timezone:ro
# TODO Provide a custom nginx conf
#- ./nginx.conf:/etc/nginx/nginx.conf:ro
# If you need SSL connection, you can provide your own certificates
# - ./certs:/etc/letsencrypt
# - ./certs-data:/data/letsencrypt
environment:
- NGINX_HOST=localhost # set your local domain or your live domain
# - NGINX_CGI=__app_slug__:9000 # same as __app_slug__ container name
#- NGINX_CGI=__app_slug__:9000 # same as __app_slug__ container name
43 changes: 31 additions & 12 deletions images/1.0/alpine/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/sh
##
## Docker image for __app_name__.
## Copyright (C) 2020 Monogramm
## Copyright (C) 2021 Monogramm
##
set -e

# -------------------------------------------------------------------
# Functions

log() {
echo "[$0] [$(date +%Y-%m-%dT%H:%M:%S%:z)] $@"
echo "[$0] [$(date +%Y-%m-%dT%H:%M:%S%:z)] $*"
}

# wait for file/directory to exists
Expand All @@ -24,15 +24,15 @@ wait_for_file() {
WAIT_STEP=${2:-10}
WAIT_TIMEOUT=${3:--1}

while [ ! -d "${WAIT_FOR_FILE}" ] ; do
while [ ! -d "${WAIT_FOR_FILE}" ]; do
if [ "${WAIT_TIMEOUT}" -gt 0 ] && [ "${WAIT_TIME}" -gt "${WAIT_TIMEOUT}" ]; then
log "File '${WAIT_FOR_FILE}' was not available on time!"
exit 1
fi

log "Waiting file '${WAIT_FOR_FILE}'..."
sleep "${WAIT_STEP}"
WAIT_TIME=$(( WAIT_TIME + WAIT_STEP ))
WAIT_TIME=$((WAIT_TIME + WAIT_STEP))
done
log "File '${WAIT_FOR_FILE}' exists."
}
Expand Down Expand Up @@ -67,21 +67,21 @@ wait_for_service() {
WAIT_STEP=${3:-10}
WAIT_TIMEOUT=${4:--1}

while ! nc -z "${WAIT_FOR_ADDR}" "${WAIT_FOR_PORT}" ; do
while ! nc -z "${WAIT_FOR_ADDR}" "${WAIT_FOR_PORT}"; do
if [ "${WAIT_TIMEOUT}" -gt 0 ] && [ "${WAIT_TIME}" -gt "${WAIT_TIMEOUT}" ]; then
log "Service '${WAIT_FOR_ADDR}:${WAIT_FOR_PORT}' was not available on time!"
exit 1
fi

log "Waiting service '${WAIT_FOR_ADDR}:${WAIT_FOR_PORT}'..."
sleep "${WAIT_STEP}"
WAIT_TIME=$(( WAIT_TIME + WAIT_STEP ))
WAIT_TIME=$((WAIT_TIME + WAIT_STEP))
done
log "Service '${WAIT_FOR_ADDR}:${WAIT_FOR_PORT}' available."
}

wait_for_services() {
if [ -z "${WAIT_FOR_SERVICES}" ]; then
if [ -z "${WAIT_FOR_SERVICES:-$1}" ]; then
log "Missing env var 'WAIT_FOR_SERVICES' defining services to wait for!"
exit 1
fi
Expand All @@ -95,6 +95,25 @@ wait_for_services() {

}

# init / update application
init() {
# Check version
if [ ! -f "./.docker-version" ]; then
log "[TODO] __app_name__ init to $(cat /app/src/.docker-version)..."
elif ! cmp --silent "./.docker-version" "/app/src/.docker-version"; then
log "[TODO] __app_name__ update from $(cat ./.docker-version) to $(cat /app/src/.docker-version)..."
fi

cp -p "/app/src/.docker-version" "./.docker-version"
}

# start application
start() {
init

echo "[TODO] Start main service"
}

# display help
print_help() {
echo "Monogramm Docker entrypoint for __app_name__.
Expand All @@ -114,9 +133,9 @@ Options:

# Execute task based on command
case "${1}" in
# Management tasks
("--help") print_help ;;
# Service tasks
("start") echo "TODO Start main service" ;;
(*) exec "$@" ;;
# Management tasks
-h|--help) print_help ;;
# Service tasks
start) start ;;
*) exec "$@" ;;
esac
3 changes: 3 additions & 0 deletions images/1.0/alpine/hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DockerHub automated tests hooks

See documentation for details: <https://docs.docker.com/docker-hub/builds/advanced/>
6 changes: 3 additions & 3 deletions images/1.0/alpine/hooks/build
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# https://docs.docker.com/docker-hub/builds/advanced/

docker build \
--build-arg TAG="${DOCKER_TAG}" \
--build-arg VCS_REF=`git rev-parse --short HEAD` \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
--build-arg VERSION="${DOCKER_TAG}" \
--build-arg "VCS_REF=$(git rev-parse --short HEAD)" \
--build-arg "BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
-f "$DOCKERFILE_PATH" \
-t "$IMAGE_NAME" .
13 changes: 13 additions & 0 deletions images/1.0/alpine/hooks/post_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# See documentation for details:
# https://docs.docker.com/docker-hub/builds/advanced/

if [ -f ./.dockertags ]; then
for tag in $(cat ./.dockertags); do
if [ ! "${tag}" = "${DOCKER_TAG}" ]; then
docker tag "$IMAGE_NAME" "$DOCKER_REPO:$tag"
docker push "$DOCKER_REPO:$tag"
fi
done
fi
7 changes: 7 additions & 0 deletions images/1.0/alpine/hooks/post_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# See documentation for details:
# https://docs.docker.com/docker-hub/builds/advanced/

#echo 'Removing persisted data...'
#rm -rf "/srv/__app_slug__/data"
8 changes: 8 additions & 0 deletions images/1.0/alpine/hooks/pre_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# See documentation for details:
# https://docs.docker.com/docker-hub/builds/advanced/

#echo 'Preparing persisted data...'
#mkdir -p "/srv/__app_slug__/data"
#chown 777 "/srv/__app_slug__/data"
Loading

0 comments on commit 1ce1871

Please sign in to comment.