Skip to content

Commit 1f5be1c

Browse files
authored
Merge pull request #139 from RafaelJohn9/feat/publish
feat: updated build yml to support publishing to different platforms
2 parents da27f1b + 749598f commit 1f5be1c

1 file changed

Lines changed: 159 additions & 63 deletions

File tree

.github/workflows/build.yml

Lines changed: 159 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,190 @@ name: Release
33
on:
44
push:
55
tags:
6-
- v[0-9]+.[0-9]+.[0-9]+ # Matches semantic version tags
6+
- v[0-9]+.[0-9]+.[0-9]+
77

88
permissions:
99
contents: write
1010

1111
jobs:
12-
# === Linux + Windows builds using `cross` ===
13-
build-and-release-linux-windows:
12+
publish-cargo:
13+
name: Publish to Cargo
1414
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Setup Rust
18+
uses: actions-rs/toolchain@v1
19+
with:
20+
toolchain: stable
21+
- name: Publish to crates.io
22+
run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
23+
24+
publish-binaries:
25+
name: Publish binaries
26+
runs-on: ${{ matrix.build.os }}
27+
needs: publish-cargo
1528
strategy:
29+
fail-fast: false
1630
matrix:
17-
target:
18-
- x86_64-unknown-linux-gnu
19-
- x86_64-unknown-linux-musl
20-
- x86_64-pc-windows-gnu
21-
- aarch64-unknown-linux-gnu
31+
build:
32+
- { NAME: linux-x64-glibc, OS: ubuntu-22.04, TARGET: x86_64-unknown-linux-gnu }
33+
- { NAME: linux-x64-musl, OS: ubuntu-22.04, TARGET: x86_64-unknown-linux-musl }
34+
- { NAME: linux-arm64-glibc, OS: ubuntu-22.04, TARGET: aarch64-unknown-linux-gnu }
35+
- { NAME: win32-x64-msvc, OS: windows-latest, TARGET: x86_64-pc-windows-msvc }
36+
- { NAME: darwin-x64, OS: macos-15, TARGET: x86_64-apple-darwin }
37+
- { NAME: darwin-arm64, OS: macos-15, TARGET: aarch64-apple-darwin }
2238

2339
steps:
2440
- uses: actions/checkout@v4
2541

26-
- name: Install Rust
42+
- name: Setup Rust
2743
uses: actions-rs/toolchain@v1
2844
with:
2945
toolchain: stable
30-
override: true
46+
target: ${{ matrix.build.TARGET }}
3147

3248
- name: Install cross
49+
if: runner.os == 'Linux'
3350
run: cargo install cross --git https://github.com/cross-rs/cross
3451

35-
- name: Build with cross
36-
run: cross build --release --target ${{ matrix.target }}
37-
env:
38-
OPENSSL_NO_VENDOR: "0"
39-
OPENSSL_STATIC: "1"
40-
41-
- name: Rename binary
52+
- name: Build and prepare
4253
run: |
43-
if [[ "${{ matrix.target }}" == *"windows"* ]]; then
44-
cp target/${{ matrix.target }}/release/gh-templates.exe gh-templates-${{ matrix.target }}.exe
54+
VERSION=$(echo "${{ github.ref_name }}" | sed 's/^v//')
55+
56+
# Build
57+
if [[ "${{ runner.os }}" == "Linux" ]]; then
58+
cross build --release --target ${{ matrix.build.TARGET }}
4559
else
46-
cp target/${{ matrix.target }}/release/gh-templates gh-templates-${{ matrix.target }}
60+
cargo build --release --target ${{ matrix.build.TARGET }}
4761
fi
48-
49-
- name: Upload asset to GitHub Release
62+
63+
# Copy binary
64+
if [[ "${{ matrix.build.TARGET }}" == *"windows"* ]]; then
65+
BINARY="gh-templates.exe"
66+
cp target/${{ matrix.build.TARGET }}/release/$BINARY .
67+
else
68+
BINARY="gh-templates"
69+
cp target/${{ matrix.build.TARGET }}/release/$BINARY .
70+
chmod +x $BINARY
71+
fi
72+
73+
# Create npm package
74+
mkdir -p npm/bin python/gh_templates_bin
75+
cp $BINARY npm/bin/
76+
cp $BINARY python/gh_templates_bin/
77+
78+
# npm package.json
79+
cat > npm/package.json << EOF
80+
{
81+
"name": "@rafaeljohn9/gh-templates-${{ matrix.build.NAME }}",
82+
"version": "$VERSION",
83+
"bin": { "gh-templates": "./bin/$BINARY" },
84+
"files": ["bin/"],
85+
"license": "APACHE-2.0",
86+
}
87+
EOF
88+
89+
# Python setup.py
90+
cat > python/setup.py << EOF
91+
from setuptools import setup
92+
setup(
93+
name='gh-templates-${{ matrix.build.NAME }}',
94+
version='$VERSION',
95+
packages=['gh_templates_bin'],
96+
package_data={'gh_templates_bin': ['*']},
97+
entry_points={'console_scripts': ['gh-templates=gh_templates_bin:main']},
98+
license='APACHE-2.0",'
99+
)
100+
EOF
101+
102+
# Python __init__.py
103+
cat > python/gh_templates_bin/__init__.py << 'EOF'
104+
import os, sys, subprocess
105+
def main():
106+
binary = os.path.join(os.path.dirname(__file__), '$BINARY')
107+
sys.exit(subprocess.run([binary] + sys.argv[1:]).returncode)
108+
EOF
109+
110+
- name: Upload to GitHub Release
50111
uses: softprops/action-gh-release@v2
51112
with:
52-
name: gh-templates-${{ matrix.target }}
53-
tag_name: ${{ github.ref_name }}
54-
files: gh-templates-${{ matrix.target }}*
55-
env:
56-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
113+
files: gh-templates*
57114

58-
# === macOS builds using native cargo ===
59-
build-and-release-macos:
60-
runs-on: macos-latest
61-
strategy:
62-
matrix:
63-
target:
64-
- x86_64-apple-darwin
65-
- aarch64-apple-darwin
115+
- name: Publish to npm
116+
if: matrix.build.NAME != 'linux-x64-musl'
117+
run: |
118+
cd npm
119+
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
120+
npm publish --access public
66121
122+
- name: Publish to PyPI
123+
run: |
124+
cd python
125+
pip install twine
126+
python setup.py sdist bdist_wheel
127+
echo -e "[pypi]\nusername = __token__\npassword = ${{ secrets.PYPI_API_TOKEN }}" > ~/.pypirc
128+
twine upload dist/* --skip-existing
129+
130+
publish-homebrew:
131+
name: Update Homebrew formula
132+
runs-on: macos-latest
133+
needs: publish-binaries
67134
steps:
68-
- uses: actions/checkout@v4
69-
70-
- name: Install Rust
71-
uses: actions-rs/toolchain@v1
72-
with:
73-
toolchain: stable
74-
target: ${{ matrix.target }}
75-
override: true
76-
77-
- name: Build with cargo
78-
run: cargo build --release --target ${{ matrix.target }}
79-
env:
80-
OPENSSL_NO_VENDOR: "0"
81-
OPENSSL_STATIC: "1"
82-
83-
- name: Rename binary
135+
- name: Update Homebrew formula
84136
run: |
85-
cp target/${{ matrix.target }}/release/gh-templates gh-templates-${{ matrix.target }}
86-
87-
- name: Upload asset to GitHub Release
88-
uses: softprops/action-gh-release@v2
89-
with:
90-
name: gh-templates-${{ matrix.target }}
91-
tag_name: ${{ github.ref_name }}
92-
files: gh-templates-${{ matrix.target }}
93-
env:
94-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95-
96-
137+
VERSION=$(echo "${{ github.ref_name }}" | sed 's/^v//')
138+
139+
# Get release assets
140+
LINUX_URL="https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/gh-templates-linux-x64-glibc"
141+
DARWIN_ARM_URL="https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/gh-templates-darwin-arm64"
142+
DARWIN_X64_URL="https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/gh-templates-darwin-x64"
143+
144+
# Calculate checksums
145+
wget -q $LINUX_URL -O linux-binary
146+
wget -q $DARWIN_ARM_URL -O darwin-arm-binary
147+
wget -q $DARWIN_X64_URL -O darwin-x64-binary
148+
149+
LINUX_SHA=$(shasum -a 256 linux-binary | cut -d' ' -f1)
150+
DARWIN_ARM_SHA=$(shasum -a 256 darwin-arm-binary | cut -d' ' -f1)
151+
DARWIN_X64_SHA=$(shasum -a 256 darwin-x64-binary | cut -d' ' -f1)
152+
153+
# Clone homebrew tap (replace with your tap)
154+
git clone https://github.com/rafaeljohn9/homebrew-tap.git
155+
cd homebrew-tap
156+
157+
# Create/update formula
158+
cat > Formula/gh-templates.rb << EOF
159+
class GhTemplates < Formula
160+
desc "GitHub Templates CLI tool"
161+
homepage "https://github.com/${{ github.repository }}"
162+
version "$VERSION"
163+
164+
on_macos do
165+
if Hardware::CPU.arm?
166+
url "$DARWIN_ARM_URL"
167+
sha256 "$DARWIN_ARM_SHA"
168+
else
169+
url "$DARWIN_X64_URL"
170+
sha256 "$DARWIN_X64_SHA"
171+
end
172+
end
173+
174+
on_linux do
175+
url "$LINUX_URL"
176+
sha256 "$LINUX_SHA"
177+
end
178+
179+
def install
180+
bin.install "gh-templates-darwin-arm64" => "gh-templates" if Hardware::CPU.arm?
181+
bin.install "gh-templates-darwin-x64" => "gh-templates" if Hardware::CPU.intel?
182+
bin.install "gh-templates-linux-x64-glibc" => "gh-templates" if OS.linux?
183+
end
184+
end
185+
EOF
186+
187+
# ComAPACHE-2.0", and push
188+
git config user.name "github-actions[bot]"
189+
git config user.email "github-actions[bot]@users.noreply.github.com"
190+
git add Formula/gh-templates.rb
191+
git comAPACHE-2.0", -m "Update gh-templates to $VERSION"
192+
git push https://x-access-token:${{ secrets.HOMEBREW_GITHUB_TOKEN }}@github.com/rafaeljohn9/homebrew-tap.git

0 commit comments

Comments
 (0)