skip to Main Content

I have a gitlab pipeline split in two jobs but unfortunately the second job complains /bin/sh: eval: pip: not found.

Below is the .gitlab-ci.yml file.

stages:
  - build
  - test

default:
  image: docker:24.0
  
build_image:
  stage: build
  script:
    - docker build .

test:
  stage: test
  script:
    - pip install .[dev]
    - pytest

The build_image job is doing a build on my Dockerfile but despite that the next job does not find pip. The contents of the Dockerfile looks as follows:

FROM python:3.12-slim
WORKDIR /usr/src/template_repository
COPY ./requirements.txt .
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

How can I get the test job to find pip and install all the requirements?

2

Answers


  1. This means that the base image you are using for running your jobs (the docker image) does not have pip installed.

    To use a different image in the test job, you should use the image directive, for example:

    test:
      stage: test
      image: python:3.12-slim
      script:
        - pip install .[dev]
        - pytest
    

    I don’t know what you are doing, but I think you are confusing concepts, as far as I know, it is not possible/good practice to build an image and use it as a base for the next job. You could try to build an image, push it to the gitlab registry, and then use it in your jobs.

    Login or Signup to reply.
  2. You are confusing a couple of concepts about how Gitlab jobs work.

    1) Each job you run is independent.

    Each job will look at it’s settings (possibly pulling from default if not set) and use that to launch a fresh new job. This makes sense when you consider that Gitlab jobs typically run in their own container and can be run on different hosts that share nothing.

    stages:
      - build
      - test
    
    default:
      image: docker:24.0
      
    # This job pulls image docker:24.0 as it's starting point
    build_image:
      stage: build
      script:
        - docker build .
    
    # This job ALSO pulls image docker:24.0 as it's starting point    
    test:
      stage: test
      script:
        - pip install .[dev]
        - pytest
    

    2) To use an image you build, you need to publish it and specify it in the job

    If you want to use your own image as the base image for a job, you’ll need to publish it somewhere.

    stages:
      - build
      - test
    
    # publish to your gitlab after build 
    build_image:
      stage: build
      image: docker:24.0
      script:
        - docker build . -t docker build -t yourgitlab.com/group/project/image
        - docker push yourgitlab.com/group/project/image
    
    # use the image by setting image tag
    test:
      stage: test
      image: yourgitlab.com/group/project/image
      script:
        - pip install .[dev]
        - pytest
    

    3) If you just want to build your code in the job, and not explicitly build an image, just do that

    If you just want to run tests, you don’t really need to build a docker image. You can just use existing ones and install what you need. You can also share setup logic using before scripts in Gitlab jobs. In the below example both test_pytest and test_pylint jobs are independent jobs that run the same setup steps when the jobs are run.

    stages:
      - test
    
    default:
      image: python3.10
      before_script:
        - pip install .[dev]
    
    test_pytest:
      stage: test
      script:
        - pytest
    
    test_pylint:
      stage: test
      script:
        - pylint
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search