Artifacts

Using Artifacts in Subsequent Jobs

< >

Using Artifacts in Subsequent Jobs

Artifacts can be passed from one job to another using the dependencies keyword, as shown in the previous example. This allows subsequent jobs to access and use files generated by earlier jobs.

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

To complete the .gitlab-ci.yml file for the calculator project while using artifacts in subsequent jobs, you can add back the dependencies keyword for the test_job and deploy_job if you want them to rely on the artifacts generated in the build_job. Here’s how you can modify the .gitlab-ci.yml file accordingly:

stages:
  - build
  - test
  - deploy

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

test_job:
  stage: test
  script:
    - echo "Running Calculator tests"
    - pytest tests/ > test-results/report.txt
  artifacts:
    paths:
      - test-results/report.txt
  dependencies:
    - build_job  # This job depends on the artifacts from build_job

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the project"
  dependencies:
    - build_job  # Optional: Include if you want to reference build_job's artifacts

Explanation of Dependencies

  • dependencies in test_job: The test_job now specifies dependencies: - build_job. This means it will have access to the artifacts generated by the build_job, allowing it to use the build/ directory contents if necessary (although in this specific example, it does not directly use them).

  • dependencies in deploy_job: If you want the deploy_job to access any artifacts from the build_job, you can keep the dependencies line. However, if the deployment does not require any artifacts from the build stage, you can choose to omit it.

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.