I have a Jenkins pipeline for a FastAPI and when I run the pipeline using agent any I am getting an error : pytest: not found
.
This is the 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/*.*'
}
}
}
}
However when I run the pipeline using python docker agent as below pytest executes successfully :
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'
}
}
...
...
}
}
I have switched from using python docker agent to agent any because I cant find a solution yet of running the docker agent as non-root (the agent throws a bunch of errors when running as non-root).
What am I missing ?
I expected that pytest should execute as I have successfully setup the virtual environment.
The Jenkins console log also shows that pytest was indeed installed :
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 Using the full path is not working either :
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'
}
}
}
Gives error :
+ ../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: not found