Skip to content

Commit 96a0dda

Browse files
feat: add support for artifact upload and crates.io publishing in Rust workflow
1 parent e791f08 commit 96a0dda

File tree

2 files changed

+100
-14
lines changed

2 files changed

+100
-14
lines changed

.github/workflows/rust-build.yml

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,32 @@ on:
1919
description: 'Enable caching of dependencies'
2020
default: true
2121
type: boolean
22+
publish-crates-io:
23+
description: 'Publish package to crates.io'
24+
default: false
25+
type: boolean
26+
upload-artifact:
27+
description: 'Upload build artifact'
28+
default: false
29+
type: boolean
30+
artifact-name:
31+
description: 'Name of the artifact to upload'
32+
type: string
33+
required: false
34+
artifact-path:
35+
description: 'Path to the artifact to upload'
36+
type: string
37+
required: false
38+
secrets:
39+
CRATES_IO_TOKEN:
40+
required: false
2241

2342
env:
2443
CARGO_TERM_COLOR: always
2544

2645
jobs:
27-
build-and-test:
28-
name: Build & Test
46+
build:
47+
name: build
2948
runs-on: ubuntu-latest
3049
steps:
3150
- name: Checkout repository
@@ -70,3 +89,30 @@ jobs:
7089

7190
- name: Run tests
7291
run: cargo test --profile ${{ inputs.build-profile }}
92+
93+
- name: Upload artifact
94+
if: ${{ inputs.upload-artifact }}
95+
uses: actions/upload-artifact@v4
96+
with:
97+
name: ${{ inputs.artifact-name }}
98+
path: ${{ inputs.artifact-path }}
99+
100+
publish:
101+
needs: build
102+
if: ${{ inputs.publish-crates-io && needs.build.result == 'success' }}
103+
runs-on: ubuntu-latest
104+
steps:
105+
- name: Checkout repository
106+
uses: actions/checkout@v4
107+
108+
- name: Install Rust toolchain
109+
uses: dtolnay/rust-toolchain@stable
110+
with:
111+
toolchain: ${{ inputs.rust-version }}
112+
components: cargo
113+
114+
- name: Validate package
115+
run: cargo package --allow-dirty
116+
117+
- name: Publish to crates.io
118+
run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}

rust-build/README.md

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# Rust Build and Test Workflow
1+
# Rust Build Workflow
22

3-
A reusable GitHub Actions workflow for building, linting, testing, and auditing Rust packages.
3+
A reusable GitHub Actions workflow for building, linting, testing, and auditing Rust packages, with optional artifact upload and crates.io publishing.
44

55
## Features
66

77
- Build and test Rust packages
8-
- Lint code using clippy
9-
- Check formatting with cargo fmt
10-
- Run security audits with cargo audit
8+
- Lint code using `clippy`
9+
- Check formatting with `cargo fmt`
10+
- Run security audits with `cargo audit`
1111
- Cache dependencies for faster builds
12+
- Upload build artifacts
13+
- Publish to crates.io
1214

1315
## Usage
1416

@@ -25,16 +27,31 @@ jobs:
2527
build-profile: 'release'
2628
run-audit: true
2729
enable-cache: true
30+
upload-artifact: true
31+
artifact-name: my-crate
32+
artifact-path: target/release/my-crate
33+
secrets:
34+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
2835
```
2936
3037
## Inputs
3138
32-
| Name | Description | Default | Required |
33-
| --------------- | ---------------------------------------------- | --------- | -------- |
34-
| `rust-version` | Rust version to use | `stable` | No |
35-
| `build-profile` | Cargo profile to use (debug, release) | `release` | No |
36-
| `run-audit` | Run `cargo audit` for security vulnerabilities | `true` | No |
37-
| `enable-cache` | Enable caching of dependencies | `true` | No |
39+
| Name | Description | Default | Required |
40+
| ------------------- | ------------------------------------------------------------ | --------- | -------- |
41+
| `rust-version` | Rust version to use | `stable` | No |
42+
| `build-profile` | Cargo profile to use for building (`debug`, `release`, etc.) | `release` | No |
43+
| `run-audit` | Run `cargo audit` for security vulnerabilities | `true` | No |
44+
| `enable-cache` | Enable caching of dependencies | `true` | No |
45+
| `upload-artifact` | Upload a build artifact after building | `false` | No |
46+
| `artifact-name` | Name of the artifact to upload | – | No |
47+
| `artifact-path` | Path to the artifact to upload | – | No |
48+
| `publish-crates-io` | Publish the package to crates.io (only if build succeeds) | `false` | No |
49+
50+
## Secrets
51+
52+
| Name | Description | Required |
53+
| ----------------- | --------------------------------------- | ------------------------------------- |
54+
| `CRATES_IO_TOKEN` | crates.io API token for `cargo publish` | Only if `publish-crates-io` is `true` |
3855

3956
## Examples
4057

@@ -60,9 +77,32 @@ jobs:
6077

6178
```yaml
6279
jobs:
63-
jobs:
6480
build-and-test:
6581
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
6682
with:
6783
build-profile: 'debug'
84+
```
85+
86+
### Upload Artifact After Build
87+
88+
```yaml
89+
jobs:
90+
build-and-test:
91+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
92+
with:
93+
upload-artifact: true
94+
artifact-name: my-crate
95+
artifact-path: target/release/my-crate
96+
```
97+
98+
### Publish to crates.io (requires CRATES_IO_TOKEN)
99+
100+
```yaml
101+
jobs:
102+
build-and-test:
103+
uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/rust-build.yml@main
104+
with:
105+
publish-crates-io: true
106+
secrets:
107+
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
68108
```

0 commit comments

Comments
 (0)