-
Notifications
You must be signed in to change notification settings - Fork 0
125 lines (110 loc) · 4.22 KB
/
release.yml
File metadata and controls
125 lines (110 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
name: Release CLI Binary
on:
workflow_dispatch:
push:
tags:
- 'v*.*.*' # Trigger this workflow when a new tag like v1.0.0 is pushed
env: # Define environment variables accessible to all jobs
GO_VERSION: '1.24'
jobs:
test:
name: Run Go Tests
runs-on: ubuntu-latest # Tests can typically run on a single OS/arch
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Download Go modules
run: go mod download
- name: Run tests
run: go test ./... -v
build:
name: Build ${{ matrix.target_os }}-${{ matrix.target_arch }}
runs-on: ubuntu-latest # Use Ubuntu as the runner for all builds
needs: test
strategy:
matrix:
# Define the OS/Architecture combinations you want to build for
target_os: [linux, darwin, windows]
target_arch: [amd64, arm64] # Add arm64 for Apple Silicon (darwin) and ARM Linux
# Exclude specific combinations if they are not supported or desired
exclude:
- target_os: windows
target_arch: arm64 # Windows ARM64 is less common for CLI tools
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Set output filename
id: set_filename
run: |
BINARY_NAME="${{ github.event.repository.name }}"
case ${{ matrix.target_os }} in
windows)
echo "ASSET_NAME=${BINARY_NAME}-${{ matrix.target_os }}-${{ matrix.target_arch }}.zip" >> $GITHUB_OUTPUT
echo "BINARY_PATH=${BINARY_NAME}.exe" >> $GITHUB_OUTPUT
;;
*)
echo "ASSET_NAME=${BINARY_NAME}-${{ matrix.target_os }}-${{ matrix.target_arch }}.tar.gz" >> $GITHUB_OUTPUT
echo "BINARY_PATH=${BINARY_NAME}" >> $GITHUB_OUTPUT
;;
esac
- name: Build and Package
env:
GOOS: ${{ matrix.target_os }}
GOARCH: ${{ matrix.target_arch }}
CGO_ENABLED: 0 # Disable CGO for static binaries (recommended for simpler distribution)
run: |
mkdir -p dist
# Build the binary
go build -o dist/${{ steps.set_filename.outputs.BINARY_PATH }} .
# Compress the binary
if [ "${{ matrix.target_os }}" = "windows" ]; then
cd dist
zip ${{ steps.set_filename.outputs.ASSET_NAME }} ${{ steps.set_filename.outputs.BINARY_PATH }}
else
cd dist
tar -czvf ${{ steps.set_filename.outputs.ASSET_NAME }} ${{ steps.set_filename.outputs.BINARY_PATH }}
fi
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.set_filename.outputs.ASSET_NAME }}
path: dist/${{ steps.set_filename.outputs.ASSET_NAME }}
release:
name: Create GitHub Release
runs-on: ubuntu-latest
needs: build
if: success() && github.ref_type == 'tag'
permissions:
contents: write # For creating the release and uploading assets
actions: read # Not strictly needed for softprops/action-gh-release, but harmless
# Removed: checks, pull-requests, statuses - often not needed for release action specifically
steps:
- name: Download all build artifacts
# Keep this step as softprops/action-gh-release can directly consume files from a directory
uses: actions/download-artifact@v4
with:
path: release_assets # Download all artifacts into this directory
- name: Create GitHub Release
uses: softprops/action-gh-release@v2 # Using the recommended action version 2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
name: Release ${{ github.ref }} # 'name' instead of 'release_name'
draft: false
prerelease: false
# Here's the magic: Directly upload all downloaded artifacts!
# This finds all files within any subdirectory under release_assets/
files: release_assets/**/*
# If you wanted to be more specific, e.g., only zip and tar.gz:
# files: |
# release_assets/**/*.zip
# release_assets/**/*.tar.gz