Skip to content

Commit d44c78b

Browse files
authored
Workflows: Refactor docker (#4738)
* Workflows: Rewrite Docker image build process * Docker: Merge Multi-Arch Manifests
1 parent d0c80fc commit d44c78b

File tree

4 files changed

+162
-62
lines changed

4 files changed

+162
-62
lines changed

.github/docker/Dockerfile

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,61 @@
1-
# syntax=docker/dockerfile:1
2-
FROM --platform=$BUILDPLATFORM golang:alpine AS build
1+
# syntax=docker/dockerfile:latest
2+
FROM --platform=$BUILDPLATFORM golang:latest AS build
3+
4+
# Build xray-core
35
WORKDIR /src
46
COPY . .
57
ARG TARGETOS
68
ARG TARGETARCH
79
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -o xray -trimpath -ldflags "-s -w -buildid=" ./main
8-
ADD https://github.com/v2fly/geoip/releases/latest/download/geoip.dat /v2fly/geoip.dat
9-
ADD https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat /v2fly/geosite.dat
10-
ADD https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat /loyalsoldier/geoip.dat
11-
ADD https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat /loyalsoldier/geosite.dat
12-
13-
# chainguard/static contains only tzdata and ca-certificates, can be built with multiarch static binaries.
14-
FROM --platform=linux/amd64 chainguard/static:latest
15-
WORKDIR /var/log/xray
16-
COPY .github/docker/files/config.json /etc/xray/config.json
17-
COPY --from=build --chmod=755 /src/xray /usr/bin/xray
18-
19-
USER root
20-
WORKDIR /root
21-
VOLUME /etc/xray
22-
ARG TZ=Asia/Shanghai
10+
11+
# Download geodat into a staging directory
12+
ADD https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geoip.dat /tmp/geodat/geoip.dat
13+
ADD https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geosite.dat /tmp/geodat/geosite.dat
14+
15+
RUN mkdir -p /tmp/empty
16+
17+
# Create config files with empty JSON content
18+
RUN mkdir -p /tmp/usr/local/etc/xray
19+
RUN cat <<EOF >/tmp/usr/local/etc/xray/00_log.json
20+
{
21+
"log": {
22+
"error": "/var/log/xray/error.log",
23+
"loglevel": "warning",
24+
"access": "none",
25+
"dnsLog": false
26+
}
27+
}
28+
EOF
29+
RUN echo '{}' >/tmp/usr/local/etc/xray/01_api.json
30+
RUN echo '{}' >/tmp/usr/local/etc/xray/02_dns.json
31+
RUN echo '{}' >/tmp/usr/local/etc/xray/03_routing.json
32+
RUN echo '{}' >/tmp/usr/local/etc/xray/04_policy.json
33+
RUN echo '{}' >/tmp/usr/local/etc/xray/05_inbounds.json
34+
RUN echo '{}' >/tmp/usr/local/etc/xray/06_outbounds.json
35+
RUN echo '{}' >/tmp/usr/local/etc/xray/07_transport.json
36+
RUN echo '{}' >/tmp/usr/local/etc/xray/08_stats.json
37+
RUN echo '{}' >/tmp/usr/local/etc/xray/09_reverse.json
38+
39+
# Create log files
40+
RUN mkdir -p /tmp/var/log/xray && touch \
41+
/tmp/var/log/xray/access.log \
42+
/tmp/var/log/xray/error.log
43+
44+
# Build finally image
45+
FROM gcr.io/distroless/static:nonroot
46+
47+
COPY --from=build --chown=0:0 --chmod=755 /src/xray /usr/local/bin/xray
48+
COPY --from=build --chown=0:0 --chmod=644 /tmp/geodat/*.dat /usr/local/share/xray/
49+
COPY --from=build --chown=0:0 --chmod=755 /tmp/empty /usr/local/etc/xray
50+
COPY --from=build --chown=0:0 --chmod=644 /tmp/usr/local/etc/xray/*.json /usr/local/etc/xray/
51+
COPY --from=build --chown=0:0 --chmod=755 /tmp/empty /var/log/xray
52+
COPY --from=build --chown=65532:65532 --chmod=600 /tmp/var/log/xray/*.log /var/log/xray/
53+
54+
VOLUME /usr/local/etc/xray
55+
VOLUME /var/log/xray
56+
57+
ARG TZ=Etc/UTC
2358
ENV TZ=$TZ
24-
ENTRYPOINT [ "/usr/bin/xray" ]
25-
CMD [ "-confdir", "/etc/xray/" ]
2659

27-
ARG flavor=v2fly
28-
COPY --from=build --chmod=644 /$flavor /usr/share/xray
60+
ENTRYPOINT [ "/usr/local/bin/xray" ]
61+
CMD [ "-confdir", "/usr/local/etc/xray/" ]

.github/docker/Dockerfile.usa

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# syntax=docker/dockerfile:latest
2+
FROM --platform=$BUILDPLATFORM golang:latest AS build
3+
4+
# Build xray-core
5+
WORKDIR /src
6+
COPY . .
7+
ARG TARGETOS
8+
ARG TARGETARCH
9+
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -o xray -trimpath -ldflags "-s -w -buildid=" ./main
10+
11+
# Download geodat into a staging directory
12+
ADD https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geoip.dat /tmp/geodat/geoip.dat
13+
ADD https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geosite.dat /tmp/geodat/geosite.dat
14+
15+
RUN mkdir -p /tmp/empty
16+
17+
# Create config files with empty JSON content
18+
RUN mkdir -p /tmp/usr/local/etc/xray
19+
RUN cat <<EOF >/tmp/usr/local/etc/xray/00_log.json
20+
{
21+
"log": {
22+
"error": "/var/log/xray/error.log",
23+
"loglevel": "warning",
24+
"access": "none",
25+
"dnsLog": false
26+
}
27+
}
28+
EOF
29+
RUN echo '{}' >/tmp/usr/local/etc/xray/01_api.json
30+
RUN echo '{}' >/tmp/usr/local/etc/xray/02_dns.json
31+
RUN echo '{}' >/tmp/usr/local/etc/xray/03_routing.json
32+
RUN echo '{}' >/tmp/usr/local/etc/xray/04_policy.json
33+
RUN echo '{}' >/tmp/usr/local/etc/xray/05_inbounds.json
34+
RUN echo '{}' >/tmp/usr/local/etc/xray/06_outbounds.json
35+
RUN echo '{}' >/tmp/usr/local/etc/xray/07_transport.json
36+
RUN echo '{}' >/tmp/usr/local/etc/xray/08_stats.json
37+
RUN echo '{}' >/tmp/usr/local/etc/xray/09_reverse.json
38+
39+
# Create log files
40+
RUN mkdir -p /tmp/var/log/xray && touch \
41+
/tmp/var/log/xray/access.log \
42+
/tmp/var/log/xray/error.log
43+
44+
# Build finally image
45+
# Note on Distroless Base Image and Architecture Support:
46+
# - The official 'gcr.io/distroless/static' image provided by Google only supports a limited set of architectures for Linux:
47+
# - linux/amd64
48+
# - linux/arm/v7
49+
# - linux/arm64/v8
50+
# - linux/ppc64le
51+
# - linux/s390x
52+
# - Upon inspection, the blob contents of the Distroless images across these architectures are nearly identical, with only minor differences in metadata (e.g., 'Architecture' field in the manifest).
53+
# - Due to this similarity in content, it is feasible to forcibly specify a single platform (e.g., '--platform=linux/amd64') for unsupported architectures, as the core image content remains compatible with statically compiled binaries like Go applications.
54+
FROM --platform=linux/amd64 gcr.io/distroless/static:nonroot
55+
56+
COPY --from=build --chown=0:0 --chmod=755 /src/xray /usr/local/bin/xray
57+
COPY --from=build --chown=0:0 --chmod=644 /tmp/geodat/*.dat /usr/local/share/xray/
58+
COPY --from=build --chown=0:0 --chmod=755 /tmp/empty /usr/local/etc/xray
59+
COPY --from=build --chown=0:0 --chmod=644 /tmp/usr/local/etc/xray/*.json /usr/local/etc/xray/
60+
COPY --from=build --chown=0:0 --chmod=755 /tmp/empty /var/log/xray
61+
COPY --from=build --chown=65532:65532 --chmod=600 /tmp/var/log/xray/*.log /var/log/xray/
62+
63+
VOLUME /usr/local/etc/xray
64+
VOLUME /var/log/xray
65+
66+
ARG TZ=Etc/UTC
67+
ENV TZ=$TZ
68+
69+
ENTRYPOINT [ "/usr/local/bin/xray" ]
70+
CMD [ "-confdir", "/usr/local/etc/xray/" ]

.github/docker/files/config.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/docker.yml

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ name: Build docker image
33
on:
44
release:
55
types: [published]
6-
push:
7-
branches:
8-
- main
96

107
jobs:
118
build-image:
@@ -14,63 +11,81 @@ jobs:
1411
packages: write
1512
steps:
1613
- uses: actions/checkout@v4
14+
1715
- name: Docker metadata
1816
id: meta
1917
uses: docker/metadata-action@v5
2018
with:
2119
images: ghcr.io/${{ github.repository_owner }}/xray-core
2220
flavor: latest=auto
2321
tags: |
24-
type=sha
25-
type=ref,event=branch
26-
type=ref,event=pr
2722
type=semver,pattern={{version}}
28-
- name: Docker metadata Loyalsoldier flavor
29-
id: loyalsoldier
23+
24+
- name: Docker metadata (unsupported architectures)
25+
id: metausa
3026
uses: docker/metadata-action@v5
3127
with:
3228
images: ghcr.io/${{ github.repository_owner }}/xray-core
3329
flavor: |
3430
latest=auto
35-
suffix=-ls,onlatest=true
31+
suffix=-usa,onlatest=true
3632
tags: |
37-
type=sha
38-
type=ref,event=branch
39-
type=ref,event=pr
4033
type=semver,pattern={{version}}
34+
4135
- name: Login to GitHub Container Registry
4236
uses: docker/login-action@v3
4337
with:
4438
registry: ghcr.io
4539
username: ${{ github.repository_owner }}
4640
password: ${{ secrets.GITHUB_TOKEN }}
41+
4742
- name: Set up Docker Buildx
4843
uses: docker/setup-buildx-action@v3
44+
4945
- name: Build and push
5046
uses: docker/build-push-action@v6
5147
with:
5248
context: .
5349
platforms: |
5450
linux/amd64
55-
linux/arm64
56-
linux/loong64
57-
linux/riscv64
51+
linux/arm/v7
52+
linux/arm64/v8
53+
linux/ppc64le
54+
linux/s390x
5855
provenance: false
5956
file: .github/docker/Dockerfile
6057
push: true
6158
tags: ${{ steps.meta.outputs.tags }}
62-
- name: Build and push Loyalsoldier flavor
59+
60+
- name: Build and push (unsupported architectures)
6361
uses: docker/build-push-action@v6
6462
with:
6563
context: .
6664
platforms: |
67-
linux/amd64
68-
linux/arm64
69-
linux/loong64
65+
linux/386
66+
linux/arm/v6
7067
linux/riscv64
68+
linux/loong64
7169
provenance: false
72-
file: .github/docker/Dockerfile
73-
build-args: flavor=loyalsoldier
70+
file: .github/docker/Dockerfile.usa
7471
push: true
75-
tags: |
76-
${{ steps.loyalsoldier.outputs.tags }}
72+
tags: ${{ steps.metausa.outputs.tags }}
73+
74+
- name: Merge Multi-Arch Manifests
75+
run: |
76+
echo "Starting to merge multi-architecture manifests..."
77+
78+
# Convert newlines to spaces and split into array
79+
TAGS=($(echo "${{ steps.meta.outputs.tags }}" | tr '\n' ' '))
80+
81+
echo "Total tags to process: ${#TAGS[@]}"
82+
for tag in "${TAGS[@]}"; do
83+
echo "Merging tag: $tag with unsupported architectures ($tag-usa)"
84+
docker buildx imagetools create --append --tag "$tag" "$tag-usa"
85+
if [ $? -ne 0 ]; then
86+
echo "Error: Failed to merge $tag-usa into $tag"
87+
exit 1
88+
fi
89+
done
90+
91+
echo "Multi-architecture manifest merge completed successfully."

0 commit comments

Comments
 (0)