coverage report post step not picking up data in previous step - python

I have a post always step in Jenkinsfile that runs coverage report -m. The reason why it's in the post step is I want coverage report, regardless even if tests fail. I see that it's running but it's not picking up data from the test phase: python3 -m coverage run -m pytest -v tests
In Jenkinsfile, I have:
stage('Test') {
steps {
dir('src') {
sh "python3 -m coverage run -m pytest -v tests"
}
}
post {
always {
sh "python3 -m coverage report -m"
}
}
}
coverage report -m reports that there's no data: No data to report

I needed to run coverage report -m in the same directory as the one I ran for tests.
stage('Test') {
steps {
dir('src') {
sh "python3 -m coverage run -m pytest -v tests"
}
}
post {
always {
dir('src') {
sh "python3 -m coverage report -m"
}
}
}
}

Related

How to properly use jenkinsfile "parallel" tag and docker-compose to test multiple python versions and not prematurely kill containers?

I have built a jenkinsfile that runs some tests on a repo I've got, but for multiple versions of python, and done in parallel using the "parallel" jenkinsfile tag.
This jenkinsfile makes use of a docker-compose file (corresponding to python 3.8) and then two docker-compose overrides (corresponding to python 3.9 and 3.10).
The problem I'm currently running into is that sometimes, one of the docker-compose down --remove-orphans calls seem to be trying to sometimes kill containers that are a part of one of the other parallel processes, i.e. I get a error while removing network: network <network name> id <id> has active endpoints error.
I'm wondering what is the proper way to deal with this issue. Thanks!
JENKINSFILE CODE:
pipeline {
agent none
stages {
stage('Test Across Python Versions:') {
parallel {
stage('Python 3.8: Checkout and Create Environment Variables') {
agent {
node {
label 'MyBuilder'
}
}
stages('Testing'){
stage('Python 3.8: Build and Test'){
steps {
sh 'docker-compose -p py3_8 build'
sh 'docker-compose -p py3_8 run --rm tests'
}
}
}
post{
always {
sh "docker-compose -p py3_8 down --remove-orphans"
junit '**/junit*.xml'
}
}
}
stage('Python 3.9: Checkout and Create Environment Variables') {
agent {
node {
label 'MyBuilder'
}
}
stages('Testing'){
stage('Python 3.9: Build and Test'){
steps {
sh 'docker-compose -p py3_9 -f docker-compose.yaml -f docker-compose.python3_9.yaml build'
sh 'docker-compose -p py3_9 -f docker-compose.yaml -f docker-compose.python3_9.yaml run --rm tests'
}
}
}
post{
always {
sh "docker-compose -p py3_9 -f docker-compose.yaml -f docker-compose.python3_9.yaml down --remove-orphans"
junit '**/junit*.xml'
}
}
}
stage('Python 3.10: Checkout and Create Environment Variables') {
agent {
node {
label 'MyBuilder'
}
}
stages('Testing'){
stage('Python 3.10: Build and Test'){
steps {
sh 'docker-compose -p py3_10 -f docker-compose.yaml -f docker-compose.python3_10.yaml build'
sh 'docker-compose -p py3_10 -f docker-compose.yaml -f docker-compose.python3_10.yaml run --rm tests'
}
}
}
post{
always {
sh "docker-compose -p py3_10 -f docker-compose.yaml -f docker-compose.python3_10.yaml down --remove-orphans"
junit '**/junit*.xml'
}
}
}
}
}
}
}
DOCKER-COMPOSE CODE:
version: "3.4"
x-tests: &blah
build:
context: .
dockerfile: Dockerfile
image: my_repo:test-venv-3_8
working_dir: /home
volumes:
- .:/home/
services:
tests:
<<: *blah
container_name: tests
entrypoint: python3 -m pytest -c pytest.ini
DOCKER-COMPOSE OVERRIDE CODE:
version: "3.4"
x-tests:
build:
context: .
dockerfile: python3_9.Dockerfile
image: my_repo:test-venv-3_9
working_dir: /home
volumes:
- .:/home/

How to get docker work / run python in Jenkins pipeline?

Here is the story...
i trying build a jenkins pipeline to automate a python test.
when i'm trying run 'pip install xxxx'. The jenkins built process returned pip command not found error. i notice jenkins might require python in the runtime and jenkins by default doesn't come with python.
Hence i trying to pull a docker image in pipeline.
added line "agent{ docker { image 'python:3.7.2' } }"
however docker not found error occurred during the build process
i have installed docker plugin on jenkins, but the docker error still remain...
the question is how i can make the docker work or
how i can run python in jenkins pipeline? as all i'm doing this cos i want to run python tests in jenkins pipeline.
here is the example code
pipeline {
agent{ docker { image 'python:3.7.2' } }
}
stages {
stage('checkout') {
steps{
xxxxxx
}
}
stage('setup env') {
steps {
sh 'pip install -r dependencies.txt'
}
}
stage('run test') {
steps {
sh 'pytest'
}
}
}

Running python Unittest in Jenkins

I am a new programmer. I have a simple calculator python code which i want to deploy using Jenkins pipeline.
There is a python unittest file in my git repo named "unit-test". I tried to execute unit test as mentioned in the stage(test), but it is not working.
stage('Test') {
steps {
echo 'Testing..'
python setup.py nosetests --with-xunit
nosetests unit-test.py
Can someone please help me fix the issue.
My github repo url: https://github.com/Tasfiq23/Devops_assessment_2.git/
I have re-created your pipeline in Jenkins and it got success.
Pipeline Script
pipeline {
agent any
stages {
stage ('GIT Checkout'){
steps {
git changelog: false, poll: false, url: 'https://github.com/Tasfiq23/Devops_assessment_2.git'
}
}
stage('build') {
steps {
sh 'pip install -r requirements.txt'
}
}
stage ('Test'){
steps {
sh 'python unit-test.py'
}
}
}
}
Output:

Adding testing stage into Jenkins file

I am trying to add Testing stage into my Jenkins file.
I have docker installed in the Jenkins.
I run my tests with this command:
docker-compose run web python manage.py test
This is my Jenkins file:
node{
stage('Checkout'){
def dockerHome = tool 'docker'
env.PATH = "${dockerHome}/bin"
checkout scm
}
stage('Build image') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
sh "docker login --username=mygituks --password=mdj1646MDJ"
sh "docker build -t my_git_uks -f Dockerfile ."
sh "docker tag my_git_uks gituks/uks-git-2019:second"
}
}
stage('Run Tests') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
def testsError = null
try {
sh "docker-compose run web python manage.py test"
}
catch(err) {
testsError = err
echo "Failure"
}
}
}
stage('Push image') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
sh "docker push gituks/uks-git-2019:second"
}
}
}
And I get this error:
docker-compose: command not found
This is one of the things I also tried and I get this error:
test.sh: line 3: python: command not found
I added test.sh:
#!/bin/bash
python manage.py test
And changed my Testing stage to:
stage('Run Tests') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
def testsError = null
try {
sh "bash test.sh"
}
catch(err) {
testsError = err
echo "Failure"
}
}
Hope someone can help me to figure this out or give my any hints.
SOLVED
node{
stage('Checkout'){
def dockerHome = tool 'docker'
env.PATH = "${dockerHome}/bin"
PATH = "$PATH:/usr/bin"
checkout scm
}
stage('Build image') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
sh "docker login --username=mygituks --password=mdj1646MDJ"
sh "docker build -t my_git_uks -f Dockerfile ."
sh "docker tag my_git_uks gituks/uks-git-2019:second"
}
}
stage('Run Tests') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
echo "PATH is: $PATH"
sh "docker exec my_git_web python manage.py test"
}
}
stage('Push image') {
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']){
sh "docker push gituks/uks-git-2019:second"
}
}
}
It is possible that you don't have docker-compose installed on your agent.
You can either:
Install docker-compose on the agent: (taken from docker official docs`)
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Try to use a Docker with Docker image and use
docker.image("docker").inside() {
sh "docker-compose run web python manage.py test"
}

npm start runs out of virtualenv

I have a virtualenv in which I try to run a start script defined in package.json, but somehow npm changes the source and runs outside of the venv.
If I print which python for example in that npm start script, I get /usr/local/bin/python, so not the python from virtualenv.
Any ideas?
Edit:
package.json
{
...
"scripts": {
"start": "myscript & watchify -o assets/js/mylibs.js -v -d .",
},
...
}

Categories