fix: update method to retrieve current tag in GitHub Actions workflow #37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Go | |
| on: | |
| push: | |
| branches: [ "master" ] | |
| workflow_dispatch: | |
| inputs: | |
| bump: | |
| description: "Which part of the version to bump?" | |
| required: false | |
| default: "patch" | |
| type: choice | |
| options: | |
| - patch | |
| - minor | |
| - major | |
| permissions: | |
| contents: write | |
| jobs: | |
| determine-version: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| tag: ${{ steps.next_version.outputs.tag }} | |
| current_tag: ${{ steps.current_tag.outputs.previous_tag }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get Current Tag | |
| id: current_tag | |
| run: | | |
| current_tag=$(git describe --tags --abbrev=0 HEAD^) | |
| echo "Current tag: $current_tag" | |
| echo "current_tag=$current_tag" >> $GITHUB_ENV | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Determine next version | |
| id: next_version | |
| uses: actions/github-script@v6 | |
| env: | |
| BUMP: ${{ github.event.inputs.bump || 'patch' }} | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const bump = process.env.BUMP || 'patch'; | |
| console.log(`Selected bump: ${bump}`); | |
| const latest = await github.rest.repos.getLatestRelease({ owner, repo }).catch(() => null); | |
| const currentVersion = latest ? latest.data.tag_name.replace(/^v/, '') : '0.0.0'; | |
| let [major, minor, patch] = currentVersion.split('.').map(Number); | |
| switch (bump) { | |
| case 'major': | |
| major++; | |
| minor = 0; | |
| patch = 0; | |
| break; | |
| case 'minor': | |
| minor++; | |
| patch = 0; | |
| break; | |
| default: | |
| patch++; | |
| } | |
| const nextVersion = `v${major}.${minor}.${patch}`; | |
| console.log(`Next version: ${nextVersion}`); | |
| core.setOutput('tag', nextVersion); | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: determine-version | |
| strategy: | |
| matrix: | |
| include: | |
| - os: linux | |
| arch: amd64 | |
| extension: "" | |
| - os: linux | |
| arch: arm64 | |
| extension: "" | |
| - os: darwin | |
| arch: arm64 | |
| extension: "" | |
| - os: windows | |
| arch: amd64 | |
| extension: ".exe" | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: '1.24' | |
| - name: Fetch dependencies | |
| run: go mod download | |
| - name: Build for ${{ matrix.os }}_${{ matrix.arch }} | |
| run: | | |
| GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -v -ldflags "-X 'github.com/urumo/k3sd/utils.Version=${{ needs.determine-version.outputs.tag }}'" -o k3sd${{ matrix.extension }} ./cli/main.go | |
| file k3sd${{ matrix.extension }} | |
| chmod +x k3sd${{ matrix.extension }} | |
| tar -czvf k3sd-${{ matrix.os }}-${{ matrix.arch }}.tar.gz k3sd${{ matrix.extension }} | |
| - name: Upload build artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: k3sd-${{ matrix.os }}-${{ matrix.arch }} | |
| path: | | |
| k3sd${{ matrix.extension }} | |
| k3sd-${{ matrix.os }}-${{ matrix.arch }}.tar.gz | |
| release: | |
| runs-on: ubuntu-latest | |
| needs: [ determine-version, build ] | |
| steps: | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| - name: Create Release | |
| id: create_release | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: ${{ needs.determine-version.outputs.tag }} | |
| release_name: Release ${{ needs.determine-version.outputs.tag }} | |
| draft: false | |
| body: | | |
| **Full Changelog**: https://github.com/urumo/k3sd/compare/${{ needs.determine-version.outputs.current_tag }}...${{ needs.determine-version.outputs.tag }} | |
| prerelease: false | |
| - name: Upload Linux AMD64 Release Asset | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: ./k3sd-linux-amd64/k3sd-linux-amd64.tar.gz | |
| asset_name: k3sd-linux-amd64.tar.gz | |
| asset_content_type: application/gzip | |
| - name: Upload Linux ARM64 Release Asset | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: ./k3sd-linux-arm64/k3sd-linux-arm64.tar.gz | |
| asset_name: k3sd-linux-arm64.tar.gz | |
| asset_content_type: application/gzip | |
| - name: Upload Darwin ARM64 Release Asset | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: ./k3sd-darwin-arm64/k3sd-darwin-arm64.tar.gz | |
| asset_name: k3sd-darwin-arm64.tar.gz | |
| asset_content_type: application/gzip | |
| - name: Upload Windows AMD64 Release Asset | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: ./k3sd-windows-amd64/k3sd-windows-amd64.tar.gz | |
| asset_name: k3sd-windows-amd64.tar.gz | |
| asset_content_type: application/gzip | |
| - name: Upload Binary Assets | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: ./k3sd-linux-amd64/k3sd | |
| asset_name: k3sd-linux-amd64 | |
| asset_content_type: application/octet-stream |