When does cargo build rebuild a crate? - rust

I have a workspace project with multiple crates in it. If I run cargo test --workspace twice in a row locally, it will only compile the crates the first time. But if I push a build to CI, and then push another build with just a dummy change to the README, it will rebuild the crates.
I'm using the following github actions ci config, i.e. it's doing caching:
- uses: actions/checkout#v3
- uses: dtolnay/rust-toolchain#stable
- uses: Leafwing-Studios/cargo-cache#v1
- name: Run tests
run: cargo test --workspace --release
I'm guessing that cargo build checks the timestamp of the files in the src directories, and since it's git cloning them out each time they're always "new", but I'm not sure if that's the case.

Related

Caching Rust/Wasm tools in Gitlab CI?

I'm working with Wasm and Rust, and I'm deploying the page with gitlab pages.
I'm using a gitlab-ci.yml file that looks like this:
image: "rust:latest"
variables:
PUBLIC_URL: "/repo-name"
pages:
stage: deploy
script:
- rustup target add wasm32-unknown-unknown
- cargo install wasm-pack
- wasm-pack build --target web
- mkdir public
- mv ./pkg ./public/pkg
- cp ./index.html ./public/index.html
artifacts:
paths:
- public
But even for a "Hello World" app, this takes ~12 minutes.
~11 minutes of that is taken by the cargo install wasm-pack step.
Is there any way I can cache the intermediate step, to avoid doing this every time?
This page: Caching in GitLab CI/CD talks about caching and/or using artifacts to persist files between jobs. You may be able to make use of that.
It then becomes a question of how to get cargo install to use that cache or the saved artifacts.
Alternatively, you can define your own base build image (run the cargo install steps in that), and store that in Gitlab's docker registry; see https://docs.gitlab.com/ee/user/packages/container_registry/.

How to store node modules between jobs and stages in gitlab with continuous integration

I am fairly new to GitLab CI and I've been trying different approaches to use the node_modules directory in my entire pipeline. From what I've read in the official docs, cache and artifacts seem to be valid approaches to pass on files between jobs:
cache is used to specify a list of files and directories which should
be cached between jobs. You can only use paths that are within the
project workspace.
However, my issue with the caching method is that the node_modules would be persisted between pipelines by default:
cache can be set globally and per-job.
from GitLab 9.0, caching is enabled and shared between pipelines and jobs by default.
I do not want to persist the node_modules between pipelines. What I actually want is to trigger a fresh install with npm in my setup stage and then allow all further jobs in the pipeline to use these modules. Hence, I started using artifacts instead of cache, which is described similarly:
artifacts is used to specify a list of files and directories which
should be attached to the job after success. [...]
The artifacts will be sent to GitLab after the job finishes
successfully and will be available for download in the GitLab UI.
The dependency feature should be used in conjunction with artifacts
and allows you to define the artifacts to pass between different jobs.
The artifact-dependency method seems to be usable in my case. However, both cache and artifacts are extremely inefficient and slow. The node_modules are installed and usable, but the entire directory then gets uploaded somewhere and is re-downloaded between each job. (I would really love to know what happens here... Where do the modules go?)
Is there a better approach to run npm install only once at the beginning of the pipeline and then keep the node_modules in the pipeline during its entire runtime? I do not want to keep the node_modules after all jobs are finished so they don't need to be uploaded or downloaded anywhere.
Sample pipeline configuration file to reproduce the behavior:
image: node:lts
stages:
- setup
- build
- test
node:
stage: setup
script:
- npm install
artifacts:
paths:
- node_modules/
build:
stage: build
script:
- npm run build
dependencies:
- node
test:
stage: test
script:
- npm run lint
- npm run test
dependencies:
- node
Where do the modules go?
By default artifacts are saved on the main gitlab machine:
/var/opt/gitlab/gitlab-rails/shared/artifacts
Is there a better approach to run npm install only once at the beginning of the pipeline and then keep the node_modules in the pipeline during its entire runtime?
There are some options that you can try:
Merge setup and build stages to one stage.
Local npm cache on builder machines. Faster npm install times. Or use private npm proxy registry (for example - Nexus/Artifactory)
Check if gitlab main machine and the builders are in the same network so the upload/download will be faster
Consider packaging your build in docker. You will get reusable docker images between your gitlab stages. (Of course that there is an overhead of uploading the images to docker registry)

Gitlab pipeline with external dependency

I have 2 maven projects hosted on Gitlab. Let's call them A and B. Project A depends on project B.
I want to use gitlab-ci to build A.
Here is the gitlab-ci.yml file for project B:
image: maven:3-jdk-8
build:
script: "mvn install -B"
What should the gitlab-ci in project A look like?
Use GIT SUBMODULES with your project A to refer to project B and then add
GIT_SUBMODULE_STRATEGY: recursive
to the gitlab-ci.yml file in project A. Further project B needs also a CI configuration file in the project root.
https://docs.gitlab.com/ce/ci/git_submodules.html

Add Source Code's Lint step to Gitlab CI stages

There's some way to add a source code linting step to a gitlab ci enabled project?
So as the stage fails if lint detects critical issues?
Of course it's possible.
Simply add a job that will run the linter of your choice. As long as your linter returns a non zero code when it finds an error, that will work.
Here's an example to add to your .gitlab-ci.yml file:
lint:
stage: test
script:
- linter src/

Gitlab CI: Cannot find output of build stage

I have my .gitlab-ci.yml file set up in the typical three stages: test, build, deploy. During the build stage, I run a command that compiles my project and puts it in a tarball. The build stage appears to execute successfully because it moves on to the deploy stage, but the deploy stage then says it can't find the tarball. Is it in another directory? What happened to it? Thanks.
For each test gitlab-ci clean the build folder, therefore the output files of the build stage are not available in the deploy stage.
You need to rebuild your project also in the deploy stage.
The "stages" are only useful to order your tests, i.e. avoid to try to do a deploy test if a build test failed.
EDIT:
Since Gitlab 8.6, it is possible using dependencies feature
I was surprised to see the same behaviour (on GitLab 8.4).
I use cmake to create makefiles, then make to build, and then make test to run the test. I run all these in a build/ directory.
I don't want to repeat myself and identify easily which steps are failing. As such, I've created different gitlab-ci stages: cmake, make, test, etc. I then tell gitlab-ci to keep the build directory using the cache option:
cache:
key: "$CI_BUILD_REF_NAME"
untracked: true
paths:
- build/
I think that the key option will keep the same build directory for all stages acting on the same branch. See the gitlab-ci doc here: http://doc.gitlab.com/ce/ci/yaml/README.html#cache
EDIT: Don't use the cache for this! GitLab implemented reusable artifacts between stages in 8.4: https://gitlab.com/gitlab-org/gitlab-ce/issues/3423
The CI runners will have to be adapted to support this. See: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/336

Resources