From 1ab8b6e747a9ecbede8fc38d4639208080820607 Mon Sep 17 00:00:00 2001 From: Sly_tom_cat Date: Sun, 2 Jul 2023 15:35:13 +0300 Subject: [PATCH] Optimize binary size --- .github/workflows/go.yml | 47 ++++++++++++++++++++++++++++++++++++++++ build.sh | 3 +++ main.go | 6 ++--- 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/go.yml create mode 100755 build.sh diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..dc19753 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,47 @@ +name: Go +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.20.5 + - name: Cache + uses: actions/cache@v3 + with: + path: | + /home/runner/.cache/go-build/ + /home/runner/go/pkg/mod/ + key: GO_MOD_CACHE + - name: Requirements + run: sudo apt-get install upx-ucl + - name: Build + run: ./build.sh + - name: Upload a Build Artifacts + uses: actions/upload-artifact@v3 + with: + name: build_artifacts + path: | + ws + push: + needs: build + if: github.ref == 'refs/heads/master' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Download Artifacts + uses: actions/download-artifact@v3 + with: + name: build_artifacts + - name: Draft new release and upload assets + env: + GH_TOKEN: ${{ secrets.ACTION_TOKEN }} + run: | + gh release create "$(git branch --show-current)-$(git rev-parse --short HEAD)" ./ws \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..973e613 --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash +CGO_ENABLED=0 go build -buildvcs=false -trimpath -ldflags="-s -w -X main.version=$(git branch --show-current)-$(git rev-parse --short HEAD)" . +upx -qqq --best ws \ No newline at end of file diff --git a/main.go b/main.go index 56f4dfc..d8a5b4a 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ import ( ) // Version is app version -const Version = "0.2.3" +var version = "" var options struct { origin string @@ -31,7 +31,7 @@ var options struct { func main() { rootCmd := &cobra.Command{ Use: "ws URL", - Short: fmt.Sprintf("ws is a websocket client v.%s", Version), + Short: fmt.Sprintf("ws is a websocket client v.%s", version), Run: root, } rootCmd.Flags().StringVarP(&options.origin, "origin", "o", "", "websocket origin") @@ -48,7 +48,7 @@ func main() { func root(cmd *cobra.Command, args []string) { if options.printVersion { - fmt.Printf("ws v.%s\n", Version) + fmt.Printf("ws v.%s\n", version) os.Exit(0) }