Skip to content

Commit a875d5c

Browse files
feat(rust-build): add reusable workflow (#44)
* feat(rust-build): add reusable workflow * docs(rust-build): update workflow references to iExecBlockchainComputing
1 parent f7cf44d commit a875d5c

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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: [pull_request]
18+
19+
jobs:
20+
build-and-test:
21+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
22+
with:
23+
rust-version: 'stable'
24+
build-target: 'release'
25+
enable-cache: true
26+
upload-artifact: true
27+
artifact-name: 'my-rust-app'
28+
artifact-path: 'target/release/my-app'
29+
```
30+
31+
## Inputs
32+
33+
| Name | Description | Default | Required |
34+
|---------------------|----------------------------------------------------|-----------|-------------------------------------|
35+
| `rust-version` | Rust version to use | `stable` | No |
36+
| `build-target` | Cargo profile to use for building (debug, release) | `release` | No |
37+
| `enable-cache` | Enable caching of dependencies | `true` | No |
38+
| `publish-crates-io` | Publish package to crates.io | `false` | No |
39+
| `upload-artifact` | Upload build artifact | `false` | No |
40+
| `artifact-name` | Name of the artifact to upload | - | Only if `upload-artifact` is `true` |
41+
| `artifact-path` | Path to the artifact to upload | - | Only if `upload-artifact` is `true` |
42+
43+
## Secrets
44+
45+
| Name | Description | Required |
46+
|-------------------|-----------------------------------|---------------------------------------|
47+
| `CRATES_IO_TOKEN` | Token for publishing to crates.io | Only if `publish-crates-io` is `true` |
48+
49+
## Examples
50+
51+
### Basic Build and Test
52+
53+
```yaml
54+
jobs:
55+
build-and-test:
56+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
57+
```
58+
59+
### Build, Test, and Upload Artifact
60+
61+
```yaml
62+
jobs:
63+
build-and-test:
64+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
65+
with:
66+
upload-artifact: true
67+
artifact-name: 'my-rust-app'
68+
artifact-path: 'target/release/my-app'
69+
```
70+
71+
### Build, Test, and Publish to crates.io
72+
73+
```yaml
74+
jobs:
75+
build-and-publish:
76+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
77+
with:
78+
publish-crates-io: true
79+
secrets:
80+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
81+
```

0 commit comments

Comments
 (0)