From cc4b935b59daafc39f8e077de7e6ffbe72f16dfd Mon Sep 17 00:00:00 2001 From: Al Berez Date: Tue, 12 Nov 2024 10:51:32 -0800 Subject: [PATCH 1/2] Add release workflow --- .github/workflows/release.yml | 146 ++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2e5eac7 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,146 @@ +name: Release + +on: + workflow_dispatch: + inputs: + version_increment: + description: 'Version increment type' + type: choice + options: + - major + - minor + - patch + default: 'minor' + required: true + draft: + description: 'Create draft release' + type: boolean + default: true + required: true + +permissions: + contents: write + packages: read + +jobs: + prepare-release: + runs-on: ubuntu-latest + outputs: + release_version: ${{ steps.get_version.outputs.version }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Get or calculate version + id: get_version + shell: bash + run: | + set -euo pipefail + + git fetch --tags + latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") + current_version=${latest_tag#v} + IFS='.' read -ra version_parts <<< "$current_version" + + case "${{ inputs.version_increment }}" in + major) + new_major=$((version_parts[0] + 1)) + version="v${new_major}.0.0" + ;; + minor) + new_minor=$((version_parts[1] + 1)) + version="v${version_parts[0]}.$new_minor.0" + ;; + patch) + new_patch=$((version_parts[2] + 1)) + version="v${version_parts[0]}.${version_parts[1]}.$new_patch" + ;; + esac + echo "Calculated new version: $version" + + echo "version=$version" >> $GITHUB_OUTPUT + + build: + needs: prepare-release + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - os: linux + arch: amd64 + goos: linux + goarch: amd64 + suffix: 64 + - os: darwin + arch: amd64 + goos: darwin + goarch: amd64 + suffix: amd64 + - os: darwin + arch: arm64 + goos: darwin + goarch: arm64 + suffix: arm + - os: windows + arch: amd64 + goos: windows + goarch: amd64 + suffix: 64 + + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + check-latest: true + + - name: Build Binary + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + CGO_ENABLED: 0 + run: | + output_name="stack-auditor-${{ matrix.os }}-${{ matrix.suffix }}" + echo "::group::Building for ${{ matrix.os }}-${{ matrix.arch }}" + go build -v -trimpath -ldflags="-s -w" -o "dist/$output_name" . + echo "::endgroup::" + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: stack-auditor-${{ matrix.os }}-${{ matrix.suffix }} + path: dist/stack-auditor* + compression-level: 0 + + create-release: + needs: [prepare-release, build] + runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Download all artifacts + uses: actions/download-artifact@v4 + with: + path: dist + merge-multiple: true + + - name: Create GitHub Release + run: | + echo "# Changes in this release" > release_notes.md + + draft_flag="${{ github.event.inputs.draft || 'true' }}" + + gh release create "${{ needs.prepare-release.outputs.release_version }}" \ + --draft="$draft_flag" \ + --title="${{ needs.prepare-release.outputs.release_version }}" \ + --notes-file=release_notes.md \ + dist/stack-auditor* From 2f3c9f483e8d57635295ac3b56453b2293a01f86 Mon Sep 17 00:00:00 2001 From: Al Berez Date: Tue, 12 Nov 2024 14:18:46 -0800 Subject: [PATCH 2/2] code review suggestions --- .github/workflows/release.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2e5eac7..bb35f4c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -65,7 +65,7 @@ jobs: needs: prepare-release runs-on: ubuntu-latest strategy: - fail-fast: false + fail-fast: true matrix: include: - os: linux @@ -106,8 +106,13 @@ jobs: CGO_ENABLED: 0 run: | output_name="stack-auditor-${{ matrix.os }}-${{ matrix.suffix }}" + git_tag=${{ needs.prepare-release.outputs.release_version }} + version=${git_tag:1} echo "::group::Building for ${{ matrix.os }}-${{ matrix.arch }}" - go build -v -trimpath -ldflags="-s -w" -o "dist/$output_name" . + go build -v -trimpath \ + -ldflags="-s -w" \ + -X main.tagVersion=${version:?} \ + -o "dist/${output_name}" . echo "::endgroup::" - name: Upload artifact @@ -127,6 +132,13 @@ jobs: with: fetch-depth: 0 + - name: Create and push git tag + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git tag -f "${{ needs.prepare-release.outputs.release_version }}" + git push origin "${{ needs.prepare-release.outputs.release_version }}" --force + - name: Download all artifacts uses: actions/download-artifact@v4 with: