Skip to content

Commit

Permalink
chore: Faster builds
Browse files Browse the repository at this point in the history
- Enable caching on ghcr.io
- Use docker buildx bake to define all images

Signed-off-by: Damien Duportal <damien.duportal@gmail.com>
  • Loading branch information
dduportal committed May 13, 2021
1 parent 3f3b99c commit 1dc7884
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 58 deletions.
14 changes: 8 additions & 6 deletions .dockerignore
@@ -1,7 +1,9 @@
tests
tests/*
.git*
README-original.adoc
README.adoc
README.md
cache
tests
README*.adoc
README*.md
cache/*
updatecli/*
docker-bake.hcl
*gz
*zip
37 changes: 29 additions & 8 deletions .github/workflows/ci.yml
Expand Up @@ -12,25 +12,46 @@ on:
jobs:
build:
runs-on: ubuntu-20.04
env:
CACHE_REGISTRY: ghcr.io
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install dependencies
- name: Define custom env variables
run: |
echo "CACHE_REGISTRY_PREFIX=${CACHE_REGISTRY}/asciidoctor" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
registry: ${{ env.CACHE_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build
run: |
make build
- name: Deploy Cache
# Only upstream has the cache registry credential as PRs cannot be trusted
if: github.event_name != 'pull_request'
run: |
make docker-cache
- name: Install dependencies for Tests
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends bats
- name: Set up Git user
- name: Test
run: make test
- name: Set up Git user for deploy and custom README
run: |
git config --local user.name "${GITHUB_ACTOR}"
git config --local user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Build
run: |
make build
make test
make README
- name: Generate README
run: make README
- name: Deploy
if: github.event_name != 'pull_request'
run: make deploy-README deploy
env:
DOCKERHUB_SOURCE_TOKEN: ${{ secrets.DOCKERHUB_SOURCE_TOKEN }}
DOCKERHUB_TRIGGER_TOKEN: ${{ secrets.DOCKERHUB_TRIGGER_TOKEN }}
DOCKERHUB_FINAL_IMAGE_NAME: "${GITHUB_ACTOR}"
run: make deploy-README deploy
58 changes: 17 additions & 41 deletions Makefile
@@ -1,54 +1,30 @@

DOCKER_IMAGE_NAME ?= docker-asciidoctor
DOCKERHUB_USERNAME ?= asciidoctor
export DOCKER_BUILDKIT=1
GIT_TAG = $(shell git describe --exact-match --tags HEAD 2>/dev/null)
ifeq ($(strip $(GIT_TAG)),)
GIT_REF = $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
else
GIT_REF = $(GIT_TAG)
endif
DOCKER_IMAGE_TAG ?= $(shell echo $(GIT_REF) | sed 's/\//-/' )
DOCKER_IMAGE_NAME_TO_TEST ?= $(DOCKERHUB_USERNAME)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
export DOCKER_IMAGE_NAME_TO_TEST

TESTS_ENV_FILE ?= $(CURDIR)/tests/env_vars.yml
export TESTS_ENV_FILE

PANDOC_VERSION ?= 2.10.1

all: build test README

build:
docker build \
--target main-minimal \
--tag="$(DOCKER_IMAGE_NAME_TO_TEST)-minimal" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from="$(DOCKER_IMAGE_NAME_TO_TEST)-minimal" \
--file=Dockerfile \
$(CURDIR)/
docker build \
--target build-haskell \
--tag="$(DOCKER_IMAGE_NAME_TO_TEST)-build-haskell" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from="$(DOCKER_IMAGE_NAME_TO_TEST)-build-haskell" \
--file=Dockerfile \
$(CURDIR)/
docker build \
--target main \
--tag="$(DOCKER_IMAGE_NAME_TO_TEST)" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from="$(DOCKER_IMAGE_NAME_TO_TEST)-minimal" \
--cache-from="$(DOCKER_IMAGE_NAME_TO_TEST)-build-haskell" \
--cache-from="$(DOCKER_IMAGE_NAME_TO_TEST)" \
--file=Dockerfile \
$(CURDIR)/

shell: build
docker run -it -v $(CURDIR)/tests/fixtures:/documents/ $(DOCKER_IMAGE_NAME_TO_TEST)

test:
bats $(CURDIR)/tests/*.bats
build: asciidoctor-minimal.build build-haskell.build asciidoctor.build

%.build:
docker buildx bake $(*) --load --set '*.cache-to=""'

docker-cache: asciidoctor-minimal.cache build-haskell.cache asciidoctor.cache

%.docker-cache:
docker buildx bake $(*)

test: asciidoctor.test

%.test:
bats $(CURDIR)/tests/$(*).bats

deploy:
ifdef DOCKERHUB_SOURCE_TOKEN
Expand Down Expand Up @@ -80,14 +56,14 @@ cache/pandoc-$(PANDOC_VERSION)/bin/pandoc: cache/pandoc-$(PANDOC_VERSION)-linux.

# GitHub renders asciidoctor but DockerHub requires markdown.
# This recipe creates README.md and README.adoc from README-original.adoc and env_vars.yml.
README: build cache/pandoc-$(PANDOC_VERSION)/bin/pandoc
README: asciidoctor.build cache/pandoc-$(PANDOC_VERSION)/bin/pandoc
cat tests/env_vars.yml | sed -e 's/^[A-Z]/:&/' | sed '/^#/d' > "$(CURDIR)/cache/env_vars.adoc"
cat "$(CURDIR)/cache/env_vars.adoc" README-original.adoc > README.adoc
docker run --rm -t -v $(CURDIR):/documents --entrypoint bash $(DOCKER_IMAGE_NAME_TO_TEST) \
docker run --rm -t -v $(CURDIR):/documents --entrypoint bash asciidoctor \
-c "asciidoctor -b docbook -a leveloffset=+1 -o - README.adoc | /documents/cache/pandoc-$(PANDOC_VERSION)/bin/pandoc --atx-headers --wrap=preserve -t gfm -f docbook - > README.md"

deploy-README: README
git add README.adoc README.md && git commit -s -m "Updating README files using 'make README command'" \
&& git push origin $(shell git rev-parse --abbrev-ref HEAD) || echo 'No changes to README files'

.PHONY: all build test shell deploy clean README deploy-README
.PHONY: all build test deploy clean README deploy-README docker-cache
2 changes: 1 addition & 1 deletion README.adoc
@@ -1,6 +1,6 @@
:ASCIIDOCTOR_VERSION: 2.0.15
:ASCIIDOCTOR_CONFLUENCE_VERSION: 0.0.2
:ASCIIDOCTOR_PDF_VERSION: 1.5.4
:ASCIIDOCTOR_PDF_VERSION: 1.6.0
:ASCIIDOCTOR_DIAGRAM_VERSION: 2.1.2
:ASCIIDOCTOR_EPUB3_VERSION: 1.5.1
:ASCIIDOCTOR_FB2_VERSION: 0.5.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ This Docker image provides:

- [Asciidoctor Diagram](https://asciidoctor.org/docs/asciidoctor-diagram/) 2.1.2 with ERD and Graphviz integration (supports plantuml and graphiz diagrams)

- [Asciidoctor PDF](https://asciidoctor.org/docs/asciidoctor-pdf/) 1.5.4
- [Asciidoctor PDF](https://asciidoctor.org/docs/asciidoctor-pdf/) 1.6.0

- [Asciidoctor EPUB3](https://asciidoctor.org/docs/asciidoctor-epub3/) 1.5.1

Expand Down
54 changes: 54 additions & 0 deletions docker-bake.hcl
@@ -0,0 +1,54 @@
variable "CACHE_REGISTRY_PREFIX" {
default = "ghcr.io/asciidoctor"
}

group "all" {
targets = [
"asciidoctor-minimal",
"build-haskell",
"asciidoctor"
]
}

target "asciidoctor-minimal" {
dockerfile = "Dockerfile"
context = "."
target = "main-minimal"
tags = [
"asciidoctor-minimal", // Required for test harness
]
cache-from = [
"${CACHE_REGISTRY_PREFIX}/asciidoctor-minimal:cache",
]
cache-to = [
"${CACHE_REGISTRY_PREFIX}/asciidoctor-minimal:cache",
]
}

// This image is only used for intermediate steps
target "build-haskell" {
dockerfile = "Dockerfile"
context = "."
target = "build-haskell"
cache-from = [
"${CACHE_REGISTRY_PREFIX}/build-haskell:cache",
]
cache-to = [
"${CACHE_REGISTRY_PREFIX}/build-haskell:cache",
]
}

target "asciidoctor" {
dockerfile = "Dockerfile"
context = "."
target = "main"
tags = [
"asciidoctor", // Required for test harness
]
cache-from = [
"${CACHE_REGISTRY_PREFIX}/asciidoctor:cache",
]
cache-to = [
"${CACHE_REGISTRY_PREFIX}/asciidoctor:cache",
]
}
3 changes: 2 additions & 1 deletion tests/test_suite.bats → tests/asciidoctor.bats
@@ -1,7 +1,8 @@
#!/usr/bin/env bats

TMP_GENERATION_DIR="${BATS_TEST_DIRNAME}/tmp"
export TMP_GENERATION_DIR
TESTS_ENV_FILE="${BATS_TEST_DIRNAME}/env_vars.yml"
DOCKER_IMAGE_NAME_TO_TEST="asciidoctor"

## Load environment variables from file
if [ -n "${TESTS_ENV_FILE}" ] && [ -f "${TESTS_ENV_FILE}" ]
Expand Down

0 comments on commit 1dc7884

Please sign in to comment.