Skip to content

Commit

Permalink
chore: automate builds
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwalker committed Jun 1, 2021
1 parent de31132 commit cddf237
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 10 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Build/Publish Image

on:
push:
branches:
- master
paths:
- 'Makefile'
- 'Dockerfile'
- 'VERSION'
pull_request:
paths:
- 'Makefile'
- 'Dockerfile'
- 'VERSION'

env:
BUILDX_CACHE_DIR: /tmp/buildx

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Get Version Info
id: versions
run: |
elixir_version="$(cat VERSION | grep elixir | cut -d ' ' -f2)"
echo "::set-output name=elixir_version::${elixir_version}"
- name: Setup QEMU
uses: docker/setup-qemu-action@v1

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker Layers
uses: actions/cache@v2
id: cache
with:
path: /tmp/buildx
key: buildx-elixir${{ steps.versions.outputs.elixir_version }}
restore-keys: |
buildx-elixir${{ steps.versions.outputs.elixir_version }}
buildx-elixir
- name: Build Image
env:
VERSION: ${{ steps.versions.outputs.elixir_version }}
run: make build

- name: Login
if: ${{ github.event_name == 'push' }}
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Publish Image
if: ${{ github.event_name == 'push' }}
env:
VERSION: ${{ steps.versions.outputs.elixir_version }}
run: make release
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM bitwalker/alpine-elixir:1.11.4
ARG ELIXIR_VERSION
FROM bitwalker/alpine-elixir:${ELIXIR_VERSION}

MAINTAINER Paul Schoenfelder <paulschoenfelder@gmail.com>

# Important! Update this no-op ENV variable when this Dockerfile
# is updated with the current date. It will force refresh of all
# of the base images and things like `apt-get update` won't be using
# old cached versions when the Dockerfile is built.
ENV REFRESHED_AT=2021-03-15
ENV REFRESHED_AT=2021-06-01

# Install NPM
RUN \
Expand All @@ -33,4 +34,4 @@ ONBUILD RUN mix do local.hex --force, local.rebar --force

WORKDIR /opt/app

CMD ["/bin/sh"]
CMD ["bash"]
37 changes: 31 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.PHONY: help

VERSION ?= `cat VERSION`
VERSION ?= `cat VERSION | grep elixir | cut -d' ' -f2`
MAJ_VERSION := $(shell echo $(VERSION) | sed 's/\([0-9][0-9]*\)\.\([0-9][0-9]*\)\(\.[0-9][0-9]*\)*/\1/')
MIN_VERSION := $(shell echo $(VERSION) | sed 's/\([0-9][0-9]*\)\.\([0-9][0-9]*\)\(\.[0-9][0-9]*\)*/\1.\2/')
IMAGE_NAME ?= bitwalker/alpine-elixir-phoenix
XDG_CACHE_HOME ?= /tmp
BUILDX_CACHE_DIR ?= $(XDG_CACHE_HOME)/buildx

help:
@echo "$(IMAGE_NAME):$(VERSION)"
Expand All @@ -18,14 +20,37 @@ shell: ## Run an Elixir shell in the image
sh: ## Boot to a shell prompt
docker run --rm -it $(IMAGE_NAME):$(VERSION) /bin/bash

build: ## Build the Docker image
docker build --force-rm -t $(IMAGE_NAME):$(VERSION) -t $(IMAGE_NAME):$(MIN_VERSION) -t $(IMAGE_NAME):$(MAJ_VERSION) -t $(IMAGE_NAME):latest - < ./Dockerfile
setup-buildx: ## Setup a Buildx builder
@mkdir -p "$(BUILDX_CACHE_DIR)"
@if ! docker buildx ls | grep buildx-builder >/dev/null; then \
docker buildx create --append --name buildx-builder --driver docker-container --use && \
docker buildx inspect --bootstrap --builder buildx-builder; \
fi

build: setup-buildx ## Build the Docker image
docker buildx build --output "type=image,push=false" \
--build-arg ELIXIR_VERSION=$(VERSION) \
--platform linux/amd64,linux/arm64 \
--cache-from "type=local,src=$(BUILDX_CACHE_DIR)" \
--cache-to "type=local,dest=$(BUILDX_CACHE_DIR)" \
-t $(IMAGE_NAME):$(VERSION) \
-t $(IMAGE_NAME):$(MIN_VERSION) \
-t $(IMAGE_NAME):$(MAJ_VERSION) \
-t $(IMAGE_NAME):latest .


clean: ## Clean up generated images
@docker rmi --force $(IMAGE_NAME):$(VERSION) $(IMAGE_NAME):$(MIN_VERSION) $(IMAGE_NAME):$(MAJ_VERSION) $(IMAGE_NAME):latest

rebuild: clean build ## Rebuild the Docker image

release: build ## Rebuild and release the Docker image to Docker Hub
docker push $(IMAGE_NAME):$(VERSION)
docker push $(IMAGE_NAME):latest
release: setup-buildx ## Build and release the Docker image to Docker Hub
docker buildx build --push \
--build-arg ELIXIR_VERSION=$(VERSION) \
--platform linux/amd64,linux/arm64 \
--cache-from "type=local,src=$(BUILDX_CACHE_DIR)" \
--cache-to "type=local,dest=$(BUILDX_CACHE_DIR)" \
-t $(IMAGE_NAME):$(VERSION) \
-t $(IMAGE_NAME):$(MIN_VERSION) \
-t $(IMAGE_NAME):$(MAJ_VERSION) \
-t $(IMAGE_NAME):latest .
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.4
elixir: 1.11.4

0 comments on commit cddf237

Please sign in to comment.