Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GitLab CI - Rust setup
image: "rust:latest"
default:
before_script:
- rustc --version
- cargo --version
stages:
- test
test-code:
stage: test
script:
- cargo test
- cargo install cargo-tarpaulin
- cargo tarpaulin --ignore-tests
lint-code:
stage: test
script:
- rustup component add clippy
- cargo clippy -- -D warnings
format-code:
stage: test
script:
- rustup component add rustfmt
- cargo fmt -- --check
audit-code:
stage: test
script:
- cargo install cargo-audit
- cargo audit
@LukeMathWalker
Copy link
Author

LukeMathWalker commented Jun 7, 2020

Courtesy of Alan Faloon.

@ColmanHumphrey
Copy link

ColmanHumphrey commented Oct 2, 2020

This is very short and neat compared to the github actions version, which I assume is because the actions version has many more bells and whistles (scheduling, toolchain specification etc). Does anyone know how would a feature-matched gitlab-ci yaml file compare?

@LukeMathWalker
Copy link
Author

LukeMathWalker commented Oct 2, 2020

The big difference with the GitHub actions version is caching and parallelism - I am not a GitLab user, but I'd be happy to host a more optimised version if provided.

@w3irdrobot
Copy link

w3irdrobot commented Dec 29, 2020

This could, and probably should, be broken into separate steps instead of everything being done in a single step. At least attempt to parallelize things that could be done at the same time.

@LukeMathWalker
Copy link
Author

LukeMathWalker commented Dec 29, 2020

If anybody who knows GitLab CI wants to pitch in with a more optimised setup I am happy to edit the gist 😁

@devonh
Copy link

devonh commented Feb 6, 2021

Hey! To parallelize things you could update this to something like the following:

image: "rust:latest"

default:
  before_script:
    - rustc --version 
    - cargo --version

stages:
  - test

test-code:
  stage: test
  script:
    - cargo test
    - cargo install cargo-tarpaulin
    - cargo tarpaulin --ignore-tests

lint-code:
  stage: test
  script:
    - rustup component add rustfmt
    - cargo fmt -- --check
    - rustup component add clippy
    - cargo clippy -- -D warnings

audit-code:
  stage: test
  script:
    - cargo install cargo-audit
    - cargo audit

@LukeMathWalker
Copy link
Author

LukeMathWalker commented Feb 6, 2021

Thanks - updated @devonh!

@Valmirius
Copy link

Valmirius commented Jul 17, 2021

What else needs to be added to the Gitlab version to account for SQLX's compile time checks?

@wynro
Copy link

wynro commented Jan 1, 2022

To consider coverage in Merge Requests, the test-code job should be:

test-code:
  stage: test
  artifacts:
    when: always
    reports:
      cobertura: "cobertura.xml"
  coverage: '/\d+\.\d+% coverage, /'
  script:
    - cargo test
    - cargo install cargo-tarpaulin
    - cargo tarpaulin --ignore-tests --out Xml

image can be moved under the default part, to allow individual jobs to use different images (for example, if you want to compile in different Rust versions), leaving it like this:

default:
  image: rust:latest
  before_script:
    - rustc --version
    - cargo --version

To add caching, you can use the following on top of the file:

cache:
  paths:
    - target/

I don't know if Rust requires to cache any other directory, but you can easily add any other directory under the paths piece (or exclude directories if it may be a problem)

@Arevjensen
Copy link

Arevjensen commented Oct 12, 2022

Ran into:

$ cargo test
Compiling zero2prod v0.1.0 (/builds/SniffleEU/zero2prod)
error: linker clang not found = note: No such file or directory (os error 2)
error: could not compile zero2prod due to previous error
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1

In the test-code block.
As I've never tried gitlab CI before my workaround is crude, but changing from:

test-code:
  stage: test  
  script:  
    - cargo test
    - cargo install cargo-tarpaulin
    - cargo tarpaulin --ignore-tests

to

test-code:
  stage: test  
  script:  
    - apt update  
    - apt install lld clang -y
    - cargo test
    - cargo install cargo-tarpaulin
    - cargo tarpaulin --ignore-tests

Fixes the issue and runs the block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment