Using Artifacts in Subsequent Jobs

Artifacts

Using Artifacts in Subsequent Jobs

Artifacts from earlier jobs are downloaded by later stages by default. Use the needs keyword when you want a job to start as soon as a required job finishes and download only that job’s artifacts.

Updating the .gitlab-ci.yml File to Use Artifacts in Subsequent Jobs

The following configuration gives test_job and deploy_job explicit access to the artifacts from build_job:

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building the application..."     
    - pip install -r requirements.txt     
    - python setup.py build --build-base build
    - echo "Build complete"
  artifacts:
    paths:
      - build/

test_job:
  stage: test
  script:
    - echo "Running Calculator tests"
    - mkdir -p test-results
    - pytest tests/ > test-results/report.txt
  artifacts:
    paths:
      - test-results/report.txt
  needs:
    - job: build_job
      artifacts: true

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the project"
  needs:
    - job: build_job
      artifacts: true

Explanation of needs:artifacts

  • needs in test_job: The job can start after build_job finishes and downloads its build/ artifact.

  • needs in deploy_job: Keep this entry only if deployment requires the build artifact.

Practical Steps for Completion

Update your .gitlab-ci.yml file: Use the updated configuration above to replace or modify your existing .gitlab-ci.yml file in your calculator project repository.

  • Test the Pipeline: Commit and push the changes to trigger the pipeline in GitLab. You should see the jobs execute in the order defined by the stages, with test_job and deploy_job having access to any artifacts produced by the build_job.

  • Access Artifacts: After the pipeline runs, you can access the artifacts generated by the build_job and test_job through the GitLab interface as previously outlined.

Optional feedback