如何解决Jenkins中的“pytest not found command”错误

b0zn9rqh  于 2023-03-22  发布在  Jenkins
关注(0)|答案(1)|浏览(384)

我有一个FastAPI的Jenkins管道,当我使用***代理任何***运行管道时,我得到一个错误:pytest: not found .
这是Jenkinsfile:

pipeline {
 agent any  
triggers {
    githubPush()
}

stages {
    
    stage('Setup'){
       steps{
        dir('.'){
            sh 'python3.8 -m venv ./venv'
        }            
        }
     }    

stage('Unit Tests'){ 
           steps{
             dir('.') {
                 sh '. ./venv/bin/activate'
                 sh 'pip install -r requirements.txt'
                 sh 'pytest -v --junitxml=docs/unit-tests/htmlcoverage/coverage.xml --cov-report xml --cov app.main'
             }           
            }                    
         }                
       stage('Publish Test Report'){ 
           steps{
              cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'coverage*.xml', conditionalCoverageTargets: '70, 0, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '80, 0, 0', maxNumberOfBuilds: 0, methodCoverageTargets: '80, 0, 0', onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
              archiveArtifacts artifacts: 'docs/unit-tests/htmlcoverage/*.*'
            }                    
         }        

      }
}

然而,当我使用***python docker agent***运行管道时,pytest成功执行:

pipeline {
  agent {
  docker {
    image 'python:3.9-slim'
    args '-u root --privileged'
  }
} 
triggers {
    githubPush()
}

stages {     
    stage('Setup'){
       steps{
           sh 'pip install -r requirements.txt'  
        }
     }
      

stage('Unit Tests'){ 
           steps{
               sh 'pytest -v --junitxml=docs/unit-tests/htmlcoverage/coverage.xml --cov-report xml --cov app.main'
            }                    
         }                              
       ...
       ...  
      }
}

我已经从使用***python docker agent***切换到***agent any***,因为我还找不到以非root身份运行docker agent的解决方案(代理在以非root身份运行时会抛出一堆错误)。
我错过了什么?
我期望pytest应该执行,因为我已经成功地设置了虚拟环境。
Jenkins控制台日志还显示pytest确实安装了:

Started by user gold
Obtained Jenkinsfile from git https://github.com/edtshuma/devsecops-labs.git
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Python-DevSecOps
...
Running in /var/lib/jenkins/workspace/Python-DevSecOps
[Pipeline] {
[Pipeline] sh
+ python3.8 -m venv ./venv
...
Running in /var/lib/jenkins/workspace/Python-DevSecOps
[Pipeline] {
[Pipeline] sh
+ . ./venv/bin/activate
+ deactivate nondestructive
+ [ -n  ]
+ [ -n  ]
+ [ -n  -o -n  ]
+ [ -n  ]
+ unset VIRTUAL_ENV
+ [ ! nondestructive = nondestructive ]
+ VIRTUAL_ENV=/var/lib/jenkins/workspace/Python-DevSecOps/venv
+ export VIRTUAL_ENV
+ _OLD_VIRTUAL_PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
+ PATH=/var/lib/jenkins/workspace/Python-DevSecOps/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
+ export PATH
+ [ -n  ]
+ [ -z  ]
+ _OLD_VIRTUAL_PS1=$ 
+ [ x(venv)  != x ]
+ PS1=(venv) $ 
+ export PS1
+ [ -n  -o -n  ]
[Pipeline] sh
+ pip install -r requirements.txt
...
Requirement already satisfied: pydantic==1.10.4 in /var/lib/jenkins/.local/lib/python3.8/site-packages (from -r requirements.txt (line 26)) (1.10.4)
Requirement already satisfied: pytest==7.2.1 in /var/lib/jenkins/.local/lib/python3.8/site-packages (from -r requirements.txt (line 27)) (7.2.1)
Requirement already satisfied: pytest-cov==4.0.0 in /var/lib/jenkins/.local/lib/python3.8/site-packages (from -r requirements.txt (line 28)) (4.0.0)
...
[Pipeline] sh
+ pytest -v --junitxml=docs/unit-tests/htmlcoverage/coverage.xml --cov-report xml --cov app.main
/var/lib/jenkins/workspace/Python-DevSecOps@tmp/durable-cd259706/script.sh: 1: pytest: not found

UPDATE使用完整路径也不行:

stage('Unit Tests'){ 
       steps{
         dir('.') {
             sh '. ./venv/bin/activate'
             sh '/var/lib/jenkins/workspace/Python-DevSecOps/venv/bin pip install -r requirements.txt'
             sh '/var/lib/jenkins/workspace/Python-DevSecOps/venv/bin pytest -v --junitxml=docs/unit-tests/htmlcoverage/coverage.xml --cov-report xml --cov app.main'
         }           
        }                    
     }

给出错误:

+ ../env/bin pytest -v --junitxml=docs/unit-tests/htmlcoverage/coverage.xml --cov-report xml --cov app.main

/var/lib/jenkins/workspace/Python-DevSecOps@tmp/durable-0925a56a/script.sh:1:../env/bin:未找到

iyzzxitl

iyzzxitl1#

不确定这是否是问题的根本原因,但如果是我,我不会依赖于Python虚拟环境被激活。

相关问题