Skip to content

Commit 369de2a

Browse files
authored
Switch release workflow to GoReleaser (#5)
1 parent 2ef97d5 commit 369de2a

6 files changed

Lines changed: 150 additions & 72 deletions

File tree

.github/workflows/release.yml

Lines changed: 90 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,82 +24,109 @@ jobs:
2424
- name: Test
2525
run: go test ./...
2626

27-
build:
28-
name: Build release binaries
27+
release:
28+
name: Run GoReleaser
2929
needs: test
3030
runs-on: ubuntu-latest
31-
strategy:
32-
fail-fast: false
33-
matrix:
34-
include:
35-
- goos: linux
36-
goarch: amd64
37-
os_name: linux
38-
ext: ""
39-
- goos: linux
40-
goarch: arm64
41-
os_name: linux
42-
ext: ""
43-
- goos: darwin
44-
goarch: amd64
45-
os_name: macos
46-
ext: ""
47-
- goos: darwin
48-
goarch: arm64
49-
os_name: macos
50-
ext: ""
51-
- goos: windows
52-
goarch: amd64
53-
os_name: windows
54-
ext: ".exe"
55-
- goos: windows
56-
goarch: arm64
57-
os_name: windows
58-
ext: ".exe"
59-
6031
steps:
6132
- name: Checkout
6233
uses: actions/checkout@v4
34+
with:
35+
fetch-depth: 0
6336

6437
- name: Set up Go
6538
uses: actions/setup-go@v5
6639
with:
6740
go-version-file: go.mod
6841

69-
- name: Build
42+
- name: Run GoReleaser
43+
uses: goreleaser/goreleaser-action@v7
44+
with:
45+
distribution: goreleaser
46+
version: "~> v2"
47+
args: release --clean
7048
env:
71-
GOOS: ${{ matrix.goos }}
72-
GOARCH: ${{ matrix.goarch }}
73-
CGO_ENABLED: "0"
74-
VERSION: ${{ github.ref_name }}
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
51+
- name: Calculate Homebrew checksums
52+
id: brew
7553
run: |
76-
mkdir -p dist
77-
output="dist/sing-box-sub_${VERSION}_${{ matrix.os_name }}_${GOARCH}${{ matrix.ext }}"
78-
go build \
79-
-trimpath \
80-
-ldflags "-s -w -X main.version=${VERSION}" \
81-
-o "${output}" \
82-
./cmd/sing-box-subscribe-cli
83-
84-
- name: Upload artifact
85-
uses: actions/upload-artifact@v4
86-
with:
87-
name: sing-box-sub_${{ github.ref_name }}_${{ matrix.os_name }}_${{ matrix.goarch }}
88-
path: dist/*
89-
if-no-files-found: error
54+
version="${GITHUB_REF_NAME#v}"
55+
tag="${GITHUB_REF_NAME}"
56+
echo "version=${version}" >> "$GITHUB_OUTPUT"
57+
echo "tag=${tag}" >> "$GITHUB_OUTPUT"
58+
echo "macos_arm64_sha=$(sha256sum "dist/sing-box-sub_${tag}_macos_arm64.tar.gz" | awk '{print $1}')" >> "$GITHUB_OUTPUT"
59+
echo "macos_amd64_sha=$(sha256sum "dist/sing-box-sub_${tag}_macos_amd64.tar.gz" | awk '{print $1}')" >> "$GITHUB_OUTPUT"
60+
echo "linux_arm64_sha=$(sha256sum "dist/sing-box-sub_${tag}_linux_arm64.tar.gz" | awk '{print $1}')" >> "$GITHUB_OUTPUT"
61+
echo "linux_amd64_sha=$(sha256sum "dist/sing-box-sub_${tag}_linux_amd64.tar.gz" | awk '{print $1}')" >> "$GITHUB_OUTPUT"
9062
91-
release:
92-
name: Publish GitHub release
93-
needs: build
94-
runs-on: ubuntu-latest
95-
steps:
96-
- name: Download artifacts
97-
uses: actions/download-artifact@v4
63+
- name: Checkout Homebrew tap
64+
uses: actions/checkout@v4
9865
with:
99-
path: dist
100-
merge-multiple: true
66+
repository: rainbend/homebrew-tap
67+
token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
68+
path: homebrew-tap
10169

102-
- name: Upload release assets
103-
uses: softprops/action-gh-release@v2
104-
with:
105-
files: dist/*
70+
- name: Update Homebrew formula
71+
env:
72+
FORMULA_PATH: homebrew-tap/Formula/sing-box-subscribe-cli.rb
73+
TAG: ${{ steps.brew.outputs.tag }}
74+
VERSION: ${{ steps.brew.outputs.version }}
75+
MACOS_ARM64_SHA: ${{ steps.brew.outputs.macos_arm64_sha }}
76+
MACOS_AMD64_SHA: ${{ steps.brew.outputs.macos_amd64_sha }}
77+
LINUX_ARM64_SHA: ${{ steps.brew.outputs.linux_arm64_sha }}
78+
LINUX_AMD64_SHA: ${{ steps.brew.outputs.linux_amd64_sha }}
79+
run: |
80+
mkdir -p "$(dirname "$FORMULA_PATH")"
81+
cat > "$FORMULA_PATH" <<FORMULA
82+
class SingBoxSubscribeCli < Formula
83+
desc "Generate sing-box configs from subscriptions and JSON templates"
84+
homepage "https://github.com/rainbend/sing-box-subscribe-cli"
85+
version "${VERSION}"
86+
license "Apache-2.0"
87+
88+
on_macos do
89+
on_arm do
90+
url "https://github.com/rainbend/sing-box-subscribe-cli/releases/download/${TAG}/sing-box-sub_${TAG}_macos_arm64.tar.gz"
91+
sha256 "${MACOS_ARM64_SHA}"
92+
end
93+
94+
on_intel do
95+
url "https://github.com/rainbend/sing-box-subscribe-cli/releases/download/${TAG}/sing-box-sub_${TAG}_macos_amd64.tar.gz"
96+
sha256 "${MACOS_AMD64_SHA}"
97+
end
98+
end
99+
100+
on_linux do
101+
if Hardware::CPU.arm? && Hardware::CPU.is_64_bit?
102+
url "https://github.com/rainbend/sing-box-subscribe-cli/releases/download/${TAG}/sing-box-sub_${TAG}_linux_arm64.tar.gz"
103+
sha256 "${LINUX_ARM64_SHA}"
104+
end
105+
106+
if Hardware::CPU.intel? && Hardware::CPU.is_64_bit?
107+
url "https://github.com/rainbend/sing-box-subscribe-cli/releases/download/${TAG}/sing-box-sub_${TAG}_linux_amd64.tar.gz"
108+
sha256 "${LINUX_AMD64_SHA}"
109+
end
110+
end
111+
112+
def install
113+
bin.install "sing-box-sub"
114+
end
115+
116+
test do
117+
assert_match version.to_s, shell_output("#{bin}/sing-box-sub version")
118+
assert_match "sb-config-1.14.json", shell_output("#{bin}/sing-box-sub list")
119+
end
120+
end
121+
FORMULA
122+
ruby -c "$FORMULA_PATH"
123+
124+
- name: Commit Homebrew formula
125+
working-directory: homebrew-tap
126+
run: |
127+
git config user.name "github-actions[bot]"
128+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
129+
[ -n "$(git status --porcelain -- Formula/sing-box-subscribe-cli.rb)" ] || exit 0
130+
git add Formula/sing-box-subscribe-cli.rb
131+
git commit -m "Update sing-box-subscribe-cli to ${GITHUB_REF_NAME}"
132+
git push

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/sing-box-subscribe-cli
22
/bin/
3+
/dist/
34
config.json

.goreleaser.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: 2
2+
3+
project_name: sing-box-subscribe-cli
4+
5+
builds:
6+
- id: sing-box-sub
7+
main: ./cmd/sing-box-subscribe-cli
8+
binary: sing-box-sub
9+
env:
10+
- CGO_ENABLED=0
11+
goos:
12+
- linux
13+
- darwin
14+
- windows
15+
goarch:
16+
- amd64
17+
- arm64
18+
ldflags:
19+
- -s -w -X main.version={{ .Tag }}
20+
21+
archives:
22+
- id: default
23+
ids:
24+
- sing-box-sub
25+
formats:
26+
- tar.gz
27+
format_overrides:
28+
- goos: windows
29+
formats:
30+
- zip
31+
name_template: 'sing-box-sub_{{ .Tag }}_{{ if eq .Os "darwin" }}macos{{ else }}{{ .Os }}{{ end }}_{{ .Arch }}'
32+
33+
checksum:
34+
name_template: checksums.txt
35+
algorithm: sha256

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ If `ExecStartPre` fails, systemd stops the startup before launching sing-box. Ma
105105

106106
### Windows
107107

108-
Download the matching `.exe` from the [GitHub Releases page](https://github.com/rainbend/sing-box-subscribe-cli/releases), rename it to `sing-box-sub.exe` if you like, and place it in a directory listed in `PATH`.
108+
Download the matching Windows `.zip` from the [GitHub Releases page](https://github.com/rainbend/sing-box-subscribe-cli/releases), extract `sing-box-sub.exe`, and place it in a directory listed in `PATH`.
109109

110110
Check the installed command:
111111

@@ -274,7 +274,7 @@ git tag v1.0.0
274274
git push origin v1.0.0
275275
```
276276

277-
The release workflow runs tests, builds Linux, macOS, and Windows binaries for `amd64` and `arm64`, injects the tag into `sing-box-sub version`, and uploads the binaries to GitHub Releases.
277+
The release workflow runs tests, uses GoReleaser to build Linux, macOS, and Windows archives for `amd64` and `arm64`, injects the tag into `sing-box-sub version`, uploads the artifacts and `checksums.txt` to GitHub Releases, and updates the Homebrew tap formula.
278278

279279
Container images are published to GitHub Packages. Version tags publish `<version>` and `latest`; pushes to `main` publish `main` and a `sha-...` tag.
280280

README.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ WantedBy=multi-user.target
105105

106106
### Windows
107107

108-
[GitHub Releases 页面](https://github.com/rainbend/sing-box-subscribe-cli/releases) 下载匹配的 `.exe` 文件,可以重命名为 `sing-box-sub.exe`,并放到 `PATH` 包含的目录。
108+
[GitHub Releases 页面](https://github.com/rainbend/sing-box-subscribe-cli/releases) 下载匹配的 Windows `.zip` 文件,解压出 `sing-box-sub.exe`,并放到 `PATH` 包含的目录。
109109

110110
检查安装结果:
111111

@@ -274,7 +274,7 @@ git tag v1.0.0
274274
git push origin v1.0.0
275275
```
276276

277-
release workflow 会运行测试,构建 Linux、macOS 和 Windows 的 `amd64``arm64` 二进制文件,把 tag 注入 `sing-box-sub version`并上传到 GitHub Releases。
277+
release workflow 会运行测试,通过 GoReleaser 构建 Linux、macOS 和 Windows 的 `amd64``arm64` 归档文件,把 tag 注入 `sing-box-sub version`将产物和 `checksums.txt` 上传到 GitHub Releases,并更新 Homebrew tap formula
278278

279279
容器镜像会发布到 GitHub Packages。版本 tag 会发布 `<version>``latest`;推送到 `main` 会发布 `main``sha-...` tag。
280280

install.sh

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,33 @@ case "$version" in
5656
*) version="v${version}" ;;
5757
esac
5858

59-
asset="${release_binary}_${version}_linux_${arch}"
60-
url="https://github.com/${repo}/releases/download/${version}/${asset}"
59+
archive_asset="${release_binary}_${version}_linux_${arch}.tar.gz"
60+
raw_asset="${release_binary}_${version}_linux_${arch}"
61+
archive_url="https://github.com/${repo}/releases/download/${version}/${archive_asset}"
62+
raw_url="https://github.com/${repo}/releases/download/${version}/${raw_asset}"
6163
tmp_dir="$(mktemp -d)"
62-
tmp_file="${tmp_dir}/${asset}"
64+
archive_file="${tmp_dir}/${archive_asset}"
65+
tmp_file="${tmp_dir}/${release_binary}"
6366

6467
cleanup() {
6568
rm -rf "$tmp_dir"
6669
}
6770
trap cleanup EXIT INT TERM
6871

69-
echo "Downloading ${asset}..."
70-
curl -fL "$url" -o "$tmp_file"
72+
echo "Downloading ${archive_asset}..."
73+
if curl -fsSL "$archive_url" -o "$archive_file"; then
74+
need_cmd tar
75+
tar -xzf "$archive_file" -C "$tmp_dir"
76+
else
77+
echo "Archive not found, falling back to ${raw_asset}..."
78+
curl -fL "$raw_url" -o "$tmp_file"
79+
fi
80+
81+
if [ ! -f "$tmp_file" ]; then
82+
echo "error: release asset did not contain ${release_binary}" >&2
83+
exit 1
84+
fi
85+
7186
chmod 0755 "$tmp_file"
7287

7388
if [ ! -d "$install_dir" ]; then

0 commit comments

Comments
 (0)