From cda5ce24c7339752b6d201bccd7f431ac105c157 Mon Sep 17 00:00:00 2001
From: Andre Miras <andre.miras@gmail.com>
Date: Mon, 26 Aug 2024 22:49:00 +0200
Subject: [PATCH] ci: Upload static build artifacts

Make static builds and upload them as artifacts.
This makes it possible to download the binary produced for each PR.
In a follow up PR we could also automate binary upload upon release
as well as enabling cross compilation for different arch and OS.

Note that we use musl-gcc as it allows to statically link a libc
implementation while CGO_ENABLED=0 only was leading to the error below:
```
ethermint@v0.22.0-sdk50-1/app/ante/eip712.go:293:36: undefined: secp256k1.RecoverPubkey
ethermint@v0.22.0-sdk50-1/app/ante/eip712.go:319:17: undefined: secp256k1.VerifySignature
```
The statically linked binary is 100M as of today.

Also drop get-diff-action as it's unmaintained and was misbehaving on
the rebasing and squashing workflow, refs #139
---
 .github/workflows/build.yml | 40 +++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 3f0dd70a2..d701d631a 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -20,22 +20,32 @@ jobs:
     strategy:
       matrix:
         go-arch: ["amd64"]
+        go-os: ["linux"]
+    env:
+      GOARCH: ${{ matrix.go-arch }}
+      GOOS: ${{ matrix.go-os }}
+      LEDGER_ENABLED: false
     steps:
-      - uses: actions/checkout@v3
-      - uses: actions/setup-go@v3
+      - uses: actions/checkout@v4
+      - uses: actions/setup-go@v5
         with:
           go-version: "^1.21"
-      - uses: technote-space/get-diff-action@v6.1.2
-        id: git_diff
+      - name: Non-static Build
+        run: |
+          make build
+          mv ./build/cantod ./build/cantod-nonstatic-${{ matrix.go-os }}-${{ matrix.go-arch }}
+      - name: Install musl-tools
+        run: sudo apt-get update && sudo apt-get install -y musl-tools
+      - name: Static Build
+        env:
+          CC: musl-gcc
+          LDFLAGS: "-extldflags -static"
+        run: |
+          rm -r build
+          make build
+          mv build/cantod ./build/cantod-${{ matrix.go-os }}-${{ matrix.go-arch }}
+      - name: Upload cantod Binary (Static)
+        uses: actions/upload-artifact@v4
         with:
-          PATTERNS: |
-            **/*.go
-            go.mod
-            go.sum
-            **/go.mod
-            **/go.sum
-            **/Makefile
-            Makefile
-      - name: Build
-        if: env.GIT_DIFF
-        run: GOARCH=${{ matrix.go-arch }} LEDGER_ENABLED=false make build
+          name: cantod-${{ matrix.go-os }}-${{ matrix.go-arch }}
+          path: ./build/cantod-${{ matrix.go-os }}-${{ matrix.go-arch }}