Refactor Docker build workflows (#1221)

* Initial implementation

* Multiple fixes and improvemens

* Better `curl` retries timing

* Remove unused `TARGETOS` and `TARGETVARIANT` vars; add the version fetch fallback code to the build scripts

* Move the fallback code before the var usage

* Use double braces to make the code compatible with the `set -u`

---------

Co-authored-by: Konstantin Koval
This commit is contained in:
Kostiantyn
2025-06-26 11:58:50 +03:00
committed by GitHub
parent 73ae3e8132
commit a8edb30ae4
13 changed files with 371 additions and 144 deletions

View File

@@ -1,5 +1,8 @@
### Build UI ###
FROM --platform=${BUILDPLATFORM:-linux/amd64} node:18 AS ui
ARG TARGETARCH
ARG GITHUB_SHA
ARG VERSION
# See for details: https://github.com/hadolint/hadolint/wiki/DL4006
SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
@@ -7,36 +10,28 @@ SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
ARG NODE_ENV
ENV NODE_ENV=${NODE_ENV:-production}
# Set environment variable REACT_APP_API_ENDPOINT from build args, uses "<web server>/api" as default
ARG REACT_APP_API_ENDPOINT
ENV REACT_APP_API_ENDPOINT=${REACT_APP_API_ENDPOINT}
# Set environment variable UI_PUBLIC_URL from build args, uses "/" as default
# Set environment variable UI_PUBLIC_URL from build args, uses "<web server>/" as default
ARG UI_PUBLIC_URL
ENV UI_PUBLIC_URL=${UI_PUBLIC_URL:-/}
ARG VERSION
ENV VERSION=${VERSION:-undefined}
ENV REACT_APP_BUILD_VERSION=${VERSION:-undefined}
ARG BUILD_DATE
ENV BUILD_DATE=${BUILD_DATE:-undefined}
ENV REACT_APP_BUILD_DATE=${BUILD_DATE:-undefined}
ARG COMMIT_SHA
ENV COMMIT_SHA=${COMMIT_SHA:-}
ENV REACT_APP_BUILD_COMMIT_SHA=${COMMIT_SHA:-}
WORKDIR /app/ui
COPY ui/package.json ui/package-lock.json /app/ui/
RUN npm ci
COPY ui/ /app/ui
RUN if [ "${BUILD_DATE}" = "undefined" ]; then \
export BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ'); \
# hadolint ignore=SC2155
RUN export BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ'); \
export REACT_APP_BUILD_DATE=${BUILD_DATE}; \
fi; \
npm run build -- --base="${UI_PUBLIC_URL}"
export COMMIT_SHA=${GITHUB_SHA:-$(git rev-parse --short HEAD || echo 000000)}; \
export REACT_APP_BUILD_COMMIT_SHA=${COMMIT_SHA}; \
export VERSION="${VERSION:-$(git rev-parse --abbrev-ref HEAD || echo unknown-branch)}-${TARGETARCH}"; \
export REACT_APP_BUILD_VERSION=${VERSION}; \
npm run build -- --base="${UI_PUBLIC_URL}"
### Build API ###
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.24-bookworm AS api
@@ -54,39 +49,39 @@ ENV CGO_ENABLED=1
# Download dependencies
COPY scripts/set_compiler_env.sh /app/scripts/
RUN chmod +x /app/scripts/*.sh \
&& source /app/scripts/set_compiler_env.sh
&& source /app/scripts/set_compiler_env.sh
COPY scripts/install_*.sh /app/scripts/
# Split values in `/env`
# hadolint ignore=SC2046
RUN chmod +x /app/scripts/*.sh \
&& export $(cat /env) \
&& /app/scripts/install_build_dependencies.sh \
&& /app/scripts/install_runtime_dependencies.sh
&& export $(cat /env) \
&& /app/scripts/install_build_dependencies.sh \
&& /app/scripts/install_runtime_dependencies.sh
COPY --from=photoview/dependencies /artifacts.tar.gz /dependencies/
# Split values in `/env`
# hadolint ignore=SC2046
RUN export $(cat /env) \
&& git config --global --add safe.directory /app \
&& cd /dependencies/ \
&& tar xfv artifacts.tar.gz \
&& cp -a include/* /usr/local/include/ \
&& cp -a pkgconfig/* ${PKG_CONFIG_PATH} \
&& cp -a lib/* /usr/local/lib/ \
&& ldconfig \
&& apt-get install -y ./deb/jellyfin-ffmpeg.deb
&& git config --global --add safe.directory /app \
&& cd /dependencies/ \
&& tar xfv artifacts.tar.gz \
&& cp -a include/* /usr/local/include/ \
&& cp -a pkgconfig/* ${PKG_CONFIG_PATH} \
&& cp -a lib/* /usr/local/lib/ \
&& ldconfig \
&& apt-get install -y ./deb/jellyfin-ffmpeg.deb
COPY api/go.mod api/go.sum /app/api/
# Split values in `/env`
# hadolint ignore=SC2046
RUN export $(cat /env) \
&& go env \
&& go mod download \
# Patch go-face
&& sed -i 's/-march=native//g' ${GOPATH}/pkg/mod/github.com/!kagami/go-face*/face.go \
# Build dependencies that use CGO
&& go install \
&& go env \
&& go mod download \
# Patch go-face
&& sed -i 's/-march=native//g' ${GOPATH}/pkg/mod/github.com/!kagami/go-face*/face.go \
# Build dependencies that use CGO
&& go install \
github.com/mattn/go-sqlite3 \
github.com/Kagami/go-face
@@ -94,8 +89,8 @@ COPY api /app/api
# Split values in `/env`
# hadolint ignore=SC2046
RUN export $(cat /env) \
&& go env \
&& go build -v -o photoview .
&& go env \
&& go build -v -o photoview .
### Build release image ###
FROM debian:bookworm-slim AS release
@@ -106,23 +101,23 @@ SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
COPY scripts/install_runtime_dependencies.sh /app/scripts/
RUN --mount=type=bind,from=api,source=/dependencies/,target=/dependencies/ \
chmod +x /app/scripts/install_runtime_dependencies.sh \
# Create a user to run Photoview server
&& groupadd -g 999 photoview \
&& useradd -r -u 999 -g photoview -m photoview \
# Install required dependencies
&& /app/scripts/install_runtime_dependencies.sh \
# Install self-building libs
&& cd /dependencies \
&& cp -a lib/*.so* /usr/local/lib/ \
&& ldconfig \
&& apt-get install -y ./deb/jellyfin-ffmpeg.deb \
&& ln -s /usr/lib/jellyfin-ffmpeg/ffmpeg /usr/local/bin/ \
&& ln -s /usr/lib/jellyfin-ffmpeg/ffprobe /usr/local/bin/ \
# Cleanup
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
chmod +x /app/scripts/install_runtime_dependencies.sh \
# Create a user to run Photoview server
&& groupadd -g 999 photoview \
&& useradd -r -u 999 -g photoview -m photoview \
# Install required dependencies
&& /app/scripts/install_runtime_dependencies.sh \
# Install self-building libs
&& cd /dependencies \
&& cp -a lib/*.so* /usr/local/lib/ \
&& ldconfig \
&& apt-get install -y ./deb/jellyfin-ffmpeg.deb \
&& ln -s /usr/lib/jellyfin-ffmpeg/ffmpeg /usr/local/bin/ \
&& ln -s /usr/lib/jellyfin-ffmpeg/ffprobe /usr/local/bin/ \
# Cleanup
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY api/data /app/data
COPY --from=ui /app/ui/dist /app/ui
@@ -141,7 +136,7 @@ ENV PHOTOVIEW_MEDIA_CACHE=/home/photoview/media-cache
EXPOSE ${PHOTOVIEW_LISTEN_PORT}
HEALTHCHECK --interval=60s --timeout=10s \
CMD curl --fail http://localhost:${PHOTOVIEW_LISTEN_PORT}/api/graphql \
CMD curl --fail http://localhost:${PHOTOVIEW_LISTEN_PORT}/api/graphql \
-X POST -H 'Content-Type: application/json' \
--data-raw '{"operationName":"CheckInitialSetup","variables":{},"query":"query CheckInitialSetup { siteInfo { initialSetup }}"}' \
|| exit 1