简体   繁体   English

如何在Gitlab-CI上安装Python

[英]How to install Python on Gitlab-CI

How do you install various versions of Python on Gitlab-CI? 如何在Gitlab-CI上安装各种版本的Python?

In my previous experience with Travis-CI, I simply run the normal Ubuntu/Debian commands to install the deadsnakes repo and then install whatever version I need like: 在我之前使用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

I've tried this similar configuration with Gitlab-CI: 我用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

but this fails with: 但这失败了:

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

I can only assume that even though I'm running an Ubuntu image, Gitlab restricts the commands available. 我只能假设即使我正在运行Ubuntu映像,Gitlab也会限制可用的命令。 What's the equivalent way to install Python in Gitlab-CI? 在Gitlab-CI中安装Python的等效方法是什么?

You should use a base image containing everything you need. 您应该使用包含所需内容的基本图像。 Installing something by hand should work in principle, but will unnecessarily cost you GitLab CI pipeline minutes. 手动安装东西应该原则上工作,但不必要地花费你GitLab CI管道分钟。

For python 3.7 you could do the following: 对于python 3.7,您可以执行以下操作:

image: python:3.7-alpine3.9

Check DockerHub for a list of all available python images: https://hub.docker.com/_/python 检查DockerHub以获取所有可用python映像的列表: https ://hub.docker.com/_/python

If you need to test with different python versions, I recommend to split your tasks into different GitLab CI jobs, each using a different python base image: 如果你需要测试不同的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

If you absolutely need to install stuff by yourself, because there is no appropriate image, I would still suggest you create an image with everything you need, upload it to DockerHub or your own GitLab container registry, and then use it in your CI pipelines. 如果您绝对需要自己安装内容,因为没有合适的图像,我仍然建议您创建一个包含所需内容的图像,将其上传到DockerHub或您自己的GitLab容器注册表,然后在CI管道中使用它。

@Arthur Havlicek had the right idea. @Arthur Havlicek有正确的想法。 I thought software-properties-common was installed by default, but it's not. 我认为默认安装了software-properties-common ,但事实并非如此。 Additionally, I was using the wrong PPA name, which is now "deadsnakes/ppa". 另外,我使用了错误的PPA名称,现在是“deadsnakes / ppa”。

The functioning config file looks like: 功能配置文件如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM