修改 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
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

View File

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

View File

@ -3,38 +3,32 @@
#debian:bullseye-slim
#sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# web
FROM node:16-alpine3.18 as builder_node
WORKDIR /web
COPY ./web /web
RUN yarn install \
&& yarn run build \
&& ls /web/ui
# 配合 github action 使用
# 需要先编译出ui文件后 再执行docker编译
# server
FROM golang:1.20-alpine3.18 as builder_golang
#TODO 本地打包时使用镜像
ENV GOPROXY=https://goproxy.cn
ENV GOOS=linux
ARG GitCommitId="gitCommitId"
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"
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

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
#国内可替换源加快速度
#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

View File

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

View File

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

View File

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

View File

@ -1 +1 @@
0.10.3
0.10.3-test