From fec98e7f695b85f2feba4d4c69300449c7b58b8b Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 29 Aug 2022 22:44:55 +0800 Subject: [PATCH] ci: auto build dev version --- .github/workflows/build.yml | 40 +++++++++++++++++++++++++++ build.sh | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 build.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..6b878997 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,40 @@ +name: build + +on: + push: + branches: [ '**' ] + pull_request: + branches: [ '**' ] + +jobs: + build: + strategy: + matrix: + platform: [ubuntu-latest] + go-version: [1.18] + name: Build + runs-on: ${{ matrix.platform }} + steps: + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + + - name: Checkout + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + docker pull techknowlogick/xgo:latest + go install src.techknowlogick.com/xgo@latest + sudo apt install upx + + - name: Build + run: | + bash build.sh dev + + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: alist + path: dist \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 00000000..1b11a34f --- /dev/null +++ b/build.sh @@ -0,0 +1,54 @@ +appName="alist" +builtAt="$(date +'%F %T %z')" +goVersion=$(go version | sed 's/go version //') +gitAuthor=$(git show -s --format='format:%aN <%ae>' HEAD) +gitCommit=$(git log --pretty=format:"%h" -1) + +if [ "$1" = "release" ]; then + version=$(git describe --long --tags --dirty --always) + webVersion=$(wget -qO- -t1 -T2 "https://api.github.com/repos/alist-org/alist-web/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g') +else + version="dev" + webVersion="dev" +fi + +echo "build version: $gitTag" + +ldflags="\ +-w -s \ +-X 'github.com/alist-org/alist/v3/internal/conf.BuiltAt=$builtAt' \ +-X 'github.com/alist-org/alist/v3/internal/conf.GoVersion=$goVersion' \ +-X 'github.com/alist-org/alist/v3/internal/conf.GitAuthor=$gitAuthor' \ +-X 'github.com/alist-org/alist/v3/internal/conf.GitCommit=$gitCommit' \ +-X 'github.com/alist-org/alist/v3/internal/conf.Version=$version' \ +-X 'github.com/alist-org/alist/v3/internal/conf.WebVersion=$webVersion' \ +" + +FetchWebBuild() { + curl -L https://codeload.github.com/alist-org/web-dist/tar.gz/refs/heads/main -o web-dist-main.tar.gz + tar -zxvf web-dist-main.tar.gz + mv web-dist-main/dist public + rm -rf web-dist-main web-dist-main.tar.gz +} + +BuildDev() { + rm -rf .git/ + xgo -targets=linux/amd64,windows/amd64,darwin/amd64 -out "$appName" -ldflags="$ldflags" -tags=jsoniter . + mkdir -p "dist" + mv alist-* dist + cd dist + upx -9 ./alist-linux* + upx -9 ./alist-windows* + find . -type f -print0 | xargs -0 md5sum >md5.txt + cat md5.txt + cd .. || exit +} + +if [ "$1" = "dev" ]; then + FetchWebBuild + BuildDev +elif [ "$1" = "release" ]; then + echo -e "To be implement" +else + echo -e "Parameter error" +fi \ No newline at end of file