Search code examples
pythongitlabgitlab-cigitlab-ci-runner

Gitlab CI, artefacts


I am doing my first CI project and I have recently got confused about artefacts...

Say I have config with next jobs:

cleanup_build:
  tags:
    - block_autotest
  stage: cleanup
  script:
    - Powershell $env:P7_TESTING_INSTALLATION_PATH\client\p7batch.exe --log-level=error --run $env:JOBS_FOLDER_PATH\clear.py

install_block:
  tags:
    - block_autotest
  stage: installation
  script:
    - Powershell $env:P7_TESTING_INSTALLATION_PATH\client\p7batch.exe --log-level=error --run $env:JOBS_FOLDER_PATH\setup_block.py

"install_block" job is not to be done if the job "cleanup_build" has failed. So, I have to create some kind of artifact after "cleanup_build" has succeeded so this artefact is visible at the stage "installation" for the job "install_block". At the job "install_block" I could use python to address the artifact and ensure the one exists.

Also I have created a speciad folder for artifacts:

ARTEFACTS_FOLDER_PATH: $CI_PROJECT_DIR\autotest\artefacts

So within the job "cleanup_build" I create a file "clean" at the artefact folder. But it seems that CI reloads repository at project directory, because if I leave just "cleanup_build" job (delete "install_block" from yml) I can see the "clean" file at the project, but if I leave both jobs this file dissapears before "install_block" job begins...


Solution

  • By default, every job starts with a 'clean' workspace. If one job modifies the workspace, it is not persisted into any other job. To pass files between jobs, you must explicitly declare artifacts to be passed between each job. Also note that the aritfact path is relative to the workspace root.

    stages:
      - one
      - two
    
    my_job:
      stage: one
      script:
        - echo "change" > myfile.txt
      artifacts:
        paths:
          # this must be a relative path, not absolute!
          - myfile.txt
    
    my_next_job:
      stage: two
      script:
        # or use 'type' instead of 'cat' on Windows
        - cat myfile.txt
    

    When the job with artifacts: defined completes, you will see a message at the end of the job log indicating the number of matched/uploaded artifacts.

    Also note that, by default, if a job fails, subsequent jobs will not run. That is: the when: behavior is on_success by default. You can enable to jobs to run additionally (or only) on failures by using when: always (or when: on_failure).