From 911b96cc7473a6b1b63b18d2acd913c80e452ec7 Mon Sep 17 00:00:00 2001 From: bjdgy Date: Mon, 29 Jan 2024 00:09:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/codeql-analysis.yml | 64 +++++++++---------- .../{go-update.yml-bak => go-update.yml} | 9 +-- .github/workflows/go.yml | 10 +-- .github/workflows/release-tag-version.yml | 31 +++++++-- .gitignore | 3 +- .goreleaser.yaml | 64 +++++++++++++++---- build.sh | 43 ++++++++++--- build_docker.sh | 2 +- server/base/app_ver.go | 10 ++- server/base/cmd.go | 49 +++++++++----- server/main.go | 10 ++- version | 1 + 12 files changed, 205 insertions(+), 91 deletions(-) rename .github/workflows/{go-update.yml-bak => go-update.yml} (94%) create mode 100644 version diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 01b5075..811c0f9 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -12,14 +12,14 @@ name: "CodeQL" on: - push: - branches: [ "main", "dev" ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ "main", "dev" ] + workflow_dispatch: - schedule: - - cron: '32 12 * * 5' +# push: +# branches: [ "main", "dev" ] +# pull_request: +# branches: [ "main", "dev" ] +# schedule: +# - cron: '32 12 * * 5' jobs: analyze: @@ -39,34 +39,34 @@ jobs: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - - name: Checkout repository - uses: actions/checkout@v4 + - name: Checkout repository + uses: actions/checkout@v4 - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v2 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v2 + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 - # ℹ️ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl - # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language - #- run: | - # make bootstrap - # make release + #- run: | + # make bootstrap + # make release - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/go-update.yml-bak b/.github/workflows/go-update.yml similarity index 94% rename from .github/workflows/go-update.yml-bak rename to .github/workflows/go-update.yml index 99e547b..08a7975 100644 --- a/.github/workflows/go-update.yml-bak +++ b/.github/workflows/go-update.yml @@ -1,8 +1,9 @@ name: go-update on: workflow_dispatch: - schedule: - - cron: "1 2 * * 5" +# schedule: +# - cron: "1 2 * * 5" + jobs: go: runs-on: ubuntu-latest @@ -26,7 +27,7 @@ jobs: go get -u go mod download - - name: Git push + - name: Git push run: | git init git config --local user.name "github-actions[bot]" @@ -38,7 +39,7 @@ jobs: git commit -m "update go.mod $(date +%Y.%m.%d.%H.%M)" #git push -f -u origin _autoaction git push -u origin _autoaction - + # 删除无用 workflow runs; - name: Delete workflow runs uses: GitRML/delete-workflow-runs@main diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9d24fe7..3333fe2 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -1,10 +1,12 @@ name: Go on: - push: - branches: [ "main", "dev" ] - pull_request: - branches: [ "main", "dev" ] + workflow_dispatch: + +# push: +# branches: [ "main", "dev" ] +# pull_request: +# branches: [ "main", "dev" ] jobs: diff --git a/.github/workflows/release-tag-version.yml b/.github/workflows/release-tag-version.yml index f57be15..b0d0dfa 100644 --- a/.github/workflows/release-tag-version.yml +++ b/.github/workflows/release-tag-version.yml @@ -1,6 +1,7 @@ name: release-tag-version on: + workflow_dispatch: push: tags: - "v0.*" @@ -9,20 +10,38 @@ jobs: Build: name: build-binary runs-on: ubuntu-latest + env: + TZ: Asia/Shanghai steps: + - name: Hello world + run: | + uname -a - uses: actions/checkout@v4 - - - run: git fetch --unshallow --quiet --tags --force - - uses: actions/setup-go@v4 with: go-version: '1.20' - go-version-file: 'server/go.mod' cache-dependency-path: 'server/go.sum' - uses: actions/setup-node@v4 with: node-version: '16' + cache: 'yarn' + cache-dependency-path: 'web/yarn.lock' - Docker: - name: build-docker \ No newline at end of file + - # 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: build + run: | + mkdir server/ui + touch server/ui/index.html + sudo apt-get install -y -q gcc-aarch64-linux-gnu g++-aarch64-linux-gnu + aarch64-linux-gnu-gcc -v + aarch64-linux-gnu-g++ -v + bash build.sh github_action + +# Docker: +# name: build-docker \ No newline at end of file diff --git a/.gitignore b/.gitignore index 345e432..1e5c7b0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ anylink-deploy anylink-deploy.tar.gz -dist -godist \ No newline at end of file +dist \ No newline at end of file diff --git a/.goreleaser.yaml b/.goreleaser.yaml index c0ddf35..f2de675 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,32 +1,61 @@ #https://goreleaser.com/static/schema.json +# release --skip=publish + # goreleaser build --skip=validate --clean --debug +# GOPROXY=https://goproxy.cn + +# docker run -it --rm -v $PWD:/app -v /go:/go -w /app --platform=linux/arm64 + +# docker run -it --rm -v $PWD:/app -v /go:/go -w /app goreleaser/goreleaser-cross build --skip=validate --clean --debug + +# docker run -it --rm -v $PWD:/app -v /go:/go -w /app bjdgyc/dcross goreleaser build --skip=validate --clean --debug + version: 1 -dist: godist +dist: dist before: hooks: - # You may remove this if you don't use go modules. - #- go mod tidy - # you may remove this if you don't need go generate - #- go generate ./... + - pwd +# - cmd: go mod tidy +# dir: +# "{{ dir .Dist}}" +# output: true +# - cmd: go generate +# dir: +# "{{ dir .Dist}}" +# output: true builds: - id: "build" #main: . dir: ./server + hooks: + pre: + - cmd: go mod tidy + dir: ./server + output: true + - cmd: go generate + dir: ./server + output: true + # {{- if eq .Arch "amd64" }}CC=x86_64-linux-gnu-gcc CXX=x86_64-linux-gnu-g++{{- end }} env: - CGO_ENABLED=1 - >- + {{- if eq .Os "linux" }} + {{- if eq .Arch "amd64" }}CC=x86_64-linux-musl-gcc{{- end }} + + {{- if eq .Arch "arm64" }}CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++{{- end }} + {{- end }} {{- if eq .Os "darwin" }} {{- if eq .Arch "amd64"}}CC=o64-clang{{- end }} - {{- if eq .Arch "arm64"}}CC=aarch64-apple-darwin20.2-clang{{- end }} + {{- if eq .Arch "arm64"}}CC=oa64-clang{{- end }} {{- end }} - {{- if eq .Os "linux" }} - {{- if eq .Arch "arm64" }}CC=aarch64-linux-gnu-gcc{{- end }} - {{- if eq .Arch "arm" }}CC=arm-linux-gnueabi-gcc{{- end }} + {{- if eq .Os "windows" }} + {{- if eq .Arch "amd64"}}CC=x86_64-w64-mingw32-gcc{{- end }} + {{- if eq .Arch "arm64"}}CC=aarch64-linux-gnu-gcc{{- end }} {{- end }} goos: - linux @@ -34,9 +63,22 @@ builds: #- windows goarch: - amd64 - - arm64 - - arm + #- arm64 + # https://go.dev/wiki/MinimumRequirements + goamd64: + - v1 + command: build + flags: + - -trimpath + - -tags osusergo,netgo,sqlite_omit_load_extension + ldflags: + # go tool link -help + # go tool compile -help + # -linkmode external + # -extld=$CC + # -fpic 作为动态链接库的时候 需要添加 + - -s -w -extldflags '-static' -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=dcross archives: - id: "archive1" diff --git a/build.sh b/build.sh index 05c9341..4321b21 100644 --- a/build.sh +++ b/build.sh @@ -1,5 +1,7 @@ #!/bin/bash +github_action=$1 + set -x function RETVAL() { rt=$1 @@ -12,32 +14,53 @@ function RETVAL() { #当前目录 cpath=$(pwd) -ver=`cat server/base/app_ver.go | grep APP_VER | awk '{print $3}' | sed 's/"//g'` +#ver=`cat server/base/app_ver.go | grep APP_VER | awk '{print $3}' | sed 's/"//g'` +ver=$(cat version) echo "当前版本 $ver" echo "编译前端项目" cd $cpath/web #国内可替换源加快速度 #npx browserslist@latest --update-db -#npm install --registry=https://registry.npm.taobao.org -#npm install -#npm run build +if [ "$github_action" == "github_action" ]; then + yarn install --registry=https://registry.npmmirror.com +else + yarn install +fi -yarn install --registry=https://registry.npmmirror.com yarn run build - RETVAL $? echo "编译二进制文件" cd $cpath/server rm -rf ui cp -rf $cpath/web/ui . -#国内可替换源加快速度 -export GOPROXY=https://goproxy.io + +flags="-v -trimpath -extldflags '-static' -tags osusergo,netgo,sqlite_omit_load_extension" +ldflags="-s -w -X main.appVer=$ver -X main.commitId=$(git rev-parse HEAD) -X main.date=$(date --iso-8601=seconds)" + +if [ "$github_action" == "github_action" ]; then + echo "github_action" +else + #国内可替换源加快速度 + export GOPROXY=https://goproxy.io + go mod tidy + go build -o anylink "$flags" -ldflags "$ldflags" + exit 0 +fi + +#github action go mod tidy -go build -v -o anylink -trimpath -ldflags "-s -w -X main.CommitId=$(git rev-parse HEAD)" -RETVAL $? +go build -o anylink_amd64 "$flags" -ldflags "$ldflags" + +#arm64交叉编译 +CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ go build -o anylink_arm64 "$flags" -ldflags "$ldflags" + +./anylink_amd64 -v +./anylink_arm64 -v + +exit 0 cd $cpath diff --git a/build_docker.sh b/build_docker.sh index ef8afac..34e6b2b 100644 --- a/build_docker.sh +++ b/build_docker.sh @@ -1,6 +1,6 @@ #!/bin/bash -ver=`cat server/base/app_ver.go | grep APP_VER | awk '{print $3}' | sed 's/"//g'` +ver=`cat server/base/app_ver.go | grep "APP_VER" | awk '{print $3}' | sed 's/"//g'` echo $ver #docker login -u bjdgyc diff --git a/server/base/app_ver.go b/server/base/app_ver.go index 2cfba09..6c1e8ad 100644 --- a/server/base/app_ver.go +++ b/server/base/app_ver.go @@ -2,6 +2,12 @@ package base const ( APP_NAME = "AnyLink" - // APP_VER app版本号 - APP_VER = "0.10.2" +) + +var ( + // APP_VER app版本号 + APP_VER = "0.0.1" + // 提交id + CommitId string + Date string ) diff --git a/server/base/cmd.go b/server/base/cmd.go index b50fc1a..a186cb2 100644 --- a/server/base/cmd.go +++ b/server/base/cmd.go @@ -1,7 +1,6 @@ package base import ( - "errors" "fmt" "io" "os" @@ -17,8 +16,6 @@ import ( ) var ( - // 提交id - CommitId string // pass明文 passwd string // 生成otp @@ -60,6 +57,15 @@ func execute() { envs[rr.Key().String()] = rr.Value().Index(0).String() } + //移动配置解析代码 + conf := linkViper.GetString("conf") + linkViper.SetConfigFile(conf) + err = linkViper.ReadInConfig() + if err != nil { + // 没有配置文件,直接报错 + panic("config file err:" + err.Error()) + } + if !runSrv { os.Exit(0) } @@ -106,19 +112,28 @@ func initCmd() { cobra.OnInitialize(func() { linkViper.AutomaticEnv() - conf := linkViper.GetString("conf") - _, err := os.Stat(conf) - if errors.Is(err, os.ErrNotExist) { - // 没有配置文件,不做处理 - panic(err) - } - - linkViper.SetConfigFile(conf) - err = linkViper.ReadInConfig() - if err != nil { - panic("config file err:" + err.Error()) - } + //ver := linkViper.GetBool("version") + //if ver { + // printVersion() + // os.Exit(0) + //} + // + //return + // + //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 { + // panic("config file err:" + err.Error()) + //} }) } @@ -164,6 +179,6 @@ func initToolCmd() *cobra.Command { } func printVersion() { - fmt.Printf("%s v%s build on %s [%s, %s] commit_id(%s) \n", - APP_NAME, APP_VER, runtime.Version(), runtime.GOOS, runtime.GOARCH, CommitId) + 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) } diff --git a/server/main.go b/server/main.go index 0337d4e..a4e73c1 100644 --- a/server/main.go +++ b/server/main.go @@ -20,11 +20,17 @@ import ( var uiData embed.FS // 程序版本 -var CommitId string +var ( + appVer string + commitId string + date string +) func main() { - base.CommitId = CommitId admin.UiData = uiData + base.APP_VER = appVer + base.CommitId = commitId + base.Date = date base.Start() handler.Start() diff --git a/version b/version new file mode 100644 index 0000000..56e9df1 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.10.3 \ No newline at end of file