简体   繁体   中英

How to Pass Value from One Shell Block to Another Shell Block or assign that value to the Variable of the Groovy Script in Jenkins

script{

    println("Starting TEST")

    //Declaring Variable var value in the Below Shell Block
    sh"""
        echo "This is Test"
        var=10
        echo "This is Test Value \$var"
    """

    //Trying to Print the var Value from above Block
    sh"""
        echo "This is Other Block"
        echo "This is Test Value \$var"
    """

    //Copying the value from Shell Script Variable to Groovy Script
    def scriptVar=sh(script:"\$var ")
}

In the above code Im trying to print the value of a shell block and use the same value to assign the value in next block but echo will just show blank message after printing value in 2nd block

You can simply write the value to a file and then read it from there. Check the following.

script{
        println("Starting TEST")
        //Declaring Variable var value in the Below Shell Block
        sh"""
            echo "This is Test"
            var=10
            echo \$var > varOut
            echo "This is Test Value \$var"
        """
    
        //Trying to Print the var Value from above Block
        sh"""
            echo "This is Other Block"
            var=\$(cat varOut)
            echo "This is Test Value \$var"
        """

        // Assign the value to a variable
        def var = readFile(file: "varOut")
        echo "Var = $var"
}

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