Search code examples
pythonjenkinskubernetespython-poetry

Running poetry in jenkinsfile


Setup is Jenkins running in Kubernetes. I want to lint my code, run my tests, then build a container. Having trouble getting poetry to install/run in one of my build steps.

podTemplate(inheritFrom: 'k8s-slave', containers: [
    containerTemplate(name: 'py38', image: 'python:3.8.4-slim-buster', ttyEnabled: true, command: 'cat')
  ]) 
{
    node(POD_LABEL) {

        stage('Checkout') {
            checkout scm
            sh 'ls -lah'
        }

        container('py38') {
            stage('Poetry Configuration') {
                sh 'apt-get update && apt-get install -y curl'
                sh "curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python"
                sh "$HOME/.poetry/bin/poetry install --no-root"
                sh "$HOME/.poetry/bin/poetry shell --no-interaction"
            }

            stage('Lint') {
                sh 'pre-commit install'
                sh "pre-commit run --all"
            }
        }
    }
}

Poetry install works fine, but when I go to activate the shell, it fails.

+ /root/.poetry/bin/poetry shell --no-interaction
Spawning shell within /root/.cache/pypoetry/virtualenvs/truveris-version-Zr2qBFRU-py3.8

[error]
(25, 'Inappropriate ioctl for device')

Solution

  • The issue here is that Jenkins runs a non-interactive shell and you are trying to start an interactive shell. The --no-interaction option doesn't mean a non-interactive shell but rather the shell not asking you questions:

      -n (--no-interaction)  Do not ask any interactive question
    

    This answer explains it 🔑🔑.

    I would just not call the shell and just use the poetry run 🏃🏃 command:

    podTemplate(inheritFrom: 'k8s-slave', containers: [
        containerTemplate(name: 'py38', image: 'python:3.8.4-slim-buster', ttyEnabled: true, command: 'cat')
      ]) 
    {
        node(POD_LABEL) {
    
            stage('Checkout') {
                checkout scm
                sh 'ls -lah'
            }
    
            container('py38') {
                stage('Poetry Configuration') {
                    sh 'apt-get update && apt-get install -y curl'
                    sh "curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python"
                    sh "$HOME/.poetry/bin/poetry install --no-root"
                }
    
                stage('Lint') {
                    sh "$HOME/.poetry/bin/poetry run 'pre-commit install'"
                    sh "$HOME/.poetry/bin/poetry run 'pre-commit run --all'"
                }
            }
        }
    }
    

    ✌️