运行 jenkinsfile 中的诗歌

Running poetry in jenkinsfile

在 Kubernetes 中设置 Jenkins 运行ning。我想检查我的代码,运行 我的测试,然后构建一个容器。在我的构建步骤之一中无法将诗歌添加到 install/run。

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"
            }
        }
    }
}

诗歌安装工作正常,但是当我去激活 shell 时,它失败了。

+ /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')

这里的问题是 Jenkins 运行的是非交互式 shell 而您正在尝试启动交互式 shell。 --no-interaction 选项并不意味着非交互式 shell 而是 shell 不问你问题:

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

This answer explains it .

我不会调用 shell 而只是使用 poetry run 命令:

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'"
            }
        }
    }
}

✌️

在容器级别安装 poetry,然后使用

解析 poetry.lock 文件
poetry export --without-hashes --dev -f requirements.txt -o requirements.txt

然后使用 pip install -r requirements.txt 代替 poetry install

安装依赖项

那么您就不必 运行 在虚拟环境中执行您的命令了。