forgejo/Dockerfile.ci

124 lines
3.6 KiB
Docker

FROM --platform=$BUILDPLATFORM golang:1.19 AS build-env
ARG GOPROXY
ENV GOPROXY "${GOPROXY:-direct}"
ARG TAG
ENV TAG ${TAG:-main}
ARG CI_COMMIT_SHA
ENV CI_COMMIT_SHA "${CI_COMMIT_SHA}"
ARG CI_REPO_CLONE_URL
ENV CI_REPO_CLONE_URL "${CI_REPO_CLONE_URL}"
# Shorthands for easy use and ease of replacement
ARG OUT
ENV OUT "${OUT:-/out-bins}"
ARG DR
ENV DR "${DR:-/docker/root}"
# Copying the arch specific binaries to the path easily accessible in the next step and rename them to gitea
COPY --chmod=755 tmp-bin/release/forgejo-$TAG-linux-amd64 "${OUT}/linux/amd64/gitea"
COPY --chmod=755 tmp-bin/release/forgejo-$TAG-linux-arm64 "${OUT}/linux/arm64/gitea"
WORKDIR /
# Check vars and create directories
RUN test -n "$CI_COMMIT_SHA" || (echo "CI_COMMIT_SHA is required but not set" >&2 && exit 1) && \
test -n "$CI_REPO_CLONE_URL" || (echo "CI_REPO_CLONE_URL is required but not set" >&2 && exit 1) && \
echo "Running prep for ${CI_REPO_CLONE_URL} commit ${CI_COMMIT_SHA}" && \
echo "Preparin out directories ${OUT}" && \
mkdir -p "${OUT}/linux/amd64" && \
mkdir -p "${OUT}/linux/arm64" && \
echo "Creating build dir" && \
mkdir /build && \
cd /build && \
echo "setting up git" && \
# quiet down git
git config --global init.defaultBranch dummy && \
git config --global advice.detachedHead false && \
git init && \
git remote add origin "${CI_REPO_CLONE_URL}" && \
# shallow clone as we just need the current data, keeps the "clone" really small
git fetch --depth 1 origin ${CI_COMMIT_SHA} && \
git checkout FETCH_HEAD && \
export ENV2INI="environment-to-ini" && \
export GOFILE="contrib/$ENV2INI/$ENV2INI.go" && \
echo "compiling for linux/amd64" && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "${OUT}/linux/amd64/$ENV2INI" "${GOFILE}" && \
echo "compiling for linux/arm64" && \
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o "${OUT}/linux/arm64/$ENV2INI" "${GOFILE}" && \
cd / && \
ls -la /* && \
echo "Creating docker root prep dir ${DR}" && \
mkdir -p "${DR}" && \
echo "Copying future root addons" && \
ls -la "/build/docker/" && \
ls -la "/build/docker/root/*" && \
cp -a "/build/docker/root/." "${DR}/" && \
ls -la "${DR}/*" && \
echo "Fixing permissions" && \
chmod 755 "${DR}/usr/bin/entrypoint" "${DR}/usr/local/bin/gitea" "${DR}/etc/s6/gitea/*" "${DR}/etc/s6/openssh/*" "${DR}/etc/s6/.s6-svscan/*" && \
echo "removing git repo" && \
rm -Rf /build && \
echo "removing go cached files" && \
rm -Rf "$(go env GOCACHE)" && \
rm -Rf "$(go env GOROOT)/pkg" && \
rm -Rf "$(go env GOPATH)/pkg"
# Main build
FROM alpine:3.17.0
LABEL maintainer="contact@forgejo.org"
EXPOSE 22 3000
RUN apk --no-cache add \
bash \
ca-certificates \
curl \
gettext \
git \
linux-pam \
openssh \
s6 \
sqlite \
su-exec \
gnupg \
tzdata
RUN addgroup \
-S -g 1000 \
git && \
adduser \
-S -H -D \
-h /data/git \
-s /bin/bash \
-u 1000 \
-G git \
git && \
echo "git:*" | chpasswd -e
ENV USER git
ENV GITEA_CUSTOM /data/gitea
VOLUME ["/data"]
ENTRYPOINT ["/usr/bin/entrypoint"]
CMD ["/bin/s6-svscan", "/etc/s6"]
ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
# Shorthands for easy use and ease of replacement
ARG OUT
ENV OUT "${OUT:-/out-bins}"
ARG DR
ENV DR "${DR:-"/docker/root"}"
COPY --from=build-env "${DR}" /
COPY --from=build-env --chmod=755 "${OUT}/$TARGETPLATFORM/*" /app/gitea/
# unsure it it's better to just use 2 copys and bring it to the correct place
# or ln -s (had mixed results usually ln -s makes a smaller end result)
RUN ln -s /app/gitea/environment-to-ini /usr/local/bin/environment-to-ini