Skip to content

Commit e6e2d03

Browse files
Mmx233xhofe
andauthored
perf: make docker release 10 times faster (#5803)
* build: improve multistage docker build * build: add dockerfile for ci * build: add BuildDockerMultiplatform function in build.sh for ci * ci: change build method * build: add missing mod download command to the Dockerfile * build: revert changes made ffmpeg installed * build: use musl build for docker release * ci: apply to dev version * fix: don't login on pr * fix: don't build_docker_with_aria2 on pr --------- Co-authored-by: Andy Hsu <i@nn.ci>
1 parent 28bb3f6 commit e6e2d03

File tree

6 files changed

+113
-13
lines changed

6 files changed

+113
-13
lines changed

.github/workflows/build_docker.yml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,63 @@ name: build_docker
33
on:
44
push:
55
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
68

79
concurrency:
810
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
911
cancel-in-progress: true
1012

1113
jobs:
1214
build_docker:
13-
name: Build docker
15+
name: Build Docker
1416
runs-on: ubuntu-latest
1517
steps:
1618
- name: Checkout
1719
uses: actions/checkout@v4
20+
21+
- uses: actions/setup-go@v4
22+
with:
23+
go-version: 'stable'
24+
25+
- name: Build go binary
26+
run: bash build.sh dev docker-multiplatform
27+
1828
- name: Docker meta
1929
id: meta
2030
uses: docker/metadata-action@v5
2131
with:
2232
images: xhofe/alist
23-
- name: Replace release with dev
24-
run: |
25-
sed -i 's/release/dev/g' Dockerfile
33+
2634
- name: Set up QEMU
2735
uses: docker/setup-qemu-action@v3
36+
2837
- name: Set up Docker Buildx
2938
uses: docker/setup-buildx-action@v3
39+
3040
- name: Login to DockerHub
41+
if: github.event_name == 'push'
3142
uses: docker/login-action@v3
3243
with:
3344
username: xhofe
3445
password: ${{ secrets.DOCKERHUB_TOKEN }}
46+
3547
- name: Build and push
3648
id: docker_build
3749
uses: docker/build-push-action@v5
3850
with:
3951
context: .
40-
push: true
52+
file: Dockerfile.ci
53+
push: ${{ github.event_name == 'push' }}
4154
tags: ${{ steps.meta.outputs.tags }}
4255
labels: ${{ steps.meta.outputs.labels }}
43-
platforms: linux/amd64,linux/arm64
56+
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386,linux/arm/v6,linux/s390x
4457

4558
build_docker_with_aria2:
4659
needs: build_docker
4760
name: Build docker with aria2
4861
runs-on: ubuntu-latest
62+
if: github.event_name == 'push'
4963
steps:
5064
- name: Checkout repo
5165
uses: actions/checkout@v4

.github/workflows/release_docker.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ jobs:
1313
- name: Checkout
1414
uses: actions/checkout@v4
1515

16+
- uses: actions/setup-go@v4
17+
with:
18+
go-version: 'stable'
19+
20+
- name: Build go binary
21+
run: bash build.sh release docker-multiplatform
22+
1623
- name: Docker meta
1724
id: meta
1825
uses: docker/metadata-action@v5
@@ -36,6 +43,7 @@ jobs:
3643
uses: docker/build-push-action@v5
3744
with:
3845
context: .
46+
file: Dockerfile.ci
3947
push: true
4048
tags: ${{ steps.meta.outputs.tags }}
4149
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
FROM alpine:edge as builder
22
LABEL stage=go-builder
33
WORKDIR /app/
4+
RUN apk add --no-cache bash curl gcc git go musl-dev
5+
COPY go.mod go.sum ./
6+
RUN go mod download
47
COPY ./ ./
5-
RUN apk add --no-cache bash curl gcc git go musl-dev; \
6-
bash build.sh release docker
8+
RUN bash build.sh release docker
79

810
FROM alpine:edge
911
LABEL MAINTAINER="i@nn.ci"
1012
VOLUME /opt/alist/data/
1113
WORKDIR /opt/alist/
1214
COPY --from=builder /app/bin/alist ./
1315
COPY entrypoint.sh /entrypoint.sh
14-
RUN apk add --no-cache bash ca-certificates su-exec tzdata; \
15-
chmod +x /entrypoint.sh
16+
RUN apk update && \
17+
apk upgrade --no-cache && \
18+
apk add --no-cache bash ca-certificates su-exec tzdata; \
19+
chmod +x /entrypoint.sh && \
20+
rm -rf /var/cache/apk/*
1621
ENV PUID=0 PGID=0 UMASK=022
1722
EXPOSE 5244 5245
18-
CMD [ "/entrypoint.sh" ]
23+
CMD [ "/entrypoint.sh" ]

Dockerfile.ci

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM alpine:edge
2+
ARG TARGETPLATFORM
3+
LABEL MAINTAINER="i@nn.ci"
4+
VOLUME /opt/alist/data/
5+
WORKDIR /opt/alist/
6+
COPY /${TARGETPLATFORM}/alist ./
7+
COPY entrypoint.sh /entrypoint.sh
8+
RUN apk update && \
9+
apk upgrade --no-cache && \
10+
apk add --no-cache bash ca-certificates su-exec tzdata; \
11+
chmod +x /entrypoint.sh && \
12+
rm -rf /var/cache/apk/* && \
13+
/entrypoint.sh version
14+
ENV PUID=0 PGID=0 UMASK=022
15+
EXPOSE 5244 5245
16+
CMD [ "/entrypoint.sh" ]

build.sh

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,61 @@ BuildDev() {
8585
cat md5.txt
8686
}
8787

88-
BuildDocker() {
88+
PrepareBuildDocker() {
8989
echo "replace github.com/mattn/go-sqlite3 => github.com/leso-kn/go-sqlite3 v0.0.0-20230710125852-03158dc838ed" >>go.mod
9090
go get gorm.io/driver/sqlite@v1.4.4
91+
go mod download
92+
}
93+
94+
BuildDocker() {
95+
PrepareBuildDocker
9196
go build -o ./bin/alist -ldflags="$ldflags" -tags=jsoniter .
9297
}
9398

99+
BuildDockerMultiplatform() {
100+
PrepareBuildDocker
101+
102+
BASE="https://musl.cc/"
103+
FILES=(x86_64-linux-musl-cross aarch64-linux-musl-cross i486-linux-musl-cross s390x-linux-musl-cross armv6-linux-musleabihf-cross armv7l-linux-musleabihf-cross)
104+
for i in "${FILES[@]}"; do
105+
url="${BASE}${i}.tgz"
106+
curl -L -o "${i}.tgz" "${url}"
107+
sudo tar xf "${i}.tgz" --strip-components 1 -C /usr/local
108+
rm -f "${i}.tgz"
109+
done
110+
111+
docker_lflags="--extldflags '-static -fpic' $ldflags"
112+
export CGO_ENABLED=1
113+
114+
OS_ARCHES=(linux-amd64 linux-arm64 linux-386 linux-s390x)
115+
CGO_ARGS=(x86_64-linux-musl-gcc aarch64-linux-musl-gcc i486-linux-musl-gcc s390x-linux-musl-gcc)
116+
for i in "${!OS_ARCHES[@]}"; do
117+
os_arch=${OS_ARCHES[$i]}
118+
cgo_cc=${CGO_ARGS[$i]}
119+
os=${os_arch%%-*}
120+
arch=${os_arch##*-}
121+
export GOOS=$os
122+
export GOARCH=$arch
123+
export CC=${cgo_cc}
124+
echo "building for $os_arch"
125+
go build -o ./$os/$arch/alist -ldflags="$docker_lflags" -tags=jsoniter .
126+
done
127+
128+
DOCKER_ARM_ARCHES=(linux-arm/v6 linux-arm/v7)
129+
CGO_ARGS=(armv6-linux-musleabihf-gcc armv7l-linux-musleabihf-gcc)
130+
GO_ARM=(6 7)
131+
export GOOS=linux
132+
export GOARCH=arm
133+
for i in "${!DOCKER_ARM_ARCHES[@]}"; do
134+
docker_arch=${DOCKER_ARM_ARCHES[$i]}
135+
cgo_cc=${CGO_ARGS[$i]}
136+
export GOARM=${GO_ARM[$i]}
137+
export CC=${cgo_cc}
138+
echo "building for $docker_arch"
139+
go build -o ./${docker_arch%%-*}/${docker_arch##*-}/alist -ldflags="$docker_lflags" -tags=jsoniter .
140+
done
141+
}
142+
94143
BuildRelease() {
95144
rm -rf .git/
96145
mkdir -p "build"
@@ -190,13 +239,17 @@ if [ "$1" = "dev" ]; then
190239
FetchWebDev
191240
if [ "$2" = "docker" ]; then
192241
BuildDocker
242+
elif [ "$2" = "docker-multiplatform" ]; then
243+
BuildDockerMultiplatform
193244
else
194245
BuildDev
195246
fi
196247
elif [ "$1" = "release" ]; then
197248
FetchWebRelease
198249
if [ "$2" = "docker" ]; then
199250
BuildDocker
251+
elif [ "$2" = "docker-multiplatform" ]; then
252+
BuildDockerMultiplatform
200253
elif [ "$2" = "linux_musl_arm" ]; then
201254
BuildReleaseLinuxMuslArm
202255
MakeRelease "md5-linux-musl-arm.txt"

entrypoint.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ chown -R ${PUID}:${PGID} /opt/alist/
44

55
umask ${UMASK}
66

7-
exec su-exec ${PUID}:${PGID} ./alist server --no-prefix
7+
if [ "$1" = "version" ]; then
8+
./alist version
9+
else
10+
exec su-exec ${PUID}:${PGID} ./alist server --no-prefix
11+
fi

0 commit comments

Comments
 (0)