问题
I'm using the following .gitlab-ci.yml
file for setting up gitlab-CI for a Haskell Stack project created with stack new actividad3 --resolver=lts-14.6
.
image: haskell:8.6.5
cache:
paths:
- .stack
- .stack-work
- target
test:
stage: test
script:
- ghc --version
- stack --system-ghc build
- stack test
Building and testing the project last almost 5 minutes. Most of the time is spent building the hspec library. Is there any way to cache the used libraries between pipeline runs?
Thanks in advance.
回答1:
Most important for proper caching of a stack project is saving all folders being involved:
- Project work directory (or many directories if it is a multi-package setup), usually
.stack-work
- Global stack directory, usually
~/.stack
- Possibly a separate folder with binaries (ghc, ghc-pkg, ...)
These can vary between the operating systems and customized with environment variables, but can be easily discovered by asking stack
itself. Run stack path
within a stack project and you'll see all of the paths that stack might care about. These are the ones you'll need to cache and restore on CI in order to prevent recompilation:
stack path --stack-root
.stack-work
directories in all of the packages within the project (paths frompackages
instack.yaml
)- on Windows
stack path --programs
Just in case, if you want to see how this stuff can be derived programmatically from Haskell itself, you can find it here
I wrote this tool called cache-s3 a while back that allows you to use an AWS S3 bucket as cache for your CI and it has separate mode that will save and restore all of the stack related directories. This is probably an overkill for a simple project, so the gitlab's caching mechanism will likely be sufficient, but in case you need it is an option.
回答2:
Add these parts to your .gitlab-ci.yml
:
variables:
STACK_ROOT: "${CI_PROJECT_DIR}/.stack-root"
cache:
paths:
- .stack-work/
- .stack-root/
The $STACK_ROOT
env variable changes the folder stack uses for it's global files. This is required because GitLab CI can only cache files under the project folder, so caching $HOME/.stack
, ~/.stack
or /root/.stack
won't work.
A few relevant references from the web:
- http://blog.braulio.me/2018/10/24/use-haskell-stack-gitlab-ci.html;
- https://vadosware.io/post/zero-to-continuous-integrated-testing-a-haskell-project-with-gitlab/
- https://dev.to/drbearhands/haskell-for-madmen-setup-4cj9
- https://github.com/bitemyapp/haskell-continuous-integration/blob/master/.gitlab-ci.yml
来源:https://stackoverflow.com/questions/58017051/gitlab-ci-for-a-haskell-stack-project-how-to-cache-built-libraries