2

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?

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
al3x
  • 589
  • 1
  • 4
  • 16

2 Answers2

1

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/.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • 3
    "how to get cargo install to use that cache or the saved artifacts." — Try `CARGO_TARGET_DIR=$(pwd)/target cargo install wasm-pack` so that the build files are put in the same directory as the rest of what you're caching. That helped for my (GitHub) CI cache setup. – Kevin Reid Feb 04 '22 at 16:50
1

Make sure to cache $CARGO_HOME (downloaded sources) and $CARGO_TARGET_DIR (built targets).

Note that you need to add $CARGO_HOME/bin to your $PATH or call binaries from there directly.

Your example would look as follows:

pages:
  stage: deploy
  variables:
    CARGO_HOME: .cargo
    CARGO_TARGET_DIR: cargo-install
  cache:
    paths:
      - .cargo
      - cargo-install
  script:
    - rustup target add wasm32-unknown-unknown
    - cargo install wasm-pack
    - .cargo/bin/wasm-pack build --target web
    - mkdir public
    - mv ./pkg ./public/pkg
    - cp ./index.html ./public/index.html
  artifacts:
    paths:
      - public
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
darkdragon
  • 392
  • 5
  • 13