rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.86k stars 2.43k forks source link

Expand "CI best practicies" section to the guide #5656

Open matklad opened 6 years ago

matklad commented 6 years ago

Things to talk about:

Ideally, the section should contain copy-pastable config files for various CI providers.

matklad commented 6 years ago

A blog post about this feature: https://blog.illicitonion.com/2018/06/rust-minimum-versions-semver-is-lie.html

ehuss commented 6 years ago

I assume this means to expand https://github.com/rust-lang/cargo/blob/master/src/doc/src/guide/continuous-integration.md? Should it maybe leverage or at least point to japaric/trust?

matklad commented 6 years ago

Ah, I haven't realized that https://github.com/rust-lang/cargo/blob/master/src/doc/src/guide/continuous-integration.md exists.

Should it maybe leverage or at least point to japaric/trust?

Yep, we should definitely mention it!

Eh2406 commented 6 years ago

@ehuss That is a good place for it to go, thanks for the reminder!

The impetus for this is that when we stabilize --minimal-versions we want to craft a message about how to use it. Specifically as a small part of a thorough CI setup. Witch will just lead everyone to ask, "What showed the big parts of the CI setup be?" So we thought we should write up our opinion before we stabilize --minimal-versions.

withoutboats commented 6 years ago

Relevant: rust-lang/rfcs#2483

aturon commented 6 years ago

I've just posted a blog post that talks about version selection in detail, and touches on the questions here as well.

Eh2406 commented 6 years ago

That is an excellent read. Thank you for writing this up so articulately. I especially liked the paragraph of:

In the long run, it could even make sense to combine the two approaches, allowing crates to state their toolchain requirements (and have that influence resolution), but encourage core crates to state “LTS” as their requirement.

That sounds like the tradeoff bending I know and love from the rust ethic. I.E. a careful and pedantic system to ensure you are correct but with a large pit of success to make it usable. It also involves the most design work, so it may not be feasible.

In our earlier discussion today you suggested that "equality constraints are rare in the ecosystem" and I resisted being pedantic and pointing out lock files. So I was glad to see them addressed in your blog post. But I do have one nit with:

Similarly, when dependencies are subsequently adjusted, Cargo will “unlock” the affected dependencies and again choose the maximum version.

Currently that adjustment is very conservative, if the version of the sub dependency that appears in the lock file satisfies the requirement of the new dependency then it is not unlocked. Meaning that I am often testing a combination not in the well tested set. So if the new dependency does not have lower-bound precision and my lockfile is old enough then I will brake.

Thanks for the thought provoking guidance of this community.

Nemo157 commented 6 years ago

There's also the https://crate-ci.github.io/ project (cc @epage). Maybe the Cargo Guide could have a small CI best practices section and link out to a larger exploration of the area like this?

withoutboats commented 6 years ago

In our earlier discussion today you suggested that "equality constraints are rare in the ecosystem" and I resisted being pedantic and pointing out lock files.

The important difference with lock files is that you once you have one you should have a successful build, and because it was generated using max versions, it will not run into the too low min version problem. Ceiling'd constraints are a problem because they influence version resolution, whereas the lockfile is after version resolution.

You do identify exactly where a problem can arise, though: cargo update -p. It might be worth considering changing the behavior of cargo update -p to update the entire subgraph under the crate you're updating.

dwijnand commented 6 years ago

More blog posts: https://levans.fr/rust_travis_cache.html (https://www.reddit.com/r/rust/comments/9d7sax/beware_the_rust_cache_on_travis_or_why_you_should/)

Turbo87 commented 5 years ago

Currently that adjustment is very conservative, if the version of the sub dependency that appears in the lock file satisfies the requirement of the new dependency then it is not unlocked. Meaning that I am often testing a combination not in the well tested set. So if the new dependency does not have lower-bound precision and my lockfile is old enough then I will brake.

I've run into this problem quite a few times lately. I'm using dependabot on some of my projects to automatically update dependencies, and the updates regularly break CI because the lockfile has an older transitive dependency, but some other dependency relies on it being the most recent release.

Examples:

It’s possible that we will eventually have workflows that depend on the accuracy of lower bounds in Cargo.toml. At the moment, however, this is purely speculative; the Cargo team does not have any ready examples.

Hopefully the examples above are helpful in that regard

mathstuf commented 5 years ago

I've been working to improve CI times on crates I work on here. There's gitlab-ci examples there (minimum version, stable, nightly, with/without feature flags, -Z minimal-versions, and, because it affects us, testing against git master). It handles caching and artifacting between steps. The most comprehensive one I have is for git-checks.

For Cirrus CI, I have keyutils. Though it runs the -Z minimal-versions under nightly rather than the minimum compiler version.

Basic strategy for "optimal" builds (AFAICT):

trevordmiller commented 4 years ago

I recently added CI / CD for a Rust project and after a good amount of research this is what I ended up with (specific to GitHub Actions), in case it is helpful for this issue:

Run CI when pushing to branches for pull requeests

name: Pull request
on:
  push:
    branches-ignore:
      - master
jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Check
      run: cargo check
    - name: Test
      run: cargo test
    - name: Lint
      run: cargo clippy --all-targets -- -D warnings
    - name: Format
      run: cargo fmt -- --check
    - name: Publish
      run: cargo publish --dry-run

Run CI and CD when merging pull requests to master

name: Merge
on:
  push:
    branches:
      - master
jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Check
      run: cargo check
    - name: Test
      run: cargo test
    - name: Lint
      run: cargo clippy --all-targets -- -D warnings
    - name: Format
      run: cargo fmt -- --check
  executable:
    runs-on: ubuntu-latest
    needs: [verify]
    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Login
      run: cargo login ${{ secrets.CRATE_REGISTRY_PAT }}
    - name: Publish
      if: success()
      run: cargo publish
mathstuf commented 4 years ago

So my feedback on this:

4ydan commented 1 year ago

To reduce our pipeline time I have come up with following testing strategy: parallel cargo doc, build, test and check does that seem reasonable to you?

cargo check should also be separate (building will have to do it anyways, so if you want the faster results, I'd just do that)

When building is already doing cargo check can we just skip it or did I get that wrong?

epage commented 1 year ago

imo cargo check and cargo build are mutually exclusive unless you want the artifact.

Similarly, cargo check and cargo test` may be mutually exclusive, depending on your needs.

My CI tends to be broken down into

epage commented 1 year ago

12382 documented a way to verify latest dependencies.

13056 documents a way to verify MSRV

Holes I still see

briansmith commented 1 year ago

IMO, this project (cargo) isn't the place to document CI best practices for Rust projects, even ones that use Cargo. I do think that such guides need to exist but why burden the cargo project with maintaining these docs?

epage commented 1 year ago

Because there isn't a better place at this time?

rursprung commented 1 year ago

thanks for working on this!

would it be an option to provide a (set of) GitHub Workflows which contain these best practices (where possible)? then users of other CIs can essentially copy whatever is being done there (with the GH Workflows being the canonical reference for the best practices at that time).

as workflows are versioned (semver) and come with documentation that might be a good way of rolling out new patterns over time (and if users have dependabot enabled for the workflows then they'll also notice if a new version comes around)

mathstuf commented 1 year ago

Things I do in my Rust CI (though on GitLab-CI; the steps might be useful at least) that aren't mentioned here:

epage commented 1 year ago

would it be an option to provide a (set of) GitHub Workflows which contain these best practices (where possible)?

For what we've included so far, we list a lot of trade offs so there isn't a single right solution.

Personally, I also am unsure of the value of providing an Action over people just calling the commands directly. The exception is I have a specific workflow in my own projects for clippy that leverages SARIF reporting in github.

  • do generate-lockfile once per resolution ("standard", lock-file, -Zminimal-versions, etc.)
  • pull the crates and cache them
  • everything else does --frozen --locked to avoid skew between jobs if an index update comes in during the pipeline run
  • rather than doing "artifact cache"-friendly things like no incremental and no debuginfo, use sccache instead (though probably not better with cloud-based CI; we have our CI all on local machines with a Redis server we use for compile caching)

Some of this gets in the question of how much we talk about principles vs CI specific practices (caching for that one CI provider).

  • cargo audit each dependency resolution

One question would be cargo deny vs cargo audit and whether we feel rustsec is GA enough for us to advertise

  • cargo tarpaulin for coverage

There are multiple solutions. We'd need to at least reference the main ones even if we point to one

  • cargo semver-checks

imo this isn't GA enough for us to include yet.