Skip to content

Commit

Permalink
修改 docker 编译
Browse files Browse the repository at this point in the history
  • Loading branch information
bjdgyc committed Jan 29, 2024
1 parent 4f77fd9 commit 289f748
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 73 deletions.
58 changes: 45 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 6 additions & 10 deletions build_docker.sh
Original file line number Diff line number Diff line change
@@ -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

35 changes: 15 additions & 20 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,40 @@
#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
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

Expand Down
19 changes: 19 additions & 0 deletions docker/init.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions server/base/app_ver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ var (
// APP_VER app版本号
APP_VER = "0.0.1"
// 提交id
CommitId string
Date string
CommitId string
BuildDate string
)
30 changes: 15 additions & 15 deletions server/base/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
//}
// }
})
}

Expand Down Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.3
0.10.3-test

0 comments on commit 289f748

Please sign in to comment.