修改 docker 编译

This commit is contained in:
bjdgyc
2024-01-29 17:47:46 +08:00
parent 4f77fd9013
commit 289f748543
9 changed files with 118 additions and 74 deletions

View File

@@ -14,33 +14,65 @@ jobs:
TZ: Asia/Shanghai TZ: Asia/Shanghai
steps: steps:
- name: Hello world - name: Hello world
run: | run: uname -a
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'
- uses: actions/checkout@v4
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
with: with:
node-version: '18' node-version: '18'
cache: 'yarn' cache: 'yarn'
cache-dependency-path: 'web/yarn.lock' 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 - # https://github.com/docker/setup-qemu-action
name: Set up QEMU name: Set up QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 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: | run: |
mkdir server/ui appVer=`cat version`
touch server/ui/index.html commitId=`git rev-parse HEAD`
#sudo apt-get install -y -q gcc-aarch64-linux-gnu g++-aarch64-linux-gnu buildDate=`date --iso-8601=seconds`
bash release.sh 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: # Docker:
# name: build-docker # name: build-docker

View File

@@ -1,18 +1,14 @@
#!/bin/bash #!/bin/bash
ver=`cat server/base/app_ver.go | grep "APP_VER" | awk '{print $3}' | sed 's/"//g'` ver=$(cat version)
echo $ver 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 docker tag bjdgyc/anylink:latest bjdgyc/anylink:$ver
exit 0
docker push bjdgyc/anylink:$ver
docker push bjdgyc/anylink:latest

View File

@@ -3,38 +3,32 @@
#debian:bullseye-slim #debian:bullseye-slim
#sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list #sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# 配合 github action 使用
# web # 需要先编译出ui文件后 再执行docker编译
FROM node:16-alpine3.18 as builder_node
WORKDIR /web
COPY ./web /web
RUN yarn install \
&& yarn run build \
&& ls /web/ui
# server # server
FROM golang:1.20-alpine3.18 as builder_golang FROM golang:1.20-alpine3.19 as builder_golang
#TODO 本地打包时使用镜像
ENV GOPROXY=https://goproxy.cn ARG CN="no"
ENV GOOS=linux ARG appVer="appVer"
ARG GitCommitId="gitCommitId" ARG commitId="commitId"
ARG buildDate="2020-1-1T11:11:11"
ENV TZ=Asia/Shanghai
WORKDIR /anylink WORKDIR /anylink
COPY docker/init.sh /tmp/init.sh
COPY server /anylink COPY server /anylink
COPY --from=builder_node /web/ui /anylink/ui COPY web/ui /anylink/ui
#TODO 本地打包时使用镜像 RUN sh /tmp/init.sh
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
# anylink # anylink
FROM alpine:3.18 FROM alpine:3.19
LABEL maintainer="github.com/bjdgyc" LABEL maintainer="github.com/bjdgyc"
ENV TZ=Asia/Shanghai
ENV ANYLINK_IN_CONTAINER=true ENV ANYLINK_IN_CONTAINER=true
WORKDIR /app WORKDIR /app
@@ -42,6 +36,7 @@ COPY --from=builder_golang /anylink/anylink /app/
COPY docker/docker_entrypoint.sh /app/ COPY docker/docker_entrypoint.sh /app/
COPY ./server/bridge-init.sh /app/ COPY ./server/bridge-init.sh /app/
COPY ./server/conf /app/conf COPY ./server/conf /app/conf
COPY ./README.md /app/README.md
COPY ./LICENSE /app/LICENSE COPY ./LICENSE /app/LICENSE
COPY ./home /app/home COPY ./home /app/home

19
docker/init.sh Normal file
View File

@@ -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

View File

@@ -22,11 +22,11 @@ echo "编译前端项目"
cd $cpath/web cd $cpath/web
#国内可替换源加快速度 #国内可替换源加快速度
#npx browserslist@latest --update-db #npx browserslist@latest --update-db
#yarn install yarn install
#yarn run build yarn run build
#RETVAL $? RETVAL $?
#rm -rf $cpath/server/ui rm -rf $cpath/server/ui
#cp -rf $cpath/web/ui . $cpath/server/ui cp -rf $cpath/web/ui . $cpath/server/ui
echo "编译二进制文件" echo "编译二进制文件"
cd $cpath/server cd $cpath/server
@@ -39,14 +39,16 @@ ldflags="-s -w -extldflags '-static' -X main.appVer=$ver -X main.commitId=$(git
gopath=$(go env GOPATH) gopath=$(go env GOPATH)
#go mod tidy #go mod tidy
# alpine3
apk add gcc musl-dev
#使用 musl-dev 编译 #使用 musl-dev 编译
docker run -q --rm -v $PWD:/app -v $gopath:/go -w /app --platform=linux/amd64 \ 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" 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 \ 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" golang:1.20-alpine3.19 go build -o anylink_arm64 $flags -ldflags "$ldflags"
./anylink_amd64 -v
./anylink_arm64 -v ./anylink_arm64 -v
exit 0 exit 0

View File

@@ -8,6 +8,6 @@ var (
// APP_VER app版本号 // APP_VER app版本号
APP_VER = "0.0.1" APP_VER = "0.0.1"
// 提交id // 提交id
CommitId string CommitId string
Date string BuildDate string
) )

View File

@@ -57,7 +57,7 @@ func execute() {
envs[rr.Key().String()] = rr.Value().Index(0).String() envs[rr.Key().String()] = rr.Value().Index(0).String()
} }
//移动配置解析代码 // 移动配置解析代码
conf := linkViper.GetString("conf") conf := linkViper.GetString("conf")
linkViper.SetConfigFile(conf) linkViper.SetConfigFile(conf)
err = linkViper.ReadInConfig() err = linkViper.ReadInConfig()
@@ -113,27 +113,27 @@ func initCmd() {
cobra.OnInitialize(func() { cobra.OnInitialize(func() {
linkViper.AutomaticEnv() linkViper.AutomaticEnv()
//ver := linkViper.GetBool("version") // ver := linkViper.GetBool("version")
//if ver { // if ver {
// printVersion() // printVersion()
// os.Exit(0) // os.Exit(0)
//} // }
// //
//return // return
// //
//conf := linkViper.GetString("conf") // conf := linkViper.GetString("conf")
//_, err := os.Stat(conf) // _, err := os.Stat(conf)
//if errors.Is(err, os.ErrNotExist) { // if errors.Is(err, os.ErrNotExist) {
// // 没有配置文件,不做处理 // // 没有配置文件,不做处理
// panic("conf stat err:" + err.Error()) // panic("conf stat err:" + err.Error())
//} // }
// //
// //
//linkViper.SetConfigFile(conf) // linkViper.SetConfigFile(conf)
//err = linkViper.ReadInConfig() // err = linkViper.ReadInConfig()
//if err != nil { // if err != nil {
// panic("config file err:" + err.Error()) // panic("config file err:" + err.Error())
//} // }
}) })
} }
@@ -179,6 +179,6 @@ func initToolCmd() *cobra.Command {
} }
func printVersion() { func printVersion() {
fmt.Printf("%s v%s build on %s [%s, %s] %s commit_id(%s)\n", 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, Date, CommitId) APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, BuildDate, CommitId)
} }

View File

@@ -21,16 +21,16 @@ var uiData embed.FS
// 程序版本 // 程序版本
var ( var (
appVer string appVer string
commitId string commitId string
date string buildDate string
) )
func main() { func main() {
admin.UiData = uiData admin.UiData = uiData
base.APP_VER = appVer base.APP_VER = appVer
base.CommitId = commitId base.CommitId = commitId
base.Date = date base.BuildDate = buildDate
base.Start() base.Start()
handler.Start() handler.Start()

View File

@@ -1 +1 @@
0.10.3 0.10.3-test