How can I pass artifacts to another stage?

GitlabGitlab Ci

Gitlab Problem Overview


I'd like to use GitLab CI with the .gitlab-ci.yml file to run different stages with separate scripts. The first stage produces a tool that must be used in a later stage to perform tests. I've declared the generated tool as artifact.

Now how can I execute that tool in a later stage job? What is the correct path, and what files will there be around it?

For example the first stage builds artifacts/bin/TestTool/TestTool.exe and that directory contains other required files (DLLs and others). My .gitlab-ci.yml file looks like this:

releasebuild:
  script:
    - chcp 65001
    - build.cmd
  stage: build
  artifacts:
    paths:
      - artifacts/bin/TestTool/

systemtests:
  script:
    - chcp 65001
    - WHAT TO WRITE HERE?
  stage: test

The build and tests run on Windows if that's relevant.

Gitlab Solutions


Solution 1 - Gitlab

Use dependencies. With this config test stage will download the untracked files that were created during the build stage:

build:
  stage: build
  artifacts:
    untracked: true
  script:
    - ./Build.ps1

test:
  stage: test
  dependencies: 
    - build
  script:
    - ./Test.ps1

Solution 2 - Gitlab

Since artifacts from all previous stages are passed by default, we just need to define stages in correct order. Please try the example below, which could help understanding.

image: ubuntu:18.04

stages:
  - build_stage
  - test_stage
  - deploy_stage
  
build:
  stage: build_stage
  script:
    - echo "building..." >> ./build_result.txt
  artifacts:
    paths:
    - build_result.txt
    expire_in: 1 week

unit_test:
  stage: test_stage
  script:
    - ls
    - cat build_result.txt
    - cp build_result.txt unittest_result.txt
    - echo "unit testing..." >> ./unittest_result.txt
  artifacts:
    paths:
    - unittest_result.txt
    expire_in: 1 week

integration_test:
  stage: test_stage
  script:
    - ls
    - cat build_result.txt
    - cp build_result.txt integration_test_result.txt
    - echo "integration testing..." >> ./integration_test_result.txt
  artifacts:
    paths:
    - integration_test_result.txt
    expire_in: 1 week

deploy:
  stage: deploy_stage
  script:
    - ls
    - cat build_result.txt
    - cat unittest_result.txt
    - cat integration_test_result.txt

enter image description here

And in case to pass artifacts between jobs in different stages, we can use dependencies together with artifacts to pass the artifacts, as described from the document.

And one more simpler example:

image: ubuntu:18.04

build:
  stage: build
  script:
    - echo "building..." >> ./result.txt
  artifacts:
    paths:
    - result.txt
    expire_in: 1 week

unit_test:
  stage: test
  script:
    - ls
    - cat result.txt
    - echo "unit testing..." >> ./result.txt
  artifacts:
    paths:
    - result.txt
    expire_in: 1 week

deploy:
  stage: deploy
  script:
    - ls
    - cat result.txt

Solution 3 - Gitlab

If you want foo/ to be available in the next stage AND it is in your .gitignore you'll need to list it in the artifacts of the stage that creates it, or as explained at here use untracked: true.

This worked for me (with NO dependencies in the following stage)

   artifacts:
     paths:
       - foo/
     expire_in: 1 hour

BTW regarding the : expire_in: 1 hour part:
I read at https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2133 there's no way to get artifacts to automatically expire at the conclusion of pipeline and the default retention was surprisingly long (30 days by default) - hence the time-based kludge to get rid of them - see https://docs.gitlab.com/ee/ci/yaml/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionygoeView Question on Stackoverflow
Solution 1 - Gitlabuser1495793View Answer on Stackoverflow
Solution 2 - GitlabChuanView Answer on Stackoverflow
Solution 3 - Gitlabk1eranView Answer on Stackoverflow