From 84b29f0467c051012da2466ad1f67c19690d0881 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 13 Sep 2020 22:50:17 +0100 Subject: [PATCH] feat: build the magefile into a binary This commit builds the magefile into a binary during the docker build stage. The Dockerfile is updated to include a multi-stage build where the first stage compiles the binary and the second stage builds the final cv-builder image. The final image is now based on debian:buster instead of golang:-buster which significantly reduces the image's size. --- .dockerignore | 4 ++++ .gitlab/ci/docker-gitlab-ci.yml | 8 +++----- docker/Dockerfile | 26 ++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.dockerignore b/.dockerignore index 72e8ffc..214118b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,5 @@ * +!magefile.go +!helpers/* +!go.mod +!go.sum diff --git a/.gitlab/ci/docker-gitlab-ci.yml b/.gitlab/ci/docker-gitlab-ci.yml index 755d5f9..2ebeefb 100644 --- a/.gitlab/ci/docker-gitlab-ci.yml +++ b/.gitlab/ci/docker-gitlab-ci.yml @@ -21,9 +21,9 @@ .docker-test-rules: rules: - - if: '$CI_COMMIT_BRANCH' - changes: + - changes: - "docker/Dockerfile" + - "*.go" when: always - if: '$CI_COMMIT_REF_NAME =~ /^[0-9]{4}(.[0-9]{2}){2}$/' when: never @@ -39,9 +39,7 @@ .docker-publish-rules: rules: - - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PROJECT_PATH == "dananglin/cv"' - changes: - - "docker/Dockerfile" + - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PROJECT_PATH == "dananglin/cv" && $CI_PIPELINE_SOURCE == "web" && $BUILD_DOCKER_IMAGE == "true"' when: always #------------------------------------------------# diff --git a/docker/Dockerfile b/docker/Dockerfile index 4e1aec0..907bd7e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,11 +1,33 @@ -FROM golang:1.14.6-buster +FROM golang:1.15.2-buster AS builder -# Upgrade packages and install dependencies +RUN git clone https://github.com/magefile/mage "${GOPATH}/src/mage" + +WORKDIR ${GOPATH}/src/mage + +RUN go run bootstrap.go + +COPY go.mod ${GOPATH}/cv-builder/ +COPY go.sum ${GOPATH}/cv-builder/ +COPY magefile.go ${GOPATH}/cv-builder/ +COPY helpers ${GOPATH}/cv-builder/helpers + +WORKDIR ${GOPATH}/cv-builder + +RUN mage -compile /usr/local/bin/cv-make + +FROM debian:buster + +COPY --from=builder /usr/local/bin/cv-make /usr/local/bin + +# Install dependencies RUN \ apt-get update && \ apt-get install -y --no-install-recommends \ fonts-crosextra-carlito=20130920-1 \ rsync=3.1.3-6 \ + curl=7.64.0-4+deb10u1 \ + aspell=0.60.7~20110707-6 \ + aspell-en=2018.04.16-0-1 \ && \ apt-get clean autoclean && \ apt-get autoremove -y && \