Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why test stage fails in flask app in the Gitlab CI/CD?

I am learning GitLab CI/CD and I want to make a stage to run tests on my flask app. Without the test stage, everything works with the Gitlab CI/CD. Here is my .gitlab-ci.yml file:

 image: python:3.8-slim-buster
    
    cache:
      paths:
        - .cache/pip
    
    stages:
      - build
      - test
      - deploy_heroku
    
    build message-requests:
      stage: build
      script:
        - apt-get update
        - apt-get install -y --no-install-recommends gcc
        - apt install -y default-libmysqlclient-dev
        - pip3 install -r requirements.txt
    
    test message-requests:
      stage: test
      script:
        - python3 test.py
    
    deploy_heroku:
      stage: deploy_heroku
      image: ruby:latest
      before_script:
        - apt-get update -qy
        - apt-get install -y ruby-dev
        - gem install dpl
      script:
        - dpl --provider=heroku --app=message-requests --api-key=$HEROKU_SECRET_KEY

My pipeline results in this error on the test stage:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from app import app
  File "/builds/dave/message-requests/app.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

Locally tests are working fine. How can I use app data which was built in the build stage?
I understand that is possible to run test.py in the build stage, but I heard that it is better to have a separate stage for testing.
I think the solution would be with using artifacts, but I am not sure how they work and where the python application installs everything.
As I have mentioned I am new to GitLab CI/CD, therefore, if I am doing something wrong I am happy to hear best practices.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In GitLab, every job you create starts in a fresh environment. So, you have to install your requirements in each job.

Your test message-requests: job does not take any steps to install Flask or other dependencies. You should add those steps.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading