Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Python on Gitlab-CI

How do you install various versions of Python on Gitlab-CI?

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:

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:

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. What's the equivalent way to install Python in Gitlab-CI?

like image 742
Cerin Avatar asked Dec 10 '22 03:12

Cerin


2 Answers

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.

For python 3.7 you could do the following:

image: python:3.7-alpine3.9

Check DockerHub for a list of all available python images: 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:

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.

like image 82
Thomas Kainrad Avatar answered Dec 21 '22 18:12

Thomas Kainrad


@Arthur Havlicek had the right idea. I thought software-properties-common was installed by default, but it's not. Additionally, I was using the wrong PPA name, which is now "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
like image 20
Cerin Avatar answered Dec 21 '22 18:12

Cerin