首页 > 解决方案 > 如何在 gitlab (CI/CD) 上使用 python 和 mongodb

问题描述

我在 gitlab 上使用 CI/CD 时遇到问题。我总是使用“python:latest”,但它是 2.7.5 版本,但我想使用 python2.7.15 或 python3.7。我该如何安装它?

-

image: python:latest

services:
  - mongo:latest

variables:
  MONGO_DB: ipc_alert

cache:
  paths:
  - ~/.cache/pip/

before_script:
  - python -V 
  - pip install -r req.txt

stages:
  - test

test:
  stage: test
  script:
   - echo 'Testing'

标签: pythongitlabgitlab-ci

解决方案


On the image you're posting, you have a different problem. You cannot find django on a valid version for your requirements.

About the question itself, if you want to test against multiple versions, you need to create more than one test. For example:

test:
  stage: test
  script:
   - echo 'Testing'

That's will be:

test-python2.7:
  stage: test
  image: python:2.7
  script:
   - echo 'Testing'
test-python3.4:
  stage: test
  image: python:3.4
  script:
   - echo 'Testing'
test-python3.5:
  stage: test
  image: python:3.5
  script:
   - echo 'Testing'
test-python3.6:
  stage: test
  image: python:3.6
  script:
   - echo 'Testing'
test-python3.7:
  stage: test
  image: python:3.7
  script:
   - echo 'Testing'
test-python.latest:
  stage: test
  image: python:latest
  script:
   - echo 'Testing'

However, maybe this doesn't work, because you're using a "Shell executor". If I remember correctly, this runner execute your code against the current machine. You need to install docker and create a new runner who uses these docker. Without it, you cannot test against different environments / versions.

One exception to this, are if you have all python versions you need installed on your machine, and calls each python concrete version. It depends on your environment, but you can check on /usr/bin if you have multiple python versions. On my machine, I have on /usr/bin these ones:

maqui@kanade:~$ python -V
Python 2.7.15+
maqui@kanade:~$ python2.6 -V
Python 2.6.8
maqui@kanade:~$ python2.7 -V
Python 2.7.15+
maqui@kanade:~$ python3.6 -V
Python 3.6.8rc1
maqui@kanade:~$ python3.7 -V
Python 3.7.2rc1

(As you can see, python is an alias for python2.7).


推荐阅读