Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1Add the orbs stanza below your version, invoking the orb:
1
2
orbs:
rust: circleci/rust@1.8.0Use rust elements in your existing workflows and jobs.
Check linting with Clippy, run any present tests then build the crate.
1
2
3
4
5
6
7
8
version: '2.1'
orbs:
rust: circleci/rust@x.y.z
workflows:
production:
jobs:
- rust/lint-test-build:
release: true
Check linting with Clippy, run any present tests then build the crate.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
cache_version Cache version to use - increment this to build a fresh cache. | Cache version to use - increment this to build a fresh cache. | No | v1 type: string | string |
clippy_arguments Arguments to pass to cargo run clippy. | Arguments to pass to cargo run clippy. | No | '' type: string | string |
release Whether to build the binary for release or debug. | Whether to build the binary for release or debug. | No | false type: boolean | boolean |
version Version of Rust executor to utilize. | Version of Rust executor to utilize. | No | 1.49.0 type: string | string |
with_cache Whether to restore and save the cache or not - set to no if running multiple commands in one job. | Whether to restore and save the cache or not - set to no if running multiple commands in one job. | No | true type: boolean | boolean |
working_directory Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | No | ~/project type: string | string |
Build a Rust crate.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
cache_version Cache version to use - increment this to build a fresh cache. | Cache version to use - increment this to build a fresh cache. | No | v1 type: string | string |
crate Directory of the create to build. | Directory of the create to build. | No | '' type: string | string |
release Whether to build the binary for release or debug. | Whether to build the binary for release or debug. | No | false type: boolean | boolean |
verbose When true does a verbose build, i.e. -vv | When true does a verbose build, i.e. -vv | No | false type: boolean | boolean |
with_cache Whether to restore and save the cache or not - set to no if running multiple commands in one job. | Whether to restore and save the cache or not - set to no if running multiple commands in one job. | No | true type: boolean | boolean |
working_directory Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | No | ~/project type: string | string |
Run a cargo package.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
cache_version Cache version to use - increment this to build a fresh cache. | Cache version to use - increment this to build a fresh cache. | No | v1 type: string | string |
package Package to run. | Package to run. | No | '' type: string | string |
with_cache Whether to restore and save the cache or not - set to no if running multiple commands in one job. | Whether to restore and save the cache or not - set to no if running multiple commands in one job. | No | true type: boolean | boolean |
working_directory Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | No | ~/project type: string | string |
Run Clippy against your codebase. Learn more: https://github.com/rust-lang/rust-clippy.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
cache_version Cache version to use - increment this to build a fresh cache. | Cache version to use - increment this to build a fresh cache. | No | v1 type: string | string |
flags Additional flags to pass along to Clippy. | Additional flags to pass along to Clippy. | No | '--all --all-targets' type: string | string |
with_cache Whether to restore and save the cache or not - set to no if running multiple commands in one job. | Whether to restore and save the cache or not - set to no if running multiple commands in one job. | No | true type: boolean | boolean |
working_directory Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | No | ~/project type: string | string |
Validate formatting of Rust project.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
cache_version Cache version to use - increment this to build a fresh cache. | Cache version to use - increment this to build a fresh cache. | No | v1 type: string | string |
nightly-toolchain Whether or not to use the nightly toolchain version. | Whether or not to use the nightly toolchain version. | No | false type: boolean | boolean |
with_cache Whether to restore and save the cache or not - set to no if running multiple commands in one job. | Whether to restore and save the cache or not - set to no if running multiple commands in one job. | No | true type: boolean | boolean |
working_directory Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | No | ~/project type: string | string |
Install Rustup, Cargo and Rust. Note: it is best practice to leverage a Docker image with Rust preinstalled for reproducibility and speed.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
version Version of Rust to install and default to. | Version of Rust to install and default to. | No | stable type: string | string |
Test a cargo crate or package.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
cache_version Cache version to use - increment this to build a fresh cache. | Cache version to use - increment this to build a fresh cache. | No | v1 type: string | string |
package Package to test. | Package to test. | No | '' type: string | string |
with_cache Whether to restore and save the cache or not - set to no if running multiple commands in one job. | Whether to restore and save the cache or not - set to no if running multiple commands in one job. | No | true type: boolean | boolean |
working_directory Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root. | No | ~/project type: string | string |
Default Rust executor.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
tag Tag of the Rust image to use. | Tag of the Rust image to use. | No | 1.49.0 type: string | string |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Easily test, build, run and validate your Rust applications on CircleCI!
display:
home_url: https://www.rust-lang.org
source_url: https://github.com/CircleCI-Public/rust-orb
commands:
build:
description: |
Build a Rust crate.
parameters:
cache_version:
default: v1
description: Cache version to use - increment this to build a fresh cache.
type: string
crate:
default: ""
description: Directory of the create to build.
type: string
release:
default: false
description: Whether to build the binary for release or debug.
type: boolean
verbose:
default: false
description: When true does a verbose build, i.e. -vv
type: boolean
with_cache:
default: true
description: Whether to restore and save the cache or not - set to no if running multiple commands in one job.
type: boolean
working_directory:
default: ~/project
description: Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root.
type: string
steps:
- when:
condition: <<parameters.with_cache>>
steps:
- restore_cache:
keys:
- cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -d "/tmp/cargo_tmp" ]; then
echo "Cache directory found"
else
echo "Cache directory not found"
exit 0
fi
if [ -n "$CARGO_HOME" ]; then
rm -r "$(dirname "$CARGO_HOME")/.cargo"
cp -r /tmp/cargo_tmp/.cargo "$(dirname "$CARGO_HOME")/.cargo"
else
rm -r ~/.cargo
cp -r /tmp/cargo_tmp/.cargo ~/.cargo
fi
set +x
name: Process cache restore
- run:
command: |
#!/usr/bin/env bash
if [ "$ORB_VAL_RELEASE" = 1 ]; then
set -- "$@" --release
fi
if [ "$ORB_VAL_VERBOSE" = 1 ]; then
set -- "$@" -vv
fi
cargo build "$@"
environment:
ORB_VAL_CRATE_DIR: <<parameters.crate>>
ORB_VAL_RELEASE: <<parameters.release>>
ORB_VAL_VERBOSE: <<parameters.verbose>>
name: Cargo Build <<parameters.crate>>
working_directory: <<parameters.working_directory>>
- when:
condition: <<parameters.with_cache>>
steps:
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -n "$CARGO_HOME" ]; then
mkdir -p /tmp/cargo_tmp
cp -r "$CARGO_HOME/*" /tmp/cargo_tmp/
else
mkdir -p /tmp/cargo_tmp
cp -r ~/.cargo /tmp/cargo_tmp/
fi
set +x
name: Process cache save
- save_cache:
key: cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
paths:
- /tmp/cargo_tmp/
cargo-run:
description: |
Run a cargo package.
parameters:
cache_version:
default: v1
description: Cache version to use - increment this to build a fresh cache.
type: string
package:
default: ""
description: Package to run.
type: string
with_cache:
default: true
description: Whether to restore and save the cache or not - set to no if running multiple commands in one job.
type: boolean
working_directory:
default: ~/project
description: Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root.
type: string
steps:
- when:
condition: <<parameters.with_cache>>
steps:
- restore_cache:
keys:
- cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -d "/tmp/cargo_tmp" ]; then
echo "Cache directory found"
else
echo "Cache directory not found"
exit 0
fi
if [ -n "$CARGO_HOME" ]; then
rm -r "$(dirname "$CARGO_HOME")/.cargo"
cp -r /tmp/cargo_tmp/.cargo "$(dirname "$CARGO_HOME")/.cargo"
else
rm -r ~/.cargo
cp -r /tmp/cargo_tmp/.cargo ~/.cargo
fi
set +x
name: Process cache restore
- run:
command: cargo run <<parameters.package>>
name: Cargo Run <<parameters.package>>
working_directory: <<parameters.working_directory>>
- when:
condition: <<parameters.with_cache>>
steps:
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -n "$CARGO_HOME" ]; then
mkdir -p /tmp/cargo_tmp
cp -r "$CARGO_HOME/*" /tmp/cargo_tmp/
else
mkdir -p /tmp/cargo_tmp
cp -r ~/.cargo /tmp/cargo_tmp/
fi
set +x
name: Process cache save
- save_cache:
key: cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
paths:
- /tmp/cargo_tmp/
clippy:
description: |
Run Clippy against your codebase. Learn more: https://github.com/rust-lang/rust-clippy.
parameters:
cache_version:
default: v1
description: Cache version to use - increment this to build a fresh cache.
type: string
flags:
default: --all --all-targets
description: Additional flags to pass along to Clippy.
type: string
with_cache:
default: true
description: Whether to restore and save the cache or not - set to no if running multiple commands in one job.
type: boolean
working_directory:
default: ~/project
description: Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root.
type: string
steps:
- when:
condition: <<parameters.with_cache>>
steps:
- restore_cache:
keys:
- cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -d "/tmp/cargo_tmp" ]; then
echo "Cache directory found"
else
echo "Cache directory not found"
exit 0
fi
if [ -n "$CARGO_HOME" ]; then
rm -r "$(dirname "$CARGO_HOME")/.cargo"
cp -r /tmp/cargo_tmp/.cargo "$(dirname "$CARGO_HOME")/.cargo"
else
rm -r ~/.cargo
cp -r /tmp/cargo_tmp/.cargo ~/.cargo
fi
set +x
name: Process cache restore
- run:
command: rustup component add clippy
name: Install Clippy
- run:
command: cargo clippy <<parameters.flags>>
name: Run Clippy
working_directory: <<parameters.working_directory>>
- when:
condition: <<parameters.with_cache>>
steps:
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -n "$CARGO_HOME" ]; then
mkdir -p /tmp/cargo_tmp
cp -r "$CARGO_HOME/*" /tmp/cargo_tmp/
else
mkdir -p /tmp/cargo_tmp
cp -r ~/.cargo /tmp/cargo_tmp/
fi
set +x
name: Process cache save
- save_cache:
key: cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
paths:
- /tmp/cargo_tmp/
format:
description: |
Validate formatting of Rust project.
parameters:
cache_version:
default: v1
description: Cache version to use - increment this to build a fresh cache.
type: string
nightly-toolchain:
default: false
description: Whether or not to use the nightly toolchain version.
type: boolean
with_cache:
default: true
description: Whether to restore and save the cache or not - set to no if running multiple commands in one job.
type: boolean
working_directory:
default: ~/project
description: Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root.
type: string
steps:
- when:
condition: <<parameters.with_cache>>
steps:
- restore_cache:
keys:
- cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -d "/tmp/cargo_tmp" ]; then
echo "Cache directory found"
else
echo "Cache directory not found"
exit 0
fi
if [ -n "$CARGO_HOME" ]; then
rm -r "$(dirname "$CARGO_HOME")/.cargo"
cp -r /tmp/cargo_tmp/.cargo "$(dirname "$CARGO_HOME")/.cargo"
else
rm -r ~/.cargo
cp -r /tmp/cargo_tmp/.cargo ~/.cargo
fi
set +x
name: Process cache restore
- run:
command: |
#!/usr/bin/env bash
rustup_args=()
cargo_args=()
if [ "$ORB_VAL_NIGHTLY_TOOLCHAIN" = "true" ]; then
rustup_args+=(--toolchain nightly)
cargo_args+=(+nightly)
fi
rustup component add rustfmt "${rustup_args[@]}"
cargo "${cargo_args[@]}" fmt -- --check
environment:
ORB_VAL_NIGHTLY_TOOLCHAIN: <<parameters.nightly-toolchain>>
name: Install and Run fmt
working_directory: <<parameters.working_directory>>
- when:
condition: <<parameters.with_cache>>
steps:
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -n "$CARGO_HOME" ]; then
mkdir -p /tmp/cargo_tmp
cp -r "$CARGO_HOME/*" /tmp/cargo_tmp/
else
mkdir -p /tmp/cargo_tmp
cp -r ~/.cargo /tmp/cargo_tmp/
fi
set +x
name: Process cache save
- save_cache:
key: cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
paths:
- /tmp/cargo_tmp/
install:
description: |
Install Rustup, Cargo and Rust. Note: it is best practice to leverage a Docker image with Rust preinstalled for reproducibility and speed.
parameters:
version:
default: stable
description: Version of Rust to install and default to.
type: string
steps:
- run:
command: |
#!/usr/bin/env bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
-y --default-toolchain "$RUST_VERSION"
# shellcheck source=/dev/null
source "$HOME"/.cargo/env
environment:
RUST_VERSION: <<parameters.version>>
name: Install Rustup
- run:
command: echo 'source $HOME/.cargo/env' >> $BASH_ENV
name: Setting up Cargo environment in $BASH_ENV
test:
description: |
Test a cargo crate or package.
parameters:
cache_version:
default: v1
description: Cache version to use - increment this to build a fresh cache.
type: string
package:
default: ""
description: Package to test.
type: string
with_cache:
default: true
description: Whether to restore and save the cache or not - set to no if running multiple commands in one job.
type: boolean
working_directory:
default: ~/project
description: Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root.
type: string
steps:
- when:
condition: <<parameters.with_cache>>
steps:
- restore_cache:
keys:
- cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -d "/tmp/cargo_tmp" ]; then
echo "Cache directory found"
else
echo "Cache directory not found"
exit 0
fi
if [ -n "$CARGO_HOME" ]; then
rm -r "$(dirname "$CARGO_HOME")/.cargo"
cp -r /tmp/cargo_tmp/.cargo "$(dirname "$CARGO_HOME")/.cargo"
else
rm -r ~/.cargo
cp -r /tmp/cargo_tmp/.cargo ~/.cargo
fi
set +x
name: Process cache restore
- run:
command: cargo test <<parameters.package>>
name: Cargo Test <<parameters.package>>
working_directory: <<parameters.working_directory>>
- when:
condition: <<parameters.with_cache>>
steps:
- run:
command: |
#!/usr/bin/env bash
set -x
if [ -n "$CARGO_HOME" ]; then
mkdir -p /tmp/cargo_tmp
cp -r "$CARGO_HOME/*" /tmp/cargo_tmp/
else
mkdir -p /tmp/cargo_tmp
cp -r ~/.cargo /tmp/cargo_tmp/
fi
set +x
name: Process cache save
- save_cache:
key: cargo-<<parameters.cache_version>>-{{ checksum "<<parameters.working_directory>>/Cargo.lock" }}
paths:
- /tmp/cargo_tmp/
executors:
default:
description: |
Default Rust executor.
docker:
- image: cimg/rust:<<parameters.tag>>
parameters:
tag:
default: 1.49.0
description: Tag of the Rust image to use.
type: string
jobs:
lint-test-build:
description: |
Check linting with Clippy, run any present tests then build the crate.
executor:
name: default
tag: << parameters.version >>
parameters:
cache_version:
default: v1
description: Cache version to use - increment this to build a fresh cache.
type: string
clippy_arguments:
default: ""
description: Arguments to pass to cargo run clippy.
type: string
release:
default: false
description: Whether to build the binary for release or debug.
type: boolean
version:
default: 1.49.0
description: Version of Rust executor to utilize.
type: string
with_cache:
default: true
description: Whether to restore and save the cache or not - set to no if running multiple commands in one job.
type: boolean
working_directory:
default: ~/project
description: Path to the directory containing your Cargo.lock file. Not needed if Cargo.lock lives in the root.
type: string
steps:
- checkout:
path: /home/circleci/project
- when:
condition: <<parameters.with_cache>>
steps:
- restore_cache:
keys:
- cargo-<<parameters.cache_version>>-{{ checksum "Cargo.lock" }}
- clippy:
flags: <<parameters.clippy_arguments>>
with_cache: false
working_directory: <<parameters.working_directory>>
- test:
with_cache: false
working_directory: <<parameters.working_directory>>
- build:
release: <<parameters.release>>
with_cache: false
working_directory: <<parameters.working_directory>>
- when:
condition: <<parameters.with_cache>>
steps:
- save_cache:
key: cargo-<<parameters.cache_version>>-{{ checksum "Cargo.lock" }}
paths:
- ~/.cargo
working_directory: <<parameters.working_directory>>
examples:
lint_test_build_for_release:
description: Check linting with Clippy, run any present tests then build the crate.
usage:
version: "2.1"
orbs:
rust: circleci/rust@x.y.z
workflows:
production:
jobs:
- rust/lint-test-build:
release: true