Search code examples
dockergitlab-cidocker-in-dockerearthly

How to save an artifact locally using earthly and Gitlab CI/CD


I have an Earthfile with the following part running unit tests and generating a code coverage file (to be used in the next job running sonar scanner) :

tests:
  ARG CI_PROJECT_DIR
  FROM +copy-src --STAGE=tests
  COPY tests /api/tests
  COPY package.json package-lock.json /api
  COPY tests/config.js /api/config.js
  COPY tests/env.js /api/env.js
  RUN git init .
  RUN npm ci --force
  RUN DEBUG="app-*:error" npx jest --coverage --coverageDirectory=/api/tests/coverage/ tests
  SAVE ARTIFACT /api/tests/coverage/lcov.info AS LOCAL ${CI_PROJECT_DIR}/lcov.info

And then I have the following config in my .gitlab-ci.yml file :

unit_tests:
    before_script:
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    stage: test
    services:
      - docker:dind
    variables:
        DOCKER_HOST: tcp://docker:2375
        FORCE_COLOR: 1
        EARTHLY_EXEC_CMD: "/bin/sh"
    image: earthly/earthly:v0.8.3
    script:
        - earthly --ci +tests --CI_PROJECT_DIR=${CI_PROJECT_DIR}
        - pwd
        - ls -al
    rules:
        - if: $CI_COMMIT_REF_NAME == 'main' || $CI_COMMIT_REF_NAME == 'master' || $CI_COMMIT_REF_NAME == 'pulse-recette' || $CI_PIPELINE_SOURCE == 'merge_request_event'
    allow_failure: false
    artifacts:
        paths:
            - lcov.info
        expire_in: 1 hour

Running earthly +tests on my computer generates the lcov.info on the host (my computer) file, but in Gitlab ci it's like it cannot save it in the host. I tried with the CI_PROJECT_DIR option and without, the ls command run in the ci always show the git repo content, without lcov.info file. Am I missing something ?

Thanks


Solution

  • In target mode, --ci is equivalent to --no-output --strict which means that Earthly won't output any images or artifacts.

    The reason for that is CI environments are typically ephemeral, and outputting artifacts locally would be a waste of IO in most of the cases.

    Use just --strict instead to get that artifact generated