From 3b53f5bca32aea81816dbe76803c25dd254e6a6f Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 15 Jan 2023 15:04:27 +0800 Subject: [PATCH] chore: better workflow --- .github/release-cgo-android.sh | 17 +++ .github/release-cgo.sh | 18 +++ release.sh => .github/rename-cgo.sh | 12 +- .github/workflows/prerelease.yml | 174 +++++++++++++++++++++++----- Makefile | 16 +-- 5 files changed, 187 insertions(+), 50 deletions(-) create mode 100644 .github/release-cgo-android.sh create mode 100644 .github/release-cgo.sh rename release.sh => .github/rename-cgo.sh (69%) diff --git a/.github/release-cgo-android.sh b/.github/release-cgo-android.sh new file mode 100644 index 00000000..447efd6e --- /dev/null +++ b/.github/release-cgo-android.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +BRANCH=$(git branch --show-current) +if [ "$BRANCH" = "Alpha" ];then + VERSION=alpha-$(git rev-parse --short HEAD) +elif [ "$BRANCH" = "Beta" ]; then + VERSION=beta-$(git rev-parse --short HEAD) +elif [ "$BRANCH" = "" ]; then + VERSION=$(git describe --tags) +else + VERSION=$(git rev-parse --short HEAD) +fi + +CC=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang +Ldflags="-X 'github.com/Dreamacro/clash/constant.Version=${VERSION}' -X 'github.com/Dreamacro/clash/constant.BuildTime=${BUILDTIME}' -w -s -buildid=" + +CGO_ENABLED=1 go build -tags with_gvisor,with_lwip -trimpath -ldflags "${Ldflags}" -o bin/clash.meta-android-arm64 \ No newline at end of file diff --git a/.github/release-cgo.sh b/.github/release-cgo.sh new file mode 100644 index 00000000..6ceff700 --- /dev/null +++ b/.github/release-cgo.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +BRANCH=$(git branch --show-current) +if [ "$BRANCH" = "Alpha" ];then + VERSION=alpha-$(git rev-parse --short HEAD) +elif [ "$BRANCH" = "Beta" ]; then +VERSION=beta-$(git rev-parse --short HEAD) +elif [ "$BRANCH" = "" ]; then +VERSION=$(git describe --tags) +else +VERSION=$(git rev-parse --short HEAD) +fi + +xgoTarget=windows/*,linux/*,darwin-10.16/* +xgoTags=with_gvisor,with_lwip +Ldflags="-X 'github.com/Dreamacro/clash/constant.Version=${VERSION}' -X 'github.com/Dreamacro/clash/constant.BuildTime=${BUILDTIME}' -w -s -buildid=" + +xgo --branch ${BRANCH} --out=${BINDIR}/${NAME} --targets="${xgoTarget}" --tags="${xgoTags}" -ldflags="${Ldflags}" github.com/${REPO} \ No newline at end of file diff --git a/release.sh b/.github/rename-cgo.sh similarity index 69% rename from release.sh rename to .github/rename-cgo.sh index 6422207f..283f485e 100644 --- a/release.sh +++ b/.github/rename-cgo.sh @@ -5,22 +5,22 @@ for FILENAME in $FILENAMES do if [[ $FILENAME =~ "darwin-10.16-arm64" ]];then echo "rename darwin-10.16-arm64 $FILENAME" - mv $FILENAME clash.meta-darwin-arm64-${VERSION}-${ShortSHA} + mv $FILENAME clash.meta-darwin-arm64-cgo-${VERSION}-${ShortSHA} elif [[ $FILENAME =~ "darwin-10.16-amd64" ]];then echo "rename darwin-10.16-amd64 $FILENAME" - mv $FILENAME clash.meta-darwin-amd64-${VERSION}-${ShortSHA} + mv $FILENAME clash.meta-darwin-amd64-cgo-${VERSION}-${ShortSHA} elif [[ $FILENAME =~ "windows-4.0-386" ]];then echo "rename windows 386 $FILENAME" - mv $FILENAME clash.meta-windows-386-${VERSION}-${ShortSHA}.exe + mv $FILENAME clash.meta-windows-386-cgo-${VERSION}-${ShortSHA}.exe elif [[ $FILENAME =~ "windows-4.0-amd64" ]];then echo "rename windows amd64 $FILENAME" - mv $FILENAME clash.meta-windows-amd64-${VERSION}-${ShortSHA}.exe + mv $FILENAME clash.meta-windows-amd64-cgo-${VERSION}-${ShortSHA}.exe elif [[ $FILENAME =~ "linux" ]];then echo "rename linux $FILENAME" - mv $FILENAME $FILENAME-${VERSION}-${ShortSHA} + mv $FILENAME $FILENAME-cgo-${VERSION}-${ShortSHA} elif [[ $FILENAME =~ "android" ]];then echo "rename android $FILENAME" - mv $FILENAME $FILENAME-${VERSION}-${ShortSHA} + mv $FILENAME $FILENAME-cgo-${VERSION}-${ShortSHA} else echo "skip $FILENAME" fi done diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml index b5cbb781..d7357cb6 100644 --- a/.github/workflows/prerelease.yml +++ b/.github/workflows/prerelease.yml @@ -10,7 +10,49 @@ on: - Alpha - Beta jobs: - Build-Prerelease: + Build-Prerelease-WithoutCGO: + runs-on: ubuntu-latest + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: '1.19' + check-latest: true + + - name: Cache go module + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-WithoutCGO-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-WithoutCGO- + ${{ runner.os }}-go- + + - name: Test + if: ${{github.ref_name=='Beta'}} + run: | + go test ./... + + - name: Build + if: success() + env: + NAME: Clash.Meta + BINDIR: bin + run: make -j$(($(nproc) + 1)) releases + + - name: Delete current release assets + uses: andreaswilli/delete-release-assets-action@v2.0.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + tag: Prerelease-${{ github.ref_name }} + deleteOnlyFromDrafts: false + + Build-Prerelease-WithCGO: permissions: write-all runs-on: ubuntu-22.04 @@ -19,13 +61,13 @@ jobs: uses: actions/checkout@v3 - name: Set variables - if: ${{ github.ref_name }} == Alpha - run: echo "VERSION=master" >> $GITHUB_ENV + if: ${{github.ref_name=='Alpha'}} + run: echo "VERSION=alpha" >> $GITHUB_ENV shell: bash - name: Set variables - if: ${{ github.ref_name }} == Beta - run: echo "VERSION=alpha" >> $GITHUB_ENV + if: ${{github.ref_name=='Beta'}} + run: echo "VERSION=beta" >> $GITHUB_ENV shell: bash - name: Set ENV @@ -33,8 +75,6 @@ jobs: echo "NAME=clash.meta" >> $GITHUB_ENV echo "BUILD_TIME=$(date +%Y%m%d%H%M)" >> $GITHUB_ENV echo "REPO=${{ github.repository }}" >> $GITHUB_ENV - echo "TARGET=windows/*,linux/*,darwin-10.16/*" >> $GITHUB_ENV - echo "TAGS=with_gvisor,with_lwip" >> $GITHUB_ENV echo "ShortSHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV echo "$ShortSHA" echo "$REPO" @@ -45,18 +85,6 @@ jobs: with: go-version: '1.19' check-latest: true - cache: true - - - uses: nttld/setup-ndk@v1 - id: setup-ndk - with: - ndk-version: r25b - add-to-path: false - - - name: Go Test - if: ${{github.ref_name=='Beta'}} - run: | - go test ./... - name: Set up xgo run: | @@ -69,23 +97,111 @@ jobs: ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }} run: | mkdir bin - make android-arm64 - make xgoall - cp release.sh ./bin/ + cp .github/release-cgo.sh ./bin/ + cp .github/rename-cgo.sh ./bin/ cd bin ls -la chmod +x * - bash ./release.sh - rm ./release.sh + bash ./release-cgo.sh + rm ./release-cgo.sh + bash ./rename-cgo.sh + rm ./rename-cgo.sh ls -la cd .. - - name: Delete current release assets - uses: andreaswilli/delete-release-assets-action@v2.0.0 + - uses: actions/upload-artifact@v3 + if: ${{ success() }} with: - github_token: ${{ secrets.GITHUB_TOKEN }} - tag: Prerelease-${{ github.ref_name }} - deleteOnlyFromDrafts: false + name: artifact + path: bin/ + + Build-Prerelease-WithCGO-Android: + permissions: write-all + runs-on: ubuntu-22.04 + + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v3 + + - name: Set variables + if: ${{github.ref_name=='Alpha'}} + run: echo "VERSION=alpha" >> $GITHUB_ENV + shell: bash + + - name: Set variables + if: ${{github.ref_name=='Beta'}} + run: echo "VERSION=beta" >> $GITHUB_ENV + shell: bash + + - name: Set ENV + run: | + echo "NAME=clash.meta" >> $GITHUB_ENV + echo "BUILD_TIME=$(date +%Y%m%d%H%M)" >> $GITHUB_ENV + echo "REPO=${{ github.repository }}" >> $GITHUB_ENV + echo "ShortSHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV + echo "$ShortSHA" + echo "$REPO" + shell: bash + + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: '1.19' + check-latest: true + + - name: Cache go module + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-WithCGO-Android-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-WithCGO-Android- + ${{ runner.os }}-go- + + - uses: nttld/setup-ndk@v1 + id: setup-ndk + with: + ndk-version: r25b + add-to-path: false + + - name: Build + if: success() + env: + ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }} + run: | + mkdir bin + cp .github/release-cgo-android.sh ./ + bash ./release-cgo-android.sh + rm ./release-cgo-android.sh + cp .github/rename-cgo.sh ./bin/ + cd bin + ls -la + chmod +x * + bash ./rename-cgo.sh + rm ./rename-cgo.sh + ls -la + cd .. + + - uses: actions/upload-artifact@v3 + if: ${{ success() }} + with: + name: artifact + path: bin/ + + Upload-Prerelease: + needs: [ Build-Prerelease-WithoutCGO, Build-Prerelease-WithCGO, Build-Prerelease-WithCGO-Android ] + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v3 + with: + name: artifact + path: bin/ + + - name: Display structure of downloaded files + run: ls -R + working-directory: bin - name: Tag Repo uses: richardsimko/update-tag@v1 diff --git a/Makefile b/Makefile index 78e4cd4f..9427be29 100644 --- a/Makefile +++ b/Makefile @@ -47,23 +47,9 @@ all:linux-amd64 linux-arm64\ darwin-amd64 darwin-arm64\ windows-amd64 windows-arm64\ -xgoTarget = windows/*,linux/*,darwin-10.16/* -xgoTags = with_gvisor,with_lwip -Ldflags =-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" -X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" -w -s -buildid= - -xgoall: SHELL:=/bin/bash -xgoall: - xgo --branch $(BRANCH) --out=$(BINDIR)/$(NAME) --targets='$(xgoTarget)' --tags='$(xgoTags)' -ldflags='$(Ldflags)' github.com/$(REPO) - darwin-all: darwin-amd64 darwin-arm64 -GOBUILDCGO = CGO_ENABLED=1 go build -tags with_gvisor,with_lwip -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \ - -X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \ - -w -s -buildid=' - -CCAndroid=$(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang - docker: GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ @@ -116,7 +102,7 @@ linux-mips64le: GOARCH=mips64le GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ android-arm64: - CC=$(CCAndroid) GOARCH=arm64 GOOS=android $(GOBUILDCGO) -o $(BINDIR)/$(NAME)-$@ + GOARCH=arm64 GOOS=android $(GOBUILD) -o $(BINDIR)/$(NAME)-$@ freebsd-386: GOARCH=386 GOOS=freebsd $(GOBUILD) -o $(BINDIR)/$(NAME)-$@