How to determine the current operating system in a Jenkins pipeline

后端 未结 5 1306
醉梦人生
醉梦人生 2020-12-28 21:31

What would be the way to determine the current OS a Jenkins pipeline is running?

Context: I\'m building a shared Jenkins pipeline script that should run on all platf

5条回答
  •  甜味超标
    2020-12-28 21:37

    The workaround I found for this is

    try{
       sh(script: myScript, returnStdout: true)
     }catch(Exception ex) {
        //assume we are on windows
       bat(script: myScript, returnStdout: true)
     }
    

    Or a little bit more elegant solution without using the try/catch is to use the env.NODE_LABELS. Assuming you have all the nodes correctly labelled you can write a function like this

    def isOnWindows(){
        def os = "windows"
        def List nodeLabels  = NODE_LABELS.split()
        for (i = 0; i 

    and then

    if (isOnWindows()) {
        def osName = bat(script: command, returnStdout: true)   
    } else {
        def osName = sh(script: command, returnStdout: true)
    }
    

提交回复
热议问题