Sam
Sam

Reputation: 405

Run Python file in Jenkins on different OS

I have my Jenkins master set up on Windows and Slave on Linux. I am trying to run a python file (which I loaded on both machines locally).

So for I have been using 'Execute Windows Batch Command' but with the Linux slave I am not sure. Is there any way to introduce 'Execute Shell' into the picture so that the job can decide what to run based on the OS it decides to run?

Or is there a more effective way to run a python file on both OS in Jenkins? Please advise.

Upvotes: 2

Views: 641

Answers (2)

richarddonkin
richarddonkin

Reputation: 281

You could create a function in your JenkinsFile so that it could be used in Jenkins Pipelines:

/* A Python command which can work identically on Windows and Linux. */
def python(script_and_args) {
  if (isUnix()) {
    sh 'python3 ' + script_and_args
  }
  else {
    bat 'python ' + script_and_args
  }
}

Then you could use the function elsewhere in the JenkinsFile as follows:

stage('Script-based stage') {
  steps {
    dir(path: 'my_working_directory') {
      python('my_script.py --arguments')
    }
  }
}

I'm a newbie on Jenkins, but this approach works well for me.

Upvotes: 2

Ravindranath Barathy
Ravindranath Barathy

Reputation: 791

This could be one of the approaches you can take if you are using pipeline jobs. This also assumes that you have labeled both your agents as python

node('python') {
stage 'Run Python' {
    if (isUnix()) {
        sh 'python mypythonscript.py'
    }
    else {
        bat 'python mypythonscript.py' 
    }
}
}

Upvotes: 0

Related Questions