如何在Gitlab CI上安装Python

如何在Gitlab CI上安装Python,python,gitlab,gitlab-ci,Python,Gitlab,Gitlab Ci,如何在Gitlab CI上安装各种版本的Python 在我以前使用Travis CI的经验中,我只需运行正常的Ubuntu/Debian命令来安装deadsnakes repo,然后安装我需要的任何版本,如: sudo add-apt-repository -y ppa:fkrull/deadsnakes sudo apt-get -yq update sudo apt-get -yq install python2.7 python2.7-dev python3.4 python3.4-dev

如何在Gitlab CI上安装各种版本的Python

在我以前使用Travis CI的经验中,我只需运行正常的Ubuntu/Debian命令来安装deadsnakes repo,然后安装我需要的任何版本,如:

sudo add-apt-repository -y ppa:fkrull/deadsnakes
sudo apt-get -yq update
sudo apt-get -yq install python2.7 python2.7-dev python3.4 python3.4-dev python3.6 python3.6-dev python3.7 python3.7-dev
我在Gitlab CI中尝试过类似的配置:

image: ubuntu:latest

before_script:
  - add-apt-repository -y ppa:fkrull/deadsnakes
  - apt-get -yq update
  - apt-get -yq install python2.7 python2.7-dev python3.4 python3.4-dev python3.6 python3.6-dev python3.7 python3.7-dev
  - python -V

test:
  script:
  - ./run_my_tests.sh
但这在以下方面失败:

/bin/bash: line 82: add-apt-repository: command not found

我只能假设,即使我运行的是Ubuntu映像,Gitlab也会限制可用的命令。在Gitlab CI中安装Python的等效方法是什么?

您应该使用包含所需一切的基本映像。原则上,手工安装应该可以工作,但不必要地花费您几分钟的GitLab CI管道时间

对于python 3.7,可以执行以下操作:

image: python:3.7-alpine3.9
检查DockerHub以获取所有可用python映像的列表:

如果需要使用不同的python版本进行测试,我建议您将任务拆分为不同的GitLab CI作业,每个作业使用不同的python基本映像:

test-python-3-7:
  image: python:3.7-alpine3.9
  script:
  - ./run_my_tests.sh

test-python-2.7:
  image: python:2.7.16-alpine3.8
  script:
  - ./run_my_tests.sh

如果您确实需要自己安装东西,因为没有合适的映像,我仍然建议您创建一个包含所有所需内容的映像,将其上载到DockerHub或您自己的GitLab容器注册表,然后在CI管道中使用它。

@Arthur Havlicek的想法是正确的。我原以为
软件属性common
是默认安装的,但事实并非如此。此外,我使用了错误的PPA名称,即现在的“死蛇/PPA”

正在运行的配置文件如下所示:

image: ubuntu:latest

before_script:
  - apt-get -yq update
  - apt-get -yq install software-properties-common
  - add-apt-repository -y ppa:deadsnakes/ppa
  - apt-get -yq update
  - apt-get -yq install python-minimal python2.7 python2.7-dev python3.6 python3.6-dev python3.7 python3.7-dev python-pip

test:
  script:
  - ./run_my_tests.sh

只是把它扔出去,希望它不是。您确定系统上安装了软件包add apt repository吗?如果系统上不存在该命令,您将无法运行该命令,您需要手工编辑apt源列表,并添加以下两行
debhttp://ppa.launchpad.net/deadsnakes/ppa/ubuntu 你的UBUNTU版本在这里主要
debsrchttp://ppa.launchpad.net/deadsnakes/ppa/ubuntu 您的\u UBUNTU\u版本\u此处为main
,从快速搜索看来,添加apt存储库需要
软件属性通用
软件包。如果已经满意,您应该更新您的问题