Running code in Windows agent but script says we are in Linux

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Running code in Windows agent but script says we are in Linux

judaondo

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', '[hidden email]')
                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-jon-udaondo-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.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f35c67c1-0b6e-4f34-85d1-9c0d52d2b3c8%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Running code in Windows agent but script says we are in Linux

Mark Waite-2
Use the isUnix 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.

On Mon, Feb 10, 2020 at 5:10 AM judaondo <[hidden email]> wrote:

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', '[hidden email]')
                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-jon-udaondo-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.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f35c67c1-0b6e-4f34-85d1-9c0d52d2b3c8%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAO49JtFgnV8s7WYjx4XJjfTfwOV7OvWx5ZO_hwe4-E0N%3D9YRAQ%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Running code in Windows agent but script says we are in Linux

judaondo
Thank you @MarkWaite, I wasn´t aware of that step. And thank you also for the explanation.

El lunes, 10 de febrero de 2020, 14:38:30 (UTC+1), Mark Waite escribió:
Use the <a href="https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#isunix-checks-if-running-on-a-unix-like-node" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fpipeline%2Fsteps%2Fworkflow-basic-steps%2F%23isunix-checks-if-running-on-a-unix-like-node\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF05m-VdZeyM1KZGDFI5khoLegeUA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fpipeline%2Fsteps%2Fworkflow-basic-steps%2F%23isunix-checks-if-running-on-a-unix-like-node\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF05m-VdZeyM1KZGDFI5khoLegeUA&#39;;return true;">isUnix 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 <a href="http://os.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fos.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEBq5pahnbxzpNVLfaFeTDcQCtrYg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fos.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEBq5pahnbxzpNVLfaFeTDcQCtrYg&#39;;return true;">os.name value of the master.

On Mon, Feb 10, 2020 at 5:10 AM judaondo <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="JCux0hsCGQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jonu...@...> wrote:

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', '<a href="javascript:" target="_blank" gdf-obfuscated-mailto="JCux0hsCGQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">te...@...')
                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['<a href="http://os.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fos.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEBq5pahnbxzpNVLfaFeTDcQCtrYg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fos.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEBq5pahnbxzpNVLfaFeTDcQCtrYg&#39;;return true;">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['<a href="http://os.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fos.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEBq5pahnbxzpNVLfaFeTDcQCtrYg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fos.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEBq5pahnbxzpNVLfaFeTDcQCtrYg&#39;;return true;">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-jon-udaondo-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.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JCux0hsCGQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkins...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/f35c67c1-0b6e-4f34-85d1-9c0d52d2b3c8%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/f35c67c1-0b6e-4f34-85d1-9c0d52d2b3c8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/f35c67c1-0b6e-4f34-85d1-9c0d52d2b3c8%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/f35c67c1-0b6e-4f34-85d1-9c0d52d2b3c8%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/2b393ad0-cf29-432f-b721-eedd514f909d%40googlegroups.com.