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
0 commit comments