Minifier le contenue d'Hugo

Apres Comment j’ai virer mon wordpress pour GoHugo, voici comment y aporter quelque optimisations, au niveau docker et du contenue generer

Build

L’image Docker

L’image est build en deux parties, la premiere partie sert a compiler minify qui sert par la suite copier dans l’image definitive.

FROM golang:1.10-alpine as builder
ARG MINIFY_REPO="github.com/tdewolff/minify"
ARG MINIFY_PACKAGE="cmd/minify"
ARG MINIFY_VERSION="2.3.5"
RUN apk add --no-cache --update \
        git \
        gcc \
        libc-dev \
  && cd /go ; go get -v ${MINIFY_REPO}/${MINIFY_PACKAGE} \
  && cd /go/src/${MINIFY_REPO}/${MINIFY_PACKAGE} ; git checkout tags/v${MINIFY_VERSION} -b v${MINIFY_VERSION} \
  && cd /go ; go get -v -d ${MINIFY_REPO}/${MINIFY_PACKAGE} \
  && cd /go ; CGO_ENABLED=0 go build -ldflags "-X main.Version=${MINIFY_VERSION}" -a -o /go/bin/minify -installsuffix cgo ${MINIFY_REPO}/${MINIFY_PACKAGE} \
  && /go/bin/minify --version

FROM alpine:3.7
COPY --from=builder /go/bin/minify /usr/local/bin/
ARG HUGO_VERSION="0.41"
RUN set -x && \
  apk add --no-cache --update \
        curl \
        ca-certificates \
  && curl -L https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-64bit.tar.gz | \
        tar -xz hugo -C /usr/local/bin/

Variable

La CI

Les tags sont utilise pour build des version specifique
La branch master build l’image latest

image: docker:latest

services:
  - docker:dind

before_script:
  - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

build-master:
  stage: build
  script:
    - docker build --pull 
      --build-arg HUGO_VERSION="${HUGO_VERSION}"
      --build-arg MINIFY_VERSION="${MINIFY_VERSION}"
      -t "$CI_REGISTRY_IMAGE" .
    - docker push "${CI_REGISTRY_IMAGE}"
  only:
    - master

build:
  stage: build
  script:
    - docker build --pull
      --build-arg HUGO_VERSION="${CI_COMMIT_TAG}"
      --build-arg MINIFY_VERSION="${MINIFY_VERSION}"
      -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
  only:
    - master
  only:
    - tags

Deploiement Hugo

image: ${CI_REGISTRY}/github/hugo-build:latest

variables:
  GIT_SUBMODULE_STRATEGY: recursive

pages:
  script:
  - /usr/local/bin/hugo
  - /usr/local/bin/minify -r -o public/ public/
  artifacts:
    paths:
    - public
  only:
  - master

test:
  script:
  - hugo
  except:
  - master


comments powered by Disqus