Openshift and python as base image in cicd

I need to have a test phase in my GitLab ci-cd like below

test:
  stage: test
  image:
    - openshift/origin-cli:v3.10
    - python:3.8
  before_script:
    - python -m pip install --upgrade pip
    - pip install -r req-dep.txt
    - pip install pytest
    - oc version
    - oc login --token=${CONTAINER_REGISTRY_PASSWORD} --server=${OKD_CONSOLE_URL}
    - oc project ${OKD_PROJECT}
    - python -m src.utils.OKDUtil
   
  script:
    - python -m pytest tests/ 
  rules:
    - if: '$CI_COMMIT_TAG =~ /^test.*$/'
      when: always

the code in this script: OKDUtil needs both OC and python to be able to run. this is used for port forwarding. how can I have both these images as base images in the test step?

Hi,

You cannot have both images. Why? Because your job is executing in a container. And container can be spawned from one image only.

Solution? You have two options:

  1. Use either one of those two, and then manually install the other tool directly in the pipeline:
test:
  stage: test
  image: openshift/origin-cli:v3.10
  before_script:
   # here install anything needed additionally, etc
    - apt-get update && apt-get install -y python 
  1. Create a custom docker image - write a Dockerfile that will have both Python and the OpenShift tools you need - you can create it in this project or in another one, also build it automatically with GitLab, push it to GitLab Container Registry, and then use it as reference in your test job:
test:
  stage: test
  image: registry.gitlab.com/my-group/my-project/my-custom-image:latest
  script:
    - ....

I would personally probably create a custom Docker image in a separate project, with Python (alpine) as base image, then just install OS tools on top and reference it in this project.

Hope this helps! :slight_smile:

2 Likes

Thank you !
The first solution worked for me !

1 Like