简体   繁体   中英

Platform-independent Python call from Jenkinsfile

I have a Jenkinsfile that looks like this:

stage('MyStage') {
  steps {
    script {
      bat script: "python my-script.py"
    }
  }
}

Now I want this Jenkinsfile to be able to also run on a Linux machine, and since this line "python my-script.py", can be executed exactly the same way in both Windows and Linux I want to find a way to not duplicate the call like this:

if (isUnix()) {
  sh: "python my-script.py"
} else {
  bat: "python my-script.py"
}

Is it possible to solve this in a pretty way?

Thanks!

/klarre

def call(string cmd) {
    if (isUnix()) {
        sh: cmd
    } else {
        bat: cmd
    }
}

stage('MyStage') {
  steps {
    script {
      call("python my-script.py")
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM