1- name : Build, Test and Publish Rust Package
1+ name : Build and Test Rust Package
22
33on :
44 workflow_call :
77 description : ' Rust version to use'
88 default : ' stable'
99 type : string
10- build-target :
10+ build-profile :
1111 description : ' Cargo profile to use for building (debug, release)'
1212 default : ' release'
1313 type : string
14+ run-audit :
15+ description : ' Run cargo-audit for security vulnerabilities'
16+ default : true
17+ type : boolean
1418 enable-cache :
1519 description : ' Enable caching of dependencies'
1620 default : true
1721 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
3722
23+ env :
24+ CARGO_TERM_COLOR : always
25+
3826jobs :
39- build :
27+ build-and-test :
28+ name : Build & Test
4029 runs-on : ubuntu-latest
41- outputs :
42- build_success : ${{ steps.set-output.outputs.build_success }}
4330 steps :
4431 - name : Checkout repository
4532 uses : actions/checkout@v4
4633
4734 - name : Install Rust toolchain
48- uses : actions-rs/ toolchain@v1
35+ uses : dtolnay/rust- toolchain@stable
4936 with :
50- profile : minimal
5137 toolchain : ${{ inputs.rust-version }}
52- override : true
38+ components : clippy
5339
5440 - name : Cache dependencies
5541 if : ${{ inputs.enable-cache }}
5642 uses : actions/cache@v4
5743 with :
5844 path : |
59- ~/.cargo/registry
45+ ~/.cargo/bin/
46+ ~/.cargo/registry/index/
47+ ~/.cargo/registry/cache/
6048 ~/.cargo/git
6149 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
50+ key : ${{ runner.os }}-cargo-${{ inputs.build-profile }}-${{ hashFiles('**/Cargo.lock') }}
51+ restore-keys : |
52+ ${{ runner.os }}-cargo-${{ inputs.build-profile }}-
7353
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 }}
54+ - name : Check formatting
55+ run : cargo fmt --all -- --check
8056
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
57+ - name : Run linter (Clippy)
58+ run : cargo clippy --all-targets -- -D warnings
59+
60+ - name : Install cargo-audit
61+ if : ${{ inputs.run-audit }}
62+ run : cargo install cargo-audit
8863
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 }}
64+ - name : Run security audit
65+ if : ${{ inputs.run-audit }}
66+ run : cargo audit
9867
99- - name : Package for crates.io
100- run : cargo package
68+ - name : Build
69+ run : cargo build --profile ${{ inputs.build-profile }}
10170
102- - name : Publish to crates.io
103- run : cargo publish
71+ - name : Run tests
72+ run : cargo test --profile ${{ inputs.build-profile }}
0 commit comments