Feedback

Chat Icon

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Managing Artifacts in GitLab CI/CD: Data Sharing Between Jobs
51%

Persisting Data Between Jobs with Artifacts

Artifacts are files generated by a job that you want to pass to another job. You can use artifacts to pass files between jobs in the same pipeline or between jobs in different pipelines.

Let's say we want to show the warnings generated by the flake8 tool in a job called report. This is how we may proceed to create and launch the pipeline:

cat <$HOME/todo/app/.gitlab-ci.yml && \
cd $HOME/todo/app && \
git add . && \
git commit -m "Show warnings in the report job" && \
git push origin main
image: python:3.12

stages:
  - build  
  - test   
  - report

build:
  stage: build  
  script:
    - pip install -r requirements.txt

test:
  stage: test  
  script:
    - pip install -r requirements.txt
    - pip install flake8==7.1.1
    - flake8 --statistics \
      --extend-exclude venv \
      --output-file=flake8.txt
    - python3 test_app.py  

report:
  stage: report
  script:
    # Find warnings in the flake8.txt file
    - cat flake8.txt | grep "W" || true
EOF

Once the pipeline is triggered, you will see that the report job fails because it cannot find the flake8.txt file. This is because the flake8.txt file is generated by the test job and is not passed to the report job. Every job (build, test, report) runs in a separate container, so the flake8.txt file is not available to the report job.

Files are not passed between jobs

Files are not passed between jobs

To pass the flake8.txt file to the report job, we need to use the artifacts keyword in the test job. This is how it is done:

cat <$HOME/todo/app/.gitlab-ci.yml && \
cd $HOME/todo/app && \
git add . && \
git commit -m "Pass artifacts between jobs" && \
git push origin main
image: python:3.12

stages:
  - build  
  - test   
  - report

build:
  stage: build  
  script:
    - pip install -r requirements.txt

test:
  stage: test  
  script:
    - pip install -r requirements.txt
    - pip install flake8==7.1.1
    - flake8 --statistics \
      --extend-exclude venv \
      --output-file=flake8.txt
    - python3 test_app.py  
  artifacts:
    paths:

Cloud Native CI/CD with GitLab

From Commit to Production Ready

Enroll now to unlock all content and receive all future updates for free.