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
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: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.
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.
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.
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
andtest_pylint
jobs are independent jobs that run the same setup steps when the jobs are run.