Skip to content

Commit 4c3347a

Browse files
committed
Upgrade to latest Go version + Some Docker image improvements (note)
* Rename /site to /project * Add ldflags * Add go and node to the default image * Add Dart Sass to the default image * Build the extended version by default * Add "npm i" install support with custom entry script override * Adjust cache logic to speed up CGO rebuilds Closes #12920 See #12885
1 parent a3a0001 commit 4c3347a

File tree

4 files changed

+110
-31
lines changed

4 files changed

+110
-31
lines changed

‎.circleci/config.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ parameters:
44
defaults: &defaults
55
resource_class: large
66
docker:
7-
- image: bepsays/ci-hugoreleaser:1.22300.20000
7+
- image: bepsays/ci-hugoreleaser:1.22300.20200
88
environment: &buildenv
99
GOMODCACHE: /root/project/gomodcache
1010
version: 2
@@ -60,7 +60,7 @@ jobs:
6060
environment:
6161
<<: [*buildenv]
6262
docker:
63-
- image: bepsays/ci-hugoreleaser-linux-arm64:1.22300.20000
63+
- image: bepsays/ci-hugoreleaser-linux-arm64:1.22300.20200
6464
steps:
6565
- *restore-cache
6666
- &attach-workspace

‎Dockerfile‎

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,94 @@
22
# Twitter: https://twitter.com/gohugoio
33
# Website: https://gohugo.io/
44

5+
ARG GO_VERSION="1.23.2"
6+
ARG ALPINE_VERSION=3.20
7+
58
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.5.0 AS xx
9+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS gobuild
10+
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS gorun
11+
612

7-
FROM --platform=$BUILDPLATFORM golang:1.22.6-alpine AS build
13+
FROM gobuild AS build
14+
15+
RUN apk add clang lld
816

917
# Set up cross-compilation helpers
1018
COPY --from=xx / /
11-
RUN apk add clang lld
1219

13-
# Optionally set HUGO_BUILD_TAGS to "extended" or "nodeploy" when building like so:
14-
# docker build --build-arg HUGO_BUILD_TAGS=extended .
15-
ARG HUGO_BUILD_TAGS="none"
20+
ARG TARGETPLATFORM
21+
RUN xx-apk add musl-dev gcc g++
1622

17-
ARG CGO=1
18-
ENV CGO_ENABLED=${CGO}
19-
ENV GOOS=linux
20-
ENV GO111MODULE=on
23+
# Optionally set HUGO_BUILD_TAGS to "none" or "nodeploy" when building like so:
24+
# docker build --build-arg HUGO_BUILD_TAGS=nodeploy .
25+
#
26+
# We build the extended version by default.
27+
ARG HUGO_BUILD_TAGS="extended"
28+
ENV CGO_ENABLED=1
29+
ENV GOPROXY=https://proxy.golang.org
30+
ENV GOCACHE=/root/.cache/go-build
31+
ENV GOMODCACHE=/go/pkg/mod
32+
ARG TARGETPLATFORM
2133

2234
WORKDIR /go/src/github.com/gohugoio/hugo
2335

24-
RUN --mount=src=go.mod,target=go.mod \
25-
--mount=src=go.sum,target=go.sum \
26-
--mount=type=cache,target=/go/pkg/mod \
27-
go mod download
28-
29-
ARG TARGETPLATFORM
30-
# gcc/g++ are required to build SASS libraries for extended version
31-
RUN xx-apk add --no-scripts --no-cache gcc g++ musl-dev git
36+
# For --mount=type=cache the value of target is the default cache id, so
37+
# for the go mod cache it would be good if we could share it with other Go images using the same setup,
38+
# but the go build cache needs to be per platform.
39+
# See this comment: https://github.com/moby/buildkit/issues/1706#issuecomment-702238282
3240
RUN --mount=target=. \
33-
--mount=type=cache,target=/go/pkg/mod <<EOT
41+
--mount=type=cache,target=/go/pkg/mod \
42+
--mount=type=cache,target=/root/.cache/go-build,id=go-build-$TARGETPLATFORM <<EOT
3443
set -ex
35-
xx-go build -tags "$HUGO_BUILD_TAGS" -o /usr/bin/hugo
44+
xx-go build -tags "$HUGO_BUILD_TAGS" -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.vendorInfo=docker" -o /usr/bin/hugo
3645
xx-verify /usr/bin/hugo
3746
EOT
3847

39-
# ---
40-
41-
FROM alpine:3.18
48+
FROM gorun AS final
4249

4350
COPY --from=build /usr/bin/hugo /usr/bin/hugo
4451

45-
# libc6-compat & libstdc++ are required for extended SASS libraries
46-
# ca-certificates are required to fetch outside resources (like Twitter oEmbeds)
47-
RUN apk update && \
48-
apk add --no-cache ca-certificates libc6-compat libstdc++ git
52+
# libc6-compat are required for extended libraries (libsass, libwebp).
53+
RUN apk add --no-cache \
54+
libc6-compat \
55+
git \
56+
runuser \
57+
curl \
58+
nodejs \
59+
npm
4960

50-
VOLUME /site
51-
WORKDIR /site
61+
RUN mkdir -p /var/hugo/bin && \
62+
addgroup -Sg 1000 hugo && \
63+
adduser -Sg hugo -u 1000 -h /var/hugo hugo && \
64+
chown -R hugo: /var/hugo && \
65+
# For the Hugo's Git integration to work.
66+
runuser -u hugo -- git config --global --add safe.directory /project && \
67+
# See https://github.com/gohugoio/hugo/issues/9810
68+
runuser -u hugo -- git config --global core.quotepath false
69+
70+
VOLUME /project
71+
WORKDIR /project
72+
USER hugo:hugo
73+
ENV HUGO_CACHEDIR=/cache
74+
ARG BUILDARCH
75+
ENV BUILDARCH=${BUILDARCH}
76+
ENV PATH="/var/hugo/bin:$PATH"
77+
78+
COPY scripts/docker scripts/docker
79+
COPY scripts/docker/entrypoint.sh /entrypoint.sh
80+
81+
# Install default dependencies.
82+
RUN scripts/docker/install_runtimedeps_default.sh
83+
# Update PATH to reflect the new dependencies.
84+
# For more complex setups, we should probably find a way to
85+
# delegate this to the script itself, but this will have to do for now.
86+
# Also, the dart-sass binary is a little special, other binaries can be put/linked
87+
# directly in /var/hugo/bin.
88+
ENV PATH="/var/hugo/bin/dart-sass:$PATH"
5289

5390
# Expose port for live server
5491
EXPOSE 1313
5592

56-
ENTRYPOINT ["hugo"]
93+
ENTRYPOINT ["/entrypoint.sh"]
5794
CMD ["--help"]
95+

‎scripts/docker/entrypoint.sh‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
# Check if a custom hugo-docker-entrypoint.sh file exists.
4+
if [ -f hugo-docker-entrypoint.sh ]; then
5+
# Execute the custom entrypoint file.
6+
sh hugo-docker-entrypoint.sh "$@"
7+
exit $?
8+
fi
9+
10+
# Check if a package.json file exists.
11+
if [ -f package.json ]; then
12+
# Check if node_modules exists.
13+
if [ ! -d node_modules ]; then
14+
# Install npm packages.
15+
# Note that we deliberately do not use `npm ci` here, as it would fail if the package-lock.json file is not up-to-date,
16+
# which would be the case if you run the container with a different OS or architecture than the one used to create the package-lock.json file.
17+
npm i
18+
fi
19+
fi
20+
21+
exec "hugo" "$@"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
export DART_SASS_VERSION=1.79.3
6+
7+
# If $BUILDARCH=arm64, then we need to install the arm64 version of Dart Sass,
8+
# otherwise we install the x64 version.
9+
ARCH="x64"
10+
if [ "$BUILDARCH" = "arm64" ]; then
11+
ARCH="arm64"
12+
fi
13+
14+
cd /tmp
15+
curl -LJO https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-${ARCH}.tar.gz
16+
ls -ltr
17+
tar -xf dart-sass-${DART_SASS_VERSION}-linux-${ARCH}.tar.gz
18+
rm dart-sass-${DART_SASS_VERSION}-linux-${ARCH}.tar.gz && \
19+
# The dart-sass folder is added to the PATH by the caller.
20+
mv dart-sass /var/hugo/bin

0 commit comments

Comments
 (0)