Skip to content

Commit 8c8b090

Browse files
committed
feat(rust-build): add reusable workflow
1 parent f7cf44d commit 8c8b090

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed

.github/workflows/rust-build.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: Build, Test and Publish Rust Package
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
rust-version:
7+
description: 'Rust version to use'
8+
default: 'stable'
9+
type: string
10+
build-target:
11+
description: 'Cargo profile to use for building (debug, release)'
12+
default: 'release'
13+
type: string
14+
enable-cache:
15+
description: 'Enable caching of dependencies'
16+
default: true
17+
type: boolean
18+
publish-crates-io:
19+
description: 'Publish package to crates.io'
20+
default: false
21+
type: boolean
22+
upload-artifact:
23+
description: 'Upload build artifact'
24+
default: false
25+
type: boolean
26+
artifact-name:
27+
description: 'Name of the artifact to upload'
28+
type: string
29+
required: false
30+
artifact-path:
31+
description: 'Path to the artifact to upload'
32+
type: string
33+
required: false
34+
secrets:
35+
CRATES_IO_TOKEN:
36+
required: false
37+
38+
jobs:
39+
build:
40+
runs-on: ubuntu-latest
41+
outputs:
42+
build_success: ${{ steps.set-output.outputs.build_success }}
43+
steps:
44+
- name: Checkout repository
45+
uses: actions/checkout@v4
46+
47+
- name: Install Rust toolchain
48+
uses: actions-rs/toolchain@v1
49+
with:
50+
profile: minimal
51+
toolchain: ${{ inputs.rust-version }}
52+
override: true
53+
54+
- name: Cache dependencies
55+
if: ${{ inputs.enable-cache }}
56+
uses: actions/cache@v4
57+
with:
58+
path: |
59+
~/.cargo/registry
60+
~/.cargo/git
61+
target
62+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
63+
64+
- name: Build
65+
run: cargo build --profile ${{ inputs.build-target }}
66+
67+
- name: Run tests
68+
run: cargo test --profile ${{ inputs.build-target }}
69+
70+
- name: Set build success output
71+
id: set-output
72+
run: echo "build_success=true" >> $GITHUB_OUTPUT
73+
74+
- name: Upload artifact
75+
if: ${{ inputs.upload-artifact }}
76+
uses: actions/upload-artifact@v4
77+
with:
78+
name: ${{ inputs.artifact-name }}
79+
path: ${{ inputs.artifact-path }}
80+
81+
publish:
82+
needs: build
83+
if: ${{ inputs.publish-crates-io && needs.build.outputs.build_success == 'true' }}
84+
runs-on: ubuntu-latest
85+
steps:
86+
- name: Checkout repository
87+
uses: actions/checkout@v4
88+
89+
- name: Install Rust toolchain
90+
uses: actions-rs/toolchain@v1
91+
with:
92+
profile: minimal
93+
toolchain: ${{ inputs.rust-version }}
94+
override: true
95+
96+
- name: Login to crates.io
97+
run: cargo login ${{ secrets.CRATES_IO_TOKEN }}
98+
99+
- name: Package for crates.io
100+
run: cargo package
101+
102+
- name: Publish to crates.io
103+
run: cargo publish

rust-build/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Rust Build Workflow
2+
3+
A reusable GitHub Actions workflow for building, testing, and publishing Rust packages.
4+
5+
## Features
6+
7+
- Build and test Rust packages
8+
- Cache dependencies for faster builds
9+
- Publish packages to crates.io
10+
- Upload build artifacts
11+
12+
## Usage
13+
14+
```yaml
15+
name: Rust CI
16+
17+
on:
18+
push:
19+
branches: [ main ]
20+
pull_request:
21+
branches: [ main ]
22+
23+
jobs:
24+
build-and-test:
25+
uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main
26+
with:
27+
rust-version: 'stable'
28+
build-target: 'release'
29+
enable-cache: true
30+
upload-artifact: true
31+
artifact-name: 'my-rust-app'
32+
artifact-path: 'target/release/my-app'
33+
secrets:
34+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
35+
```
36+
37+
## Inputs
38+
39+
| Name | Description | Default | Required |
40+
|---------------------|----------------------------------------------------|-----------|-------------------------------------|
41+
| `rust-version` | Rust version to use | `stable` | No |
42+
| `build-target` | Cargo profile to use for building (debug, release) | `release` | No |
43+
| `enable-cache` | Enable caching of dependencies | `true` | No |
44+
| `publish-crates-io` | Publish package to crates.io | `false` | No |
45+
| `upload-artifact` | Upload build artifact | `false` | No |
46+
| `artifact-name` | Name of the artifact to upload | - | Only if `upload-artifact` is `true` |
47+
| `artifact-path` | Path to the artifact to upload | - | Only if `upload-artifact` is `true` |
48+
49+
## Secrets
50+
51+
| Name | Description | Required |
52+
|-------------------|-----------------------------------|---------------------------------------|
53+
| `CRATES_IO_TOKEN` | Token for publishing to crates.io | Only if `publish-crates-io` is `true` |
54+
55+
## Examples
56+
57+
### Basic Build and Test
58+
59+
```yaml
60+
jobs:
61+
build-and-test:
62+
uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main
63+
```
64+
65+
### Build, Test, and Upload Artifact
66+
67+
```yaml
68+
jobs:
69+
build-and-test:
70+
uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main
71+
with:
72+
upload-artifact: true
73+
artifact-name: 'my-rust-app'
74+
artifact-path: 'target/release/my-app'
75+
```
76+
77+
### Build, Test, and Publish to crates.io
78+
79+
```yaml
80+
jobs:
81+
build-and-publish:
82+
uses: your-org/github-actions-workflows/.github/workflows/rust-build.yml@main
83+
with:
84+
publish-crates-io: true
85+
secrets:
86+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
87+
```

0 commit comments

Comments
 (0)