Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Update project's toolchain
Browse files Browse the repository at this point in the history
- refactor Makefile with docopt format and better looping
- upgrade to the latest 1.x Bats version
- improve CI pipeline
- switch to PHP templating for Docker Hub hook
  • Loading branch information
tyranron committed Dec 14, 2018
1 parent 75e136f commit 29e7a61
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 83 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
*
!/Dockerfile
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ insert_final_newline = true
max_line_length = 80

[*.md]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false
max_line_length = off

[Dockerfile]
indent_style = space
Expand All @@ -18,10 +21,10 @@ indent_size = 4
indent_style = tab
indent_size = 4

[{*.sh,*.bats,post_push}]
[{*.sh,*.bats,post_push,post_push.tmpl.php}]
indent_style = space
indent_size = 2

[*.{yml,yaml}]
[*.{json,yml,yaml}]
indent_style = space
indent_size = 2
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/.idea
/.idea/
/*.iml
.DS_Store

/test/bats
/node_modules/
/yarn.lock
/yarn-error.log
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
language: bash

sudo: required
sudo: false

services:
- docker

before_script:
- make image no-cache=yes VERSION=test
- make deps.bats

script:
- make image no-cache=yes VERSION=testing
- make test VERSION=testing
- make test VERSION=test

notifications:
email:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# https://github.com/nodejs/docker-node/blob/a3489450fcd506538ab84174ebedb3cc5c908bc7/7.2/alpine/Dockerfile
# https://hub.docker.com/_/node/
FROM node:alpine

MAINTAINER Instrumentisto Team <developer@instrumentisto.com>
Expand Down
104 changes: 51 additions & 53 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# This Makefile automates possible operations of this project.
#
# Images and description on Docker Hub will be automatically rebuilt on
# pushes to `master` branch of this repo and on updates of
# parent `node` image.
# pushes to `master` branch of this repo and on updates of parent image.
#
# Note! Docker Hub `post_push` hook must be always up-to-date with default
# values of current Makefile. To update it just use:
Expand All @@ -13,16 +12,11 @@


IMAGE_NAME := instrumentisto/node-builder
VERSION ?= 0.0.2
TAGS ?= 0.0.2,latest

no-cache ?= no

VERSION ?= 0.1.0
TAGS ?= 0.1.0,latest


comma := ,
empty :=
space := $(empty) $(empty)
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
$(findstring $(2),$(1))),1)

Expand All @@ -31,44 +25,53 @@ eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
# Build Docker image.
#
# Usage:
# make image [no-cache=(yes|no)] [VERSION=]

no-cache-arg = $(if $(call eq, $(no-cache), yes), --no-cache, $(empty))
# make image [VERSION=<image-version>]
# [no-cache=(no|yes)]

image:
docker build $(no-cache-arg) -t $(IMAGE_NAME):$(VERSION) .
docker build --network=host --force-rm \
$(if $(call eq,$(no-cache),yes),--no-cache --pull,) \
-t $(IMAGE_NAME):$(VERSION) .



# Tag Docker image with given tags.
#
# Usage:
# make tags [VERSION=] [TAGS=t1,t2,...]

parsed-tags = $(subst $(comma), $(space), $(TAGS))
# make tags [VERSION=<image-version>]
# [TAGS=<docker-tag-1>[,<docker-tag-2>...]]

tags:
$(foreach tag, $(parsed-tags), \
docker tag $(IMAGE_NAME):$(VERSION) $(IMAGE_NAME):$(tag); \
)
$(foreach tag,$(subst $(comma), ,$(TAGS)),\
$(call tags.do,$(VERSION),$(tag)))
define tags.do
$(eval from := $(strip $(1)))
$(eval to := $(strip $(2)))
docker tag $(IMAGE_NAME):$(from) $(IMAGE_NAME):$(to)
endef



# Manually push Docker images to Docker Hub.
#
# Usage:
# make push [TAGS=t1,t2,...]
# make push [TAGS=<docker-tag-1>[,<docker-tag-2>...]]

push:
$(foreach tag, $(parsed-tags), \
docker push $(IMAGE_NAME):$(tag); \
)
$(foreach tag,$(subst $(comma), ,$(TAGS)),\
$(call push.do,$(tag)))
define push.do
$(eval tag := $(strip $(1)))
docker push $(IMAGE_NAME):$(tag)
endef



# Make manual release of Docker images to Docker Hub.
#
# Usage:
# make manual-release [no-cache=(yes|no)] [VERSION=] [TAGS=t1,t2,...]
# make release [VERSION=<image-version>] [no-cache=(no|yes)]
# [TAGS=<docker-tag-1>[,<docker-tag-2>...]]

release: | image tags push

Expand All @@ -83,48 +86,43 @@ release: | image tags push
# http://windsock.io/automated-docker-image-builds-with-multiple-tags
#
# Usage:
# make post-push-hook [TAGS=t1,t2,...]
# make post-push-hook [TAGS=<docker-tag-1>[,<docker-tag-2>...]]

post-push-hook:
mkdir -p $(PWD)/hooks
docker run --rm -i \
-v $(PWD)/post_push.j2:/data/post_push.j2:ro \
-e TEMPLATE=post_push.j2 \
pinterb/jinja2 \
image_tags='$(TAGS)' \
> $(PWD)/hooks/post_push
@mkdir -p hooks/
docker run --rm -v "$(PWD)/post_push.tmpl.php":/post_push.php:ro \
php:alpine php -f /post_push.php -- \
--image_tags='$(TAGS)' \
> hooks/post_push



# Run tests for Docker image.
# Run Bats tests for Docker image.
#
# Documentation of Bats:
# https://github.com/bats-core/bats-core
#
# Usage:
# make test [VERSION=]
# make test [VERSION=<image-version>]

test: deps-test
IMAGE=$(IMAGE_NAME):$(VERSION) ./test/bats/bats test/suite.bats
test:
ifeq ($(wildcard node_modules/.bin/bats),)
@make deps.bats
endif
IMAGE=$(IMAGE_NAME):$(VERSION) node_modules/.bin/bats test/suite.bats



# Resolve project dependencies for running tests.
# Resolve project dependencies for running tests with Yarn.
#
# Usage:
# make deps-test [BATS_VER=]

BATS_VER ?= 0.4.0

deps-test:
ifeq ($(wildcard $(PWD)/test/bats),)
mkdir -p $(PWD)/test/bats/vendor
wget https://github.com/sstephenson/bats/archive/v$(BATS_VER).tar.gz \
-O $(PWD)/test/bats/vendor/bats.tar.gz
tar -xzf $(PWD)/test/bats/vendor/bats.tar.gz \
-C $(PWD)/test/bats/vendor
rm -f $(PWD)/test/bats/vendor/bats.tar.gz
ln -s $(PWD)/test/bats/vendor/bats-$(BATS_VER)/libexec/* \
$(PWD)/test/bats/
endif
# make deps.bats

deps.bats:
docker run --rm -v "$(PWD)":/app -w /app \
node:alpine \
yarn install --non-interactive --no-progress



.PHONY: image tags push release post-push-hook test deps-test
.PHONY: image tags push release post-push-hook test deps.bats
4 changes: 2 additions & 2 deletions hooks/post_push
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# AUTOMATICALLY GENERATED
# DO NOT EDIT THIS FILE DIRECTLY, USE /post_push.j2
# DO NOT EDIT THIS FILE DIRECTLY, USE /post_push.tmpl.php

set -e

Expand All @@ -9,7 +9,7 @@ tagStart=$(expr index "$IMAGE_NAME" :)
repoName=${IMAGE_NAME:0:tagStart-1}

# Tag and push image for each additional tag
for tag in {0.0.2,latest}; do
for tag in {0.1.0,latest}; do
docker tag $IMAGE_NAME ${repoName}:${tag}
docker push ${repoName}:${tag}
done
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"bats": "^1.0.2"
}
}
6 changes: 3 additions & 3 deletions post_push.j2 → post_push.tmpl.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<? $var = getopt('', ['image_tags:']); ?>
#!/bin/bash
# AUTOMATICALLY GENERATED
# DO NOT EDIT THIS FILE DIRECTLY, USE /post_push.j2
# DO NOT EDIT THIS FILE DIRECTLY, USE /post_push.tmpl.php

set -e

Expand All @@ -9,8 +10,7 @@
repoName=${IMAGE_NAME:0:tagStart-1}

# Tag and push image for each additional tag
for tag in { {{- image_tags -}} }; do
for tag in {<?= $var['image_tags']; ?>}; do
docker tag $IMAGE_NAME ${repoName}:${tag}
docker push ${repoName}:${tag}
done
{{ '' }}
32 changes: 17 additions & 15 deletions test/suite.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#!/usr/bin/env bats


@test "post_push hook is up-to-date" {
run sh -c "cat Makefile | grep 'TAGS ?= ' | cut -d ' ' -f 3"
[ "$status" -eq 0 ]
[ ! "$output" = '' ]
expected="$output"

run sh -c "cat hooks/post_push | grep 'for tag in' \
| cut -d '{' -f 2 \
| cut -d '}' -f 1"
[ "$status" -eq 0 ]
[ ! "$output" = '' ]
actual="$output"

[ "$actual" = "$expected" ]
}


@test "contains node" {
run docker run --rm $IMAGE which node
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -43,18 +60,3 @@
run docker run --rm $IMAGE java -help
[ "$status" -eq 0 ]
}


@test "post_push hook is up-to-date" {
run sh -c "cat Makefile | grep 'TAGS ?= ' | cut -d ' ' -f 3"
[ "$status" -eq 0 ]
[ ! "$output" = '' ]
expected="$output"

run sh -c "cat hooks/post_push | grep 'for tag in' | cut -d '{' -f 2 | cut -d '}' -f 1"
[ "$status" -eq 0 ]
[ ! "$output" = '' ]
actual="$output"

[ "$actual" = "$expected" ]
}

0 comments on commit 29e7a61

Please sign in to comment.