Skip to content

Commit 3d74713

Browse files
feat(rust-build)!: enhance Rust workflow with linting and formatting (#79)
BREAKING CHANGE: enforce use of 'release' as build-target and remove artifact upload step.
1 parent 08eb8a4 commit 3d74713

File tree

2 files changed

+57
-81
lines changed

2 files changed

+57
-81
lines changed

.github/workflows/rust-build.yml

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ on:
77
description: 'Rust version to use'
88
default: 'stable'
99
type: string
10-
build-target:
11-
description: 'Cargo profile to use for building (debug, release)'
12-
default: 'release'
10+
working-directory:
11+
description: 'The directory to run jobs from'
12+
default: '.'
1313
type: string
1414
enable-cache:
1515
description: 'Enable caching of dependencies'
@@ -19,85 +19,62 @@ on:
1919
description: 'Publish package to crates.io'
2020
default: false
2121
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
3422
secrets:
35-
CRATES_IO_TOKEN:
23+
CARGO_REGISTRY_TOKEN:
3624
required: false
3725

26+
env:
27+
CARGO_TERM_COLOR: always
28+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
29+
3830
jobs:
39-
build:
31+
build_and_publish:
4032
runs-on: ubuntu-latest
41-
outputs:
42-
build_success: ${{ steps.set-output.outputs.build_success }}
4333
steps:
4434
- name: Checkout repository
4535
uses: actions/checkout@v4
4636

4737
- name: Install Rust toolchain
48-
uses: actions-rs/toolchain@v1
38+
uses: dtolnay/rust-toolchain@master
4939
with:
50-
profile: minimal
5140
toolchain: ${{ inputs.rust-version }}
52-
override: true
41+
components: clippy, rustfmt
5342

5443
- name: Cache dependencies
5544
if: ${{ inputs.enable-cache }}
5645
uses: actions/cache@v4
5746
with:
5847
path: |
48+
~/.cargo/bin/
5949
~/.cargo/registry
6050
~/.cargo/git
6151
target
62-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
52+
key: ${{ runner.os }}-cargo-release-${{ hashFiles('**/Cargo.lock') }}
53+
restore-keys: |
54+
${{ runner.os }}-cargo-release-
6355
64-
- name: Build
65-
run: cargo build --profile ${{ inputs.build-target }}
66-
67-
- name: Run tests
68-
run: cargo test --profile ${{ inputs.build-target }}
56+
- name: Check formatting
57+
working-directory: ${{ inputs.working-directory }}
58+
run: cargo fmt --all -- --check
6959

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 }}
60+
- name: Run linter (Clippy)
61+
working-directory: ${{ inputs.working-directory }}
62+
run: cargo clippy --all-targets -- -D warnings
8063

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
64+
- name: Build
65+
working-directory: ${{ inputs.working-directory }}
66+
run: cargo build --release
9567

96-
- name: Login to crates.io
97-
run: cargo login ${{ secrets.CRATES_IO_TOKEN }}
68+
- name: Run tests
69+
working-directory: ${{ inputs.working-directory }}
70+
run: cargo test --release
9871

99-
- name: Package for crates.io
72+
- name: Validate package
73+
if: ${{ inputs.publish-crates-io }}
74+
working-directory: ${{ inputs.working-directory }}
10075
run: cargo package
10176

10277
- name: Publish to crates.io
103-
run: cargo publish
78+
if: ${{ inputs.publish-crates-io }}
79+
working-directory: ${{ inputs.working-directory }}
80+
run: cargo publish

rust-build/README.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Rust Build Workflow
22

3-
A reusable GitHub Actions workflow for building, testing, and publishing Rust packages.
3+
A reusable GitHub Actions workflow for building, linting, testing, and publishing Rust packages, with optional dependency caching and working directory support.
44

55
## Features
66

77
- Build and test Rust packages
8+
- Lint code using `clippy`
9+
- Check formatting with `cargo fmt`
810
- Cache dependencies for faster builds
9-
- Publish packages to crates.io
10-
- Upload build artifacts
11+
- Set a working directory (for monorepos or nested crates)
12+
- Publish to crates.io
1113

1214
## Usage
1315

@@ -21,30 +23,29 @@ jobs:
2123
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
2224
with:
2325
rust-version: 'stable'
24-
build-target: 'release'
26+
working-directory: './my-crate'
2527
enable-cache: true
26-
upload-artifact: true
27-
artifact-name: 'my-rust-app'
28-
artifact-path: 'target/release/my-app'
28+
publish-crates-io: false
29+
secrets:
30+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
2931
```
3032
3133
## Inputs
3234
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` |
35+
| Name | Description | Default | Required |
36+
| ------------------- | --------------------------------------------------------- | -------- | -------- |
37+
| `rust-version` | Rust version to use | `stable` | No |
38+
| `working-directory` | The directory to run jobs from | `.` | No |
39+
| `enable-cache` | Enable caching of dependencies | `true` | No |
40+
| `publish-crates-io` | Publish the package to crates.io (only if build succeeds) | `false` | No |
41+
42+
Note: All builds use the release profile by default. There is no build-target input anymore
4243

4344
## Secrets
4445

45-
| Name | Description | Required |
46-
|-------------------|-----------------------------------|---------------------------------------|
47-
| `CRATES_IO_TOKEN` | Token for publishing to crates.io | Only if `publish-crates-io` is `true` |
46+
| Name | Description | Required |
47+
| ---------------------- | --------------------------------------- | ------------------------------------- |
48+
| `CARGO_REGISTRY_TOKEN` | crates.io API token for `cargo publish` | Only if `publish-crates-io` is `true` |
4849

4950
## Examples
5051

@@ -56,19 +57,17 @@ jobs:
5657
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
5758
```
5859

59-
### Build, Test, and Upload Artifact
60+
### Specify a Working Directory
6061

6162
```yaml
6263
jobs:
6364
build-and-test:
6465
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
6566
with:
66-
upload-artifact: true
67-
artifact-name: 'my-rust-app'
68-
artifact-path: 'target/release/my-app'
67+
working-directory: './my-crate'
6968
```
7069

71-
### Build, Test, and Publish to crates.io
70+
### Publish to crates.io (requires CARGO_REGISTRY_TOKEN)
7271

7372
```yaml
7473
jobs:
@@ -77,5 +76,5 @@ jobs:
7776
with:
7877
publish-crates-io: true
7978
secrets:
80-
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
79+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
8180
```

0 commit comments

Comments
 (0)