From 9c31db618b81e29e290d792a0104a7513e6e811d Mon Sep 17 00:00:00 2001 From: Serge Tkatchouk Date: Thu, 5 Aug 2021 01:45:16 +0800 Subject: [PATCH] Add test support for multiplatform builds --- .github/workflows/main.yml | 12 ++-- .github/workflows/release.yml | 101 ++++++++++++++++++++-------------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 35a0149..a44ef5c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,12 +11,16 @@ jobs: build: name: Build runs-on: ubuntu-latest + strategy: + matrix: + goos: [linux, darwin] + goarch: [amd64, arm64] steps: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: ^1.14 + go-version: ^1.16 id: go - name: Check out code into the Go module directory @@ -30,10 +34,8 @@ jobs: - name: Build run: | - export CGO_ENABLED=0 + export CGO_ENABLED=0 GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags "-s -w" \ -trimpath \ - -o ./hashcolor \ + -o ./hashcolor-${{ matrix.goos }}-${{ matrix.goarch }} \ ./cmd/hashcolor/main.go - - diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 91f40e6..54f36ff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,82 +1,103 @@ -name: Release +name: Create a draft Release on: push: tags: [ "v*" ] jobs: - build: - name: Build Release + create: + name: Create Release runs-on: ubuntu-latest + outputs: + upload_url: ${{ steps.create_release.outputs.upload_url }} steps: - - name: Set up Go 1.x - uses: actions/setup-go@v2 + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - go-version: ^1.14 - id: go + tag_name: ${{ github.ref }} + release_name: Hashcolor ${{ github.ref }} + draft: true + prerelease: false + + build: + name: Build Release binaries + runs-on: ubuntu-latest + needs: create + strategy: + matrix: + goos: [linux, darwin] + goarch: [amd64, arm64] + steps: - name: Check out code into the Go module directory uses: actions/checkout@v2 + - name: Get the version + id: get_version + run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} + + - name: Set up Go 1.x + id: go + uses: actions/setup-go@v2 + with: + go-version: ^1.16 + - name: Get dependencies run: | go get -v -t -d ./... go mod download go mod verify - - name: Get the version - id: get_version - run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} - - - name: Build + - name: Build Golang binaries (${{ matrix.goos }}/${{ matrix.goarch }}) + id: build_binary + env: + CGO_ENABLED: 0 + GOOS: "${{ matrix.goos }}" + GOARCH: "${{ matrix.goarch }}" run: | - export CGO_ENABLED=0 go build -ldflags "-s -w -X main.Version=${{ steps.get_version.outputs.VERSION }}" \ -trimpath -o ./hashcolor \ ./cmd/hashcolor/main.go - - name: Package + - name: Package (${{ matrix.goos }}/${{ matrix.goarch }}) + env: + CGO_ENABLED: 0 + GOOS: "${{ matrix.goos }}" + GOARCH: "${{ matrix.goarch }}" + VERSION: "${{ steps.get_version.outputs.VERSION }}" run: | - mkdir -p "./build/hashcolor-${{ steps.get_version.outputs.VERSION }}" - cp -av --target-directory="./build/hashcolor-${{ steps.get_version.outputs.VERSION }}" \ + mkdir -p "./build/hashcolor-${VERSION}" + cp -av --target-directory="./build/hashcolor-${VERSION}" \ ./examples ./hashcolor ./LICENSE ./README.md - cd "./build/hashcolor-${{ steps.get_version.outputs.VERSION }}" + cd "./build/hashcolor-${VERSION}" find . -type f -exec sha256sum "{}" + > checksums.lst cd .. - tar cvzf "./hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz" \ - "./hashcolor-${{ steps.get_version.outputs.VERSION }}" - sha256sum "./hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz" > \ - "./hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz.sha256" - - - name: Create Release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: Hashcolor ${{ github.ref }} - draft: true - prerelease: false + tar cvzf "./hashcolor-${VERSION}-${GOOS}-${GOARCH}.tar.gz" \ + "./hashcolor-${VERSION}" + sha256sum "./hashcolor-${VERSION}-${GOOS}-${GOARCH}.tar.gz" > \ + "./hashcolor-${VERSION}-${GOOS}-${GOARCH}.tar.gz.sha256" - - name: Upload Release Tarball + - name: Upload Release Tarball (${{ matrix.goos }}/${{ matrix.goarch }}) id: upload-release-tarball uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./build/hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz - asset_name: hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz + upload_url: ${{ needs.create.outputs.upload_url }} + asset_path: ./build/hashcolor-${{ steps.get_version.outputs.VERSION }}-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz + asset_name: hashcolor-${{ steps.get_version.outputs.VERSION }}-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz asset_content_type: application/gzip - - name: Upload Release Digest + - name: Upload Release Digest (${{ matrix.goos }}/${{ matrix.goarch }}) id: upload-release-digest uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./build/hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz.sha256 - asset_name: hashcolor-${{ steps.get_version.outputs.VERSION }}-linux-x86_64.tar.gz.sha256 + upload_url: ${{ needs.create.outputs.upload_url }} + asset_path: ./build/hashcolor-${{ steps.get_version.outputs.VERSION }}-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz.sha256 + asset_name: hashcolor-${{ steps.get_version.outputs.VERSION }}-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz.sha256 asset_content_type: text/plain