From 0745cecee0232990eb6ed05be7ddc0c8914c4015 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Wed, 4 Oct 2023 22:51:37 +0800 Subject: [PATCH] Feat: build wordflow and auto dev --- .github/workflows/build.yml | 22 +--- .github/workflows/releases.yml | 39 +++++++ .gitignore | 3 +- build.sh | 179 +++++++++++++++++++++++++++++++++ cmd/root.go | 3 +- cmd/version.go | 9 +- internal/conf/var.go | 6 ++ 7 files changed, 239 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/releases.yml create mode 100644 build.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2e94d33..a4e7348 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,28 +23,12 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Install dependencies - run: | - echo "$(pwd)" - echo "$(ls -ahl)" - - name: Build - uses: crazy-max/ghaction-xgo@v3 - with: - xgo_version: latest - go_version: ${{ matrix.go-version }} - dest: build - targets: windows/amd64,linux/amd64,linux/arm64,darwin/amd64,darwin/arm64 - v: false - x: false - race: false - tags: jsoniter - ldflags: -s -w --extldflags "-static -fpic -Wl,-z,relro,-z,now" - buildmode: pie - trimpath: true + run: | + bash build.sh -v dev -P -p "windows/amd64,linux/amd64,linux/arm64,darwin/amd64,darwin/arm64" - name: Upload artifact uses: actions/upload-artifact@v3 with: name: synctv - path: build/github.com/synctv-org + path: build diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml new file mode 100644 index 0000000..8a6ac8f --- /dev/null +++ b/.github/workflows/releases.yml @@ -0,0 +1,39 @@ +name: build + +on: + push: + tags: + - 'v*' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + strategy: + matrix: + platform: [ubuntu-latest] + go-version: [ '1.21' ] + name: Build + runs-on: ${{ matrix.platform }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Get version + id: get_version + run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\/v/} + + - name: Build + run: | + bash build.sh -v "v${{ steps.get_version.outputs.VERSION }}" -P -p "windows/amd64,linux/amd64,linux/arm64,darwin/amd64,darwin/arm64" + + - uses: "marvinpinto/action-automatic-releases@latest" + with: + repo_token: "${{ secrets.GITHUB_TOKEN }}" + automatic_release_tag: "v${{ steps.get_version.outputs.VERSION }}" + title: "Version ${{ steps.get_version.outputs.VERSION }}" + prerelease: false + files: | + build/* diff --git a/.gitignore b/.gitignore index 2020af0..f543676 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /log /public/dist/* !*.gitkeep -.DS_Store \ No newline at end of file +.DS_Store +/build diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..7427b8d --- /dev/null +++ b/build.sh @@ -0,0 +1,179 @@ +#!/bin/bash + +function ChToScriptFileDir() { + cd "$(dirname "$0")" + if [ $? -ne 0 ]; then + echo "cd to script file dir error" + exit 1 + fi +} + +function Help() { + echo "-h get help" + echo "-v set build version (default: dev)" + echo "-w set web version (default: latest releases)" + echo "-m set build mode (default: pie)" + echo "-l set ldflags (default: -s -w --extldflags \"-static -fpic -Wl,-z,relro,-z,now\")" + echo "-p set platform (default: linux/amd64,darwin/arm64)" + echo "-P set trim path (default: disable)" + echo "-b set build result dir (default: build)" +} + +function Init() { + VERSION="dev" + WEB_VERSION="" + commit="$(git log --pretty=format:"%h" -1)" + if [ $? -ne 0 ]; then + echo "git log error" + GIT_COMMIT="unknown" + else + GIT_COMMIT="$commit" + fi + BUILD_MODE="pie" + LDFLAGS='-s -w --extldflags "-static -fpic -Wl,-z,relro,-z,now"' + PLATFORM="linux/amd64,darwin/arm64" + TRIM_PATH="" + BUILD_DIR="build" +} + +function ParseArgs() { + while getopts "hv:w:m:l:p:Pb:" arg; do + case $arg in + h) + Help + exit 0 + ;; + v) + VERSION="$OPTARG" + ;; + w) + WEB_VERSION="$OPTARG" + ;; + m) + BUILD_MODE="$OPTARG" + ;; + l) + LDFLAGS="$OPTARG" + ;; + p) + PLATFORM="$OPTARG" + ;; + P) + TRIM_PATH="true" + ;; + b) + BUILD_DIR="$OPTARG" + ;; + ?) + echo "unkonw argument" + exit 1 + ;; + esac + done +} + +function GetLatestWebVersion() { + while true; do + LATEST=$(curl -sL https://api.github.com/repos/$1/releases/latest) + if [ $? -ne 0 ]; then exit $?; fi + if [ "$(echo "$LATEST" | grep -o "API rate limit exceeded")" ]; then + echo "API rate limit exceeded" + echo "sleep 5s" + sleep 5 + elif [ "$(echo "$LATEST" | grep -o "Not Found")" ]; then + echo "Not Found" + exit 1 + else + break + fi + done + + WEB_VERSION=$(echo "$LATEST" | grep -o '"tag_name": "[^"]*' | grep -o '[^"]*$') +} + +# Comply with golang version rules +function CheckVersionFormat() { + if [ "$1" == "dev" ]; then + return 0 + fi + if [ "$(echo "$1" | grep -oE "^v?[0-9]+\.[0-9]+\.[0-9]+$")" ]; then + return 0 + fi + return 1 +} + +function FixArgs() { + CheckAllPlatform + CheckVersionFormat "$VERSION" + if [ $? -ne 0 ]; then + echo "version format error" + exit 1 + fi + if [ ! "$WEB_VERSION" ]; then + GetLatestWebVersion "synctv-org/synctv-web" + fi + LDFLAGS="$LDFLAGS \ + -X 'github.com/synctv-org/synctv/internal/conf.Version=$VERSION' \ + -X 'github.com/synctv-org/synctv/internal/conf.WebVersion=$WEB_VERSION' \ + -X 'github.com/synctv-org/synctv/internal/conf.GitCommit=$GIT_COMMIT'" + + BUILD_DIR="$(echo "$BUILD_DIR" | sed 's#/$##')" +} + +function InitDep() { + rm -rf public/dist/* + echo "download: https://github.com/synctv-org/synctv-web/releases/download/${WEB_VERSION}/dist.tar.gz" + curl -sL "https://github.com/synctv-org/synctv-web/releases/download/${WEB_VERSION}/dist.tar.gz" | tar --strip-components 1 -C "public/dist" -z -x -v -f - +} + +ALLOWD_PLATFORM="linux/amd64,linux/arm64,darwin/amd64,darwin/arm64,windows/amd64,windows/arm64" + +function CheckPlatform() { + platform="$1" + for p in $(echo "$ALLOWD_PLATFORM" | tr "," "\n"); do + if [ "$p" == "$platform" ]; then + return 0 + fi + done + return 1 +} + +function CheckAllPlatform() { + for platform in $(echo "$PLATFORM" | tr "," "\n"); do + CheckPlatform "$platform" + if [ $? -ne 0 ]; then + echo "platform $platform not allowd" + exit 1 + fi + done +} + +function Build() { + platform="$1" + echo "build $platform" + GOOS=${platform%/*} + GOARCH=${platform#*/} + if [ "$GOOS" == "windows" ]; then + EXT=".exe" + else + EXT="" + fi + if [ "$TRIM_PATH" ]; then + GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" . + else + GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" . + fi +} + +function BuildAll() { + for platform in $(echo "$PLATFORM" | tr "," "\n"); do + Build "$platform" + done +} + +ChToScriptFileDir +Init +ParseArgs "$@" +FixArgs +InitDep +BuildAll diff --git a/cmd/root.go b/cmd/root.go index ff62a5f..0d4aa6d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "github.com/synctv-org/synctv/cmd/flags" + "github.com/synctv-org/synctv/internal/conf" ) var RootCmd = &cobra.Command{ @@ -22,7 +23,7 @@ func Execute() { } func init() { - RootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode") + RootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", conf.Version == "dev", "start with dev mode") RootCmd.PersistentFlags().BoolVar(&flags.LogStd, "log-std", true, "log to std") RootCmd.PersistentFlags().BoolVar(&flags.EnvNoPrefix, "env-no-prefix", false, "env no SYNCTV_ prefix") RootCmd.PersistentFlags().BoolVar(&flags.SkipConfig, "skip-config", false, "skip config") diff --git a/cmd/version.go b/cmd/version.go index bc4f87d..1e41235 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -2,8 +2,10 @@ package cmd import ( "fmt" + "runtime" "github.com/spf13/cobra" + "github.com/synctv-org/synctv/internal/conf" ) var VersionCmd = &cobra.Command{ @@ -11,7 +13,12 @@ var VersionCmd = &cobra.Command{ Short: "Print the version number of Sync TV Server", Long: `All software has versions. This is Sync TV Server's`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("synctv-server v0.1 -- HEAD") + fmt.Printf("Sync TV Server version %s\n", conf.Version) + fmt.Printf("Sync TV Web version %s\n", conf.WebVersion) + fmt.Printf("Git commit %s\n", conf.GitCommit) + fmt.Printf("Go version %s\n", runtime.Version()) + fmt.Printf("Built with %s\n", runtime.Compiler) + fmt.Printf("OS/Arch %s/%s\n", runtime.GOOS, runtime.GOARCH) }, } diff --git a/internal/conf/var.go b/internal/conf/var.go index ec6d985..2f43ca4 100644 --- a/internal/conf/var.go +++ b/internal/conf/var.go @@ -3,3 +3,9 @@ package conf var ( Conf *Config ) + +var ( + Version string = "dev" + WebVersion string = "dev" + GitCommit string +)