From 289f74854342464e0be11393108d6579182f1506 Mon Sep 17 00:00:00 2001 From: bjdgyc Date: Mon, 29 Jan 2024 17:47:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20docker=20=E7=BC=96?= =?UTF-8?q?=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/release.yml | 58 +++++++++++++++++++++++++++-------- build_docker.sh | 16 ++++------ docker/Dockerfile | 35 +++++++++------------ docker/init.sh | 19 ++++++++++++ release.sh | 18 ++++++----- server/base/app_ver.go | 4 +-- server/base/cmd.go | 30 +++++++++--------- server/main.go | 8 ++--- version | 2 +- 9 files changed, 117 insertions(+), 73 deletions(-) create mode 100644 docker/init.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a07a76ed..99e0a05c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,33 +14,65 @@ jobs: TZ: Asia/Shanghai steps: - name: Hello world - run: | - cd server && go mod tidy - uname -a - - uses: actions/checkout@v4 - - uses: actions/setup-go@v4 - with: - go-version: '1.20' - cache-dependency-path: 'server/go.sum' + run: uname -a + - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '18' cache: 'yarn' cache-dependency-path: 'web/yarn.lock' + - name: Build web + working-directory: web + run: | + yarn install + yarn run build + - uses: actions/setup-go@v4 + with: + go-version: '1.20' + cache-dependency-path: 'server/go.sum' - # https://github.com/docker/setup-qemu-action name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Docker Build + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: bjdgyc + password: ${{ secrets.DOCKERHUB_TOKEN }} + logout: true + + - name: pre bash + shell: bash run: | - mkdir server/ui - touch server/ui/index.html - #sudo apt-get install -y -q gcc-aarch64-linux-gnu g++-aarch64-linux-gnu - bash release.sh + appVer=`cat version` + commitId=`git rev-parse HEAD` + buildDate=`date --iso-8601=seconds` + echo "APP_VER=$appVer" >> $GITHUB_ENV + echo "commitId=$commitId" >> $GITHUB_ENV + echo "buildDate=$buildDate" >> $GITHUB_ENV + + cd server;go mod tidy + + - name: Build and push + uses: docker/build-push-action@v5 + with: + push: false + context: . + file: ./docker/Dockerfile + platforms: linux/amd64,linux/arm64 + build-args: appVer=$APP_VER commitId=$commitId buildDate=$buildDate + #tags: bjdgyc/anylink:latest,bjdgyc/anylink:${{ env.APP_VER }} + tags: bjdgyc/anylink:${{ env.APP_VER }} + +# - name: Docker Build binary +# shell: bash +# run: | +# #sudo apt-get install -y -q gcc-aarch64-linux-gnu g++-aarch64-linux-gnu +# bash release.sh # Docker: # name: build-docker diff --git a/build_docker.sh b/build_docker.sh index 34e6b2ba..1d773661 100644 --- a/build_docker.sh +++ b/build_docker.sh @@ -1,18 +1,14 @@ #!/bin/bash -ver=`cat server/base/app_ver.go | grep "APP_VER" | awk '{print $3}' | sed 's/"//g'` +ver=$(cat version) echo $ver -#docker login -u bjdgyc +# docker login -u bjdgyc -#docker build -t bjdgyc/anylink . +# docker build -t bjdgyc/anylink -f docker/Dockerfile . -docker build -t bjdgyc/anylink --build-arg GitCommitId=$(git rev-parse HEAD) -f docker/Dockerfile . +docker build -t bjdgyc/anylink --progress=plain --build-arg CN="yes" --build-arg appVer=$ver \ + --build-arg commitId=$(git rev-parse HEAD) --build-arg buildDate=$(date --iso-8601=seconds) \ + -f docker/Dockerfile . docker tag bjdgyc/anylink:latest bjdgyc/anylink:$ver - -exit 0 - -docker push bjdgyc/anylink:$ver -docker push bjdgyc/anylink:latest - diff --git a/docker/Dockerfile b/docker/Dockerfile index 73aadcd1..a0ada9a0 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -3,38 +3,32 @@ #debian:bullseye-slim #sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list +# 配合 github action 使用 +# 需要先编译出ui文件后 再执行docker编译 -# web -FROM node:16-alpine3.18 as builder_node -WORKDIR /web -COPY ./web /web -RUN yarn install \ - && yarn run build \ - && ls /web/ui +# server +FROM golang:1.20-alpine3.19 as builder_golang +ARG CN="no" +ARG appVer="appVer" +ARG commitId="commitId" +ARG buildDate="2020-1-1T11:11:11" -# server -FROM golang:1.20-alpine3.18 as builder_golang -#TODO 本地打包时使用镜像 -ENV GOPROXY=https://goproxy.cn -ENV GOOS=linux -ARG GitCommitId="gitCommitId" +ENV TZ=Asia/Shanghai WORKDIR /anylink +COPY docker/init.sh /tmp/init.sh COPY server /anylink -COPY --from=builder_node /web/ui /anylink/ui +COPY web/ui /anylink/ui -#TODO 本地打包时使用镜像 -RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories -RUN apk add gcc musl-dev -RUN cd /anylink;go mod tidy;go build -o anylink -trimpath -ldflags "-s -w -X main.CommitId=${GitCommitId}" \ - && /anylink/anylink tool -v +RUN sh /tmp/init.sh # anylink -FROM alpine:3.18 +FROM alpine:3.19 LABEL maintainer="github.com/bjdgyc" +ENV TZ=Asia/Shanghai ENV ANYLINK_IN_CONTAINER=true WORKDIR /app @@ -42,6 +36,7 @@ COPY --from=builder_golang /anylink/anylink /app/ COPY docker/docker_entrypoint.sh /app/ COPY ./server/bridge-init.sh /app/ COPY ./server/conf /app/conf +COPY ./README.md /app/README.md COPY ./LICENSE /app/LICENSE COPY ./home /app/home diff --git a/docker/init.sh b/docker/init.sh new file mode 100644 index 00000000..62193915 --- /dev/null +++ b/docker/init.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +set -x + +#TODO 本地打包时使用镜像 +if [[ $CN == "yes" ]]; then + sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories + export GOPROXY=https://goproxy.cn +fi + +#apk add gcc musl-dev + +cd /anylink + +go mod tidy +go build -o anylink -trimpath \ + -ldflags "-s -w -extldflags '-static' -X main.appVer=$appVer -X main.commitId=$commitId -X main.buildDate=$buildDate" + +/anylink/anylink -v diff --git a/release.sh b/release.sh index 910a7b18..30821034 100644 --- a/release.sh +++ b/release.sh @@ -22,11 +22,11 @@ echo "编译前端项目" cd $cpath/web #国内可替换源加快速度 #npx browserslist@latest --update-db -#yarn install -#yarn run build -#RETVAL $? -#rm -rf $cpath/server/ui -#cp -rf $cpath/web/ui . $cpath/server/ui +yarn install +yarn run build +RETVAL $? +rm -rf $cpath/server/ui +cp -rf $cpath/web/ui . $cpath/server/ui echo "编译二进制文件" cd $cpath/server @@ -39,14 +39,16 @@ ldflags="-s -w -extldflags '-static' -X main.appVer=$ver -X main.commitId=$(git gopath=$(go env GOPATH) #go mod tidy +# alpine3 +apk add gcc musl-dev + #使用 musl-dev 编译 docker run -q --rm -v $PWD:/app -v $gopath:/go -w /app --platform=linux/amd64 \ golang:1.20-alpine3.19 go build -o anylink_amd64 $flags -ldflags "$ldflags" -#arm64交叉编译 +./anylink_amd64 -v +#arm64编译 docker run -q --rm -v $PWD:/app -v $gopath:/go -w /app --platform=linux/arm64 \ golang:1.20-alpine3.19 go build -o anylink_arm64 $flags -ldflags "$ldflags" - -./anylink_amd64 -v ./anylink_arm64 -v exit 0 diff --git a/server/base/app_ver.go b/server/base/app_ver.go index 6c1e8ad5..60196e26 100644 --- a/server/base/app_ver.go +++ b/server/base/app_ver.go @@ -8,6 +8,6 @@ var ( // APP_VER app版本号 APP_VER = "0.0.1" // 提交id - CommitId string - Date string + CommitId string + BuildDate string ) diff --git a/server/base/cmd.go b/server/base/cmd.go index a186cb2c..3b7db054 100644 --- a/server/base/cmd.go +++ b/server/base/cmd.go @@ -57,7 +57,7 @@ func execute() { envs[rr.Key().String()] = rr.Value().Index(0).String() } - //移动配置解析代码 + // 移动配置解析代码 conf := linkViper.GetString("conf") linkViper.SetConfigFile(conf) err = linkViper.ReadInConfig() @@ -113,27 +113,27 @@ func initCmd() { cobra.OnInitialize(func() { linkViper.AutomaticEnv() - //ver := linkViper.GetBool("version") - //if ver { + // ver := linkViper.GetBool("version") + // if ver { // printVersion() // os.Exit(0) - //} + // } // - //return + // return // - //conf := linkViper.GetString("conf") - //_, err := os.Stat(conf) - //if errors.Is(err, os.ErrNotExist) { + // conf := linkViper.GetString("conf") + // _, err := os.Stat(conf) + // if errors.Is(err, os.ErrNotExist) { // // 没有配置文件,不做处理 // panic("conf stat err:" + err.Error()) - //} + // } // // - //linkViper.SetConfigFile(conf) - //err = linkViper.ReadInConfig() - //if err != nil { + // linkViper.SetConfigFile(conf) + // err = linkViper.ReadInConfig() + // if err != nil { // panic("config file err:" + err.Error()) - //} + // } }) } @@ -179,6 +179,6 @@ func initToolCmd() *cobra.Command { } func printVersion() { - fmt.Printf("%s v%s build on %s [%s, %s] %s commit_id(%s)\n", - APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, Date, CommitId) + fmt.Printf("%s v%s build on %s [%s, %s] BuildDate:%s commit_id(%s)\n", + APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, BuildDate, CommitId) } diff --git a/server/main.go b/server/main.go index a4e73c1b..467aafcd 100644 --- a/server/main.go +++ b/server/main.go @@ -21,16 +21,16 @@ var uiData embed.FS // 程序版本 var ( - appVer string - commitId string - date string + appVer string + commitId string + buildDate string ) func main() { admin.UiData = uiData base.APP_VER = appVer base.CommitId = commitId - base.Date = date + base.BuildDate = buildDate base.Start() handler.Start() diff --git a/version b/version index 56e9df10..6dadeb2b 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.10.3 \ No newline at end of file +0.10.3-test \ No newline at end of file