<p>I got the answer.
As Mark Waite answered here:</p>

<p><a href="https://groups.google.com/forum/#!topic/jenkinsci-users/aGYt-OEYds8" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/jenkinsci-users/aGYt-OEYds8</a></p>

<p>Use the <a href="https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#isunix-checks-if-running-on-a-unix-like-node" rel="nofollow noreferrer">isUnix</a> pipeline step rather than writing your own version of that step.  The System.properties that you are using will execute on the master, not on the agent.  When it executes on the master, it will report the os.name value of the master.</p>

CC BY-SA 4.0
2020-02-11T08:26:10.947Z
should accept your answer so it's closed. nice find
sheldonhull
CC BY-SA 4.0
2020-02-10T16:04:52.337Z
2020-02-11T08:39:50.163Z
2020-02-11T08:39:50.163Z
joudaon
<p>I have the following pipeline (Jenkinsfile):</p>

<pre><code>@Library('mylib@master')

import com.mylib.*


pipeline {

    agent { label 'WindowsSlaveWS2016' }

    stages {
        stage('Demo') {
            steps {
                echo 'a'
                determineOS()
                echo 'b'
                // sh 'echo "[Version 1.0.0]" | tee -a changelog.txt'
                sayHello()
                //getServiceVersion('.', 'changelog.txt', 'myservice')
                // cat service.properties
                // sendEmail('failed', 'test@test.com')
                script {
                    def utils = new Utils()
                    if (utils.isWindows()) {
                        echo '&lt;-- Is windwos --&gt;'
                    }
                    else {
                        echo '&lt;-- Is linux --&gt;'
                    }
                }
            }
        }
    }

}
</code></pre>

<p>determineOS() groovy script is a simple script that does the following:</p>

<pre><code>#!/usr/bin/env groovy

def call() {
    if (System.properties['os.name'].toLowerCase().contains('windows')) {
        println "it's Windows"
    } else {
        println "it's not Windows"
    }
}
</code></pre>

<p>and utils.Windows() does the following:</p>

<pre><code>#!/usr/bin/groovy

package com.ciplatforms;

class Utils implements Serializable {

    def isWindows() {
        if (System.properties['os.name'].toLowerCase().contains('windows')) {
            println "it's Windows"
            return true
        } else {
            println "it's not Windows"
            return false
        }
    }

}
</code></pre>

<p>I am running the job in a Windows machine as you can notice in the pipeline but the log displays the following:</p>

<pre><code>Running on WindowsSlaveWS2016 in C:\Jenkins\workspace\test-pipeline-shared-library
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Demo)
[Pipeline] echo
a
[Pipeline] echo
it's not Windows
[Pipeline] echo
b
[Pipeline] echo
Hello, human.
[Pipeline] script
[Pipeline] {
[Pipeline] echo
&lt;-- Is linux --&gt;
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
</code></pre>

<p>Any ideas why this is happening?
Thank you.</p>

CC BY-SA 4.0
2020-02-10T14:59:49.367Z
How do you call this script in configuration page in Jenkins?
Vader
<jenkins-pipeline>
Jenkins - Running code in Windows agent but script says we are in Linux
joudaon
# Jenkins - Running code in Windows agent but script says we are in Linux

(Asked by: username_0 on 2020-02-10)

I have the following pipeline (Jenkinsfile):
@Library('mylib@master')

import com.mylib.*


pipeline {

    agent { label 'WindowsSlaveWS2016' }

    stages {
        stage('Demo') {
            steps {
                echo 'a'
                determineOS()
                echo 'b'
                // sh 'echo "[Version 1.0.0]" | tee -a changelog.txt'
                sayHello()
                //getServiceVersion('.', 'changelog.txt', 'myservice')
                // cat service.properties
                // sendEmail('failed', 'test@test.com')
                script {
                    def utils = new Utils()
                    if (utils.isWindows()) {
                        echo '<-- Is windwos -->'
                    }
                    else {
                        echo '<-- Is linux -->'
                    }
                }
            }
        }
    }

}

determineOS() groovy script is a simple script that does the following:
#!/usr/bin/env groovy

def call() {
    if (System.properties['os.name'].toLowerCase().contains('windows')) {
        println "it's Windows"
    } else {
        println "it's not Windows"
    }
}

and utils.Windows() does the following:
#!/usr/bin/groovy

package com.ciplatforms;

class Utils implements Serializable {

    def isWindows() {
        if (System.properties['os.name'].toLowerCase().contains('windows')) {
            println "it's Windows"
            return true
        } else {
            println "it's not Windows"
            return false
        }
    }

}

I am running the job in a Windows machine as you can notice in the pipeline but the log displays the following:
Running on WindowsSlaveWS2016 in C:\Jenkins\workspace\test-pipeline-shared-library
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Demo)
[Pipeline] echo
a
[Pipeline] echo
it's not Windows
[Pipeline] echo
b
[Pipeline] echo
Hello, human.
[Pipeline] script
[Pipeline] {
[Pipeline] echo
<-- Is linux -->
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Any ideas why this is happening?
Thank you.

**Tags:** jenkins-pipeline

> **Comments:**
> **username_1**: How do you call this script in configuration page in Jenkins?  

---

## 1 answers

### Answer by username_0 on 2020-02-10. Score: 2 | Accepted answer

I got the answer.
As Mark Waite answered here:

<https://groups.google.com/forum/#!topic/jenkinsci-users/aGYt-OEYds8>

Use the [isUnix](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#isunix-checks-if-running-on-a-unix-like-node) pipeline step rather than writing your own version of that step. The System.properties that you are using will execute on the master, not on the agent. When it executes on the master, it will report the os.name value of the master.

> **Comments:**
> **username_2**: should accept your answer so it's closed. nice find  

---

