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"
}
Related
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"
}
}
}
}
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/
When I try to build a pipeline it gives me an error "Failed to run image 'python:2-alpine'. Error: docker: Error response from daemon: the working directory 'C:/WINDOWS/system32/config/systemprofile/AppData/Local/Jenkins/.jenkins/workspace/simple-python-pyinstaller-app/'
this is what I have jenkinsfile
pipeline {
agent none
stages {
stage('Build') {
agent {
docker {
image 'python:2-alpine'
}
}
steps {
sh 'python -m py_compile sources/add2vals.py sources/calc.py'
stash(name: 'compiled-results', includes: 'sources/*.py*')
}
}
}
}
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:
I didn't found a solution for Windows:
This is only working for Linux:
How can I call a py script in a Jenkins File (Pipeline)?
pipeline {
agent { docker { image 'python:3.8.1' } }
stages {
stage('build') {
steps {
sh 'python --version'
}
}
}
}
As you know, the sh command let you invoke the Linux shell.
To execute your Python script on Windows, you should have the following step:
steps {
bat 'python --version'
}