From ed7dd1e8bce71fca10bba1cf57c19f8d81c1e153 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Thu, 5 Oct 2023 09:48:48 +0800 Subject: [PATCH] Feat: release to docker hub --- .github/workflows/release_docker.yml | 42 ++++++++++++++++++++++++ Dockerfile | 33 +++++++++++++++++++ build.sh | 49 ++++++++++++++++++++++++---- entrypoint.sh | 7 ++++ internal/bootstrap/sysNotify.go | 5 ++- server/handlers/rooms.go | 9 +++-- 6 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/release_docker.yml create mode 100644 Dockerfile create mode 100644 entrypoint.sh diff --git a/.github/workflows/release_docker.yml b/.github/workflows/release_docker.yml new file mode 100644 index 0000000..dee59ab --- /dev/null +++ b/.github/workflows/release_docker.yml @@ -0,0 +1,42 @@ +name: release_docker + +on: + push: + tags: + - '*' + +jobs: + release_docker: + name: Release Docker + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v4 + with: + images: synctvorg/synctv + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64,darwin/amd64,darwin/arm64,windows/amd64 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c00399f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +From alpine:latest as builder + +ARG VERSION=v0.0.0 + +WORKDIR /synctv + +COPY ./ ./ + +RUN apk add --no-cache bash curl gcc git go musl-dev + +RUN bash build.sh -P -v ${VERSION} -b build + +From alpine:latest + +WORKDIR /opt/synctv + +ENV SERVER_LISTEN=0.0.0.0 + +ENV SERVER_PORT=8080 + +COPY --from=builder /synctv/build/synctv /usr/local/bin/synctv + +COPY entrypoint.sh /entrypoint.sh + +RUN apk add --no-cache bash ca-certificates su-exec tzdata + +RUN chmod +x /entrypoint.sh + +ENV PUID=0 PGID=0 UMASK=022 + +EXPOSE 8080 8080 + +CMD [ "/entrypoint.sh" ] diff --git a/build.sh b/build.sh index 7427b8d..a274621 100644 --- a/build.sh +++ b/build.sh @@ -14,9 +14,10 @@ function Help() { echo "-w set web version (default: latest releases)" echo "-m set build mode (default: pie)" echo "-l set ldflags (default: -s -w --extldflags \"-static -fpic -Wl,-z,relro,-z,now\")" - echo "-p set platform (default: linux/amd64,darwin/arm64)" + echo "-p set platform (default: host platform, support: all, linux, darwin, windows)" echo "-P set trim path (default: disable)" echo "-b set build result dir (default: build)" + echo "-T set tags (default: jsoniter)" } function Init() { @@ -31,13 +32,14 @@ function Init() { fi BUILD_MODE="pie" LDFLAGS='-s -w --extldflags "-static -fpic -Wl,-z,relro,-z,now"' - PLATFORM="linux/amd64,darwin/arm64" + PLATFORM="" TRIM_PATH="" BUILD_DIR="build" + TAGS="jsoniter" } function ParseArgs() { - while getopts "hv:w:m:l:p:Pb:" arg; do + while getopts "hv:w:m:l:p:Pb:T:" arg; do case $arg in h) Help @@ -64,6 +66,9 @@ function ParseArgs() { b) BUILD_DIR="$OPTARG" ;; + T) + TAGS="$OPTARG" + ;; ?) echo "unkonw argument" exit 1 @@ -126,7 +131,11 @@ function InitDep() { curl -sL "https://github.com/synctv-org/synctv-web/releases/download/${WEB_VERSION}/dist.tar.gz" | tar --strip-components 1 -C "public/dist" -z -x -v -f - } -ALLOWD_PLATFORM="linux/amd64,linux/arm64,darwin/amd64,darwin/arm64,windows/amd64,windows/arm64" +LINUX_ALLOWED_PLATFORM="linux/386,linux/amd64,linux/arm,linux/arm64,linux/loong64,linux/mips,linux/mips64,linux/mips64le,linux/mipsle,linux/ppc64,linux/ppc64le,linux/riscv64,linux/s390x" +DARWIN_ALLOWED_PLATFORM="darwin/amd64,darwin/arm64" +WINDOWS_ALLOWED_PLATFORM="windows/386,windows/amd64,windows/arm,windows/arm64" + +ALLOWED_PLATFORM="$LINUX_ALLOWED_PLATFORM,$DARWIN_ALLOWED_PLATFORM,$WINDOWS_ALLOWED_PLATFORM" function CheckPlatform() { platform="$1" @@ -159,13 +168,41 @@ function Build() { EXT="" fi if [ "$TRIM_PATH" ]; then - GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" . + GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" . else - GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" . + GOOS=$GOOS GOARCH=$GOARCH go build -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" . + fi +} + +function BuildSingle() { + GOOS="$(go env GOOS)" + GOARCH="$(go env GOARCH)" + if [ "$GOOS" == "windows" ]; then + EXT=".exe" + else + EXT="" + fi + echo "build $GOOS/$GOARCH" + if [ "$TRIM_PATH" ]; then + go build -trimpath -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)$EXT" . + else + go build -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)$EXT" . fi } function BuildAll() { + if [ ! "$PLATFORM" ]; then + BuildSingle + return + elif [ "$PLATFORM" == "all" ]; then + PLATFORM="$ALLOWD_PLATFORM" + elif [ "$PLATFORM" == "linux" ]; then + PLATFORM="$LINUX_ALLOWED_PLATFORM" + elif [ "$PLATFORM" == "darwin" ]; then + PLATFORM="$DARWIN_ALLOWED_PLATFORM" + elif [ "$PLATFORM" == "windows" ]; then + PLATFORM="$WINDOWS_ALLOWED_PLATFORM" + fi for platform in $(echo "$PLATFORM" | tr "," "\n"); do Build "$platform" done diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..50fd505 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +chown -R ${PUID}:${PGID} /opt/synctv/ + +umask ${UMASK} + +exec su-exec ${PUID}:${PGID} synctv server --env-no-prefix --skip-config diff --git a/internal/bootstrap/sysNotify.go b/internal/bootstrap/sysNotify.go index 2cb288f..448316b 100644 --- a/internal/bootstrap/sysNotify.go +++ b/internal/bootstrap/sysNotify.go @@ -16,6 +16,7 @@ var ( c chan os.Signal notifyTaskLock sync.Mutex notifyTaskQueue = pqueue.NewMaxPriorityQueue[*SysNotifyTask]() + once sync.Once WaitCbk func() ) @@ -34,7 +35,9 @@ func NewSysNotifyTask(name string, task func() error) *SysNotifyTask { func InitSysNotify() { c = make(chan os.Signal, 1) signal.Notify(c, syscall.SIGHUP /*1*/, syscall.SIGINT /*2*/, syscall.SIGQUIT /*3*/, syscall.SIGTERM /*15*/) - WaitCbk = sync.OnceFunc(waitCbk) + WaitCbk = func() { + once.Do(waitCbk) + } } func waitCbk() { diff --git a/server/handlers/rooms.go b/server/handlers/rooms.go index 670c3f5..cf68ce6 100644 --- a/server/handlers/rooms.go +++ b/server/handlers/rooms.go @@ -17,9 +17,12 @@ const ( var ( Rooms *rooms - initOnce = sync.OnceFunc(func() { - Rooms = newRooms() - }) + once = sync.Once{} + initOnce = func() { + once.Do(func() { + Rooms = newRooms() + }) + } ) var (