From f9d2a0bc5339c4777e73432d9a8208b092a2e9ac Mon Sep 17 00:00:00 2001 From: Adam Simpson Date: Wed, 7 Jul 2021 15:22:26 -0400 Subject: [PATCH] feat: add github actions support for building binaries The bulk of the work for Github Actions happens in build.sh. Additional platforms/architectures need to be specified in that file. --- .github/workflows/go.yml | 20 ++++++++++++++++++++ build.sh | 31 ++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..921ef7b --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,20 @@ +name: build releases + +on: + release: + types: [published] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.16 + + - name: build binaries + run: GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} ./build.sh diff --git a/build.sh b/build.sh index 0745f2d..ebbfcb9 100755 --- a/build.sh +++ b/build.sh @@ -1,15 +1,28 @@ #!/bin/sh -GIT_TAG=$(git tag | tail -1); GOOS=darwin GOARCH=arm64 go build -o arm64-macos-sb -a -ldflags="-X 'sb/cmd.AppVersion=$GIT_TAG'" +# GITHUB_EVENT_PATH documented here: +# https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables +GIT_TAG=$(jq .release.tag_name < "${GITHUB_EVENT_PATH}" | sed -e 's/"//g') +UPLOAD_URL=$(jq .release.upload_url < "${GITHUB_EVENT_PATH}" | sed -e 's/"//g' | cut -d "{" -f 1) +RELEASES="arm64-darwin-sb amd64-linux-sb amd64-darwin-sb" -GIT_TAG=$(git tag | tail -1); GOOS=darwin GOARCH=amd64 go build -o amd64-macos-sb -a -ldflags="-X 'sb/cmd.AppVersion=$GIT_TAG'" +upload_file() { + NAME=$1 -GIT_TAG=$(git tag | tail -1); GOOS=linux GOARCH=amd64 go build -o amd64-linux-sb -a -ldflags="-X 'sb/cmd.AppVersion=$GIT_TAG'" + zip "${NAME}.zip" "${NAME}" + curl -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN}" \ + -H "Content-Type: application/zip" \ + --data-binary "@${NAME}.zip" \ + "${UPLOAD_URL}?name=${NAME}.zip" +} -zip arm64-macos-sb.zip arm64-macos-sb +for PLATFORM in ${RELEASES}; do + GOOS=$(echo "${PLATFORM}" | cut -d - -f 2) \ + GOARCH=$(echo "${PLATFORM}" | cut -d - -f 1) \ + go build -o "${PLATFORM}" -a -ldflags="-X 'sb/cmd.AppVersion=${GIT_TAG}'" -zip amd64-macos-sb.zip amd64-macos-sb - -zip amd64-linux-sb.zip amd64-linux-sb - -rm *-sb + if [ "${UPLOAD_URL}" != null ]; then + upload_file "${PLATFORM}" + fi +done