Run a Python script in Jenkins Pipeline using Windows - python

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

Related

docker python:2-alpine isn't working in jenkins pipeline?

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*')
}
}
}
}

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

Cannot create a virtual environment for python by using node.js command line , terminal libraries from electron

Im trying to create a graphical user interface using electron to create virtual environments for python
I tried out following libraries
const { exec } = require('child_process');
exec('mkvirtualenv test_env_1', (err, stdout, stderr) => {
if(err){
return err;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
var cmd = require('node-cmd');
cmd.get(
`
mkvirtualenv test_env_2
`,
function(err, data, stderr){
console.log(data);
}
);
Unfortunately none of the approaches created a virtual environment for python.
Tried shelljs node package as well, and it says it does not support shell.exec for electron library.
How may I use nodejs to create a virtual environment for python?

Categories