Accessing a variable in shell

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Accessing a variable in shell

Idan Adar
Given the following script block in a stage (Declarative pipeline), how can I access the repoName variable?

stages {
    stage
("...") {
        script
{
           
for (String repoName: repoList) {
                sshagent
(credentials: ['e276113e-0ec9-4eaa-88f9-a7db5c9635b6']) {
                    sh
"""
                        git clone -b master git@****
.com:****/repoName.git
                        cd repoName
                        ....
                    """

               
}
           
}
         
}
     
}
}

--
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/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Joachim Nilsson-3
As I understand, it is written in Groovy. That means you can access a variable in a (Groovy)string using dollar and optionally curly brackets.

"  variable value is ${variable} "

Från: Idan Adar
Skickat: söndag 30 juli 13:56
Ämne: Accessing a variable in shell
Till: Jenkins Users


Given the following script block in a stage (Declarative pipeline), how can I access the repoName variable?

stages {
    stage("...") {
        script {
            for (String repoName: repoList) {
                sshagent (credentials: ['e276113e-0ec9-4eaa-88f9-a7db5c9635b6']) {
                    sh """
                        git clone -b master git@****.com:****/repoName.git
                        cd repoName
                        ....
                    """
                }
            }
         }
     }
}

--
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].
For more options, visit https://groups.google.com/d/optout.


--
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/E5FDBCA98976C447.5af66ff2-2a16-45cb-968f-645c0c29ec56%40mail.outlook.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Idan Adar
I have tried this, but it complains: 
groovy.lang.MissingPropertyException: No such property: moduleName for class: WorkflowScript

Here is the full Jenkinsfile:
The majority of it can be disregarded... the issue at hand is the use of the variables, ${repoName}, ${moduleName}, etc...

ReposToUpdate and npmDependencies are Extended choice parameters e.g.:
myrepo1,myrepo2,myrepo3,...

def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline
{
   agent
{
      label
'cert_mgmt'
   
}

   stages
{
      stage
("Update package.json") {
         steps
{
            script
{
               
for (String repoName : repoList) {
                  sshagent
(credentials: ['credentials-ID']) {
                     sh
"""
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                           
                        stat -t . > folderStat1.txt
                        for (String moduleName : moduleList) {
                           ncu -u -f "
${moduleName}"
                        }
                       
                        stat -t . > folderStat2.txt
                     """

                     
                     
def folderStat1 = readFile('folderStat1.txt').trim()
                     
def folderStat2 = readFile('folderStat2.txt').trim()
                     
                     
if (folderStat1 == folderStat2) {
                        slackSend
(
                           color
: '#199515',
                           message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                       
)
                     
}
                     
else {
                        sh
"""
                           cd ${repoName}
                           
                           git config --global user.name "
****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master
                       
                           cd ..
                           rm -rf ${repoName}
                        """

                       
                        slackSend
(
                           color
: '#199515',
                           message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                       
)
                     
}
                 
}
               
}
           
}
         
}
     
}
   
}
   
   post
{
      failure
{
         slackSend
(
            color
: '#F01717',
            message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         
)
     
}
   
}
}



On Sunday, July 30, 2017 at 10:48:39 PM UTC+3, Joachim Nilsson wrote:
As I understand, it is written in Groovy. That means you can access a variable in a (Groovy)string using dollar and optionally curly brackets.

"  variable value is ${variable} "

Skaffa <a href="https://aka.ms/ghei36" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Faka.ms%2Fghei36\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFU8yON1EsfNLPhgUtO6Jc_yaEEpA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Faka.ms%2Fghei36\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFU8yON1EsfNLPhgUtO6Jc_yaEEpA&#39;;return true;">Outlook för Android



Från: Idan Adar
Skickat: söndag 30 juli 13:56
Ämne: Accessing a variable in shell
Till: Jenkins Users


Given the following script block in a stage (Declarative pipeline), how can I access the repoName variable?

stages {
    stage("...") {
        script {
            for (String repoName: repoList) {
                sshagent (credentials: ['e276113e-0ec9-4eaa-88f9-a7db5c9635b6']) {
                    sh """
                        git clone -b master git@****.com:****/repoName.git
                        cd repoName
                        ....
                    """
                }
            }
         }
     }
}

--
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="LS3aVJkYAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%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/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"> https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


--
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/f5ba050e-2d42-47d4-8183-89e43edc6fec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Michael Pailloncy
The for loop is in Java syntax instead of script shell syntax here :

sh """
    git clone -b master git@****.com:****/${repoName}.git
    cd ${repoName}
       
    stat -t . > folderStat1.txt
    for (String moduleName : moduleList) {
       ncu -u -f "${moduleName}"
    }
    
    stat -t . > folderStat2.txt
"""

IIUC, you want to do something like :

sh "git clone -b master git@****.com:****/${repoName}.git"

dir(repoName) {
    sh "stat -t . > folderStat1.txt"
    for (int i = 0; i < moduleList.size(); i++) {
        def moduleName = moduleList[i]
        sh "ncu -u -f \"${moduleName}\""
    }
    sh "stat -t . > folderStat2.txt"
}

Hopefully it helps.

2017-08-07 14:49 GMT+02:00 Idan Adar <[hidden email]>:
I have tried this, but it complains: 
groovy.lang.MissingPropertyException: No such property: moduleName for class: WorkflowScript

Here is the full Jenkinsfile:
The majority of it can be disregarded... the issue at hand is the use of the variables, ${repoName}, ${moduleName}, etc...

ReposToUpdate and npmDependencies are Extended choice parameters e.g.:
myrepo1,myrepo2,myrepo3,...

def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline
{
   agent
{
      label
'cert_mgmt'
   
}

   stages
{
      stage
("Update package.json") {
         steps
{
            script
{
               
for (String repoName : repoList) {

                  sshagent
(credentials: ['credentials-ID']) {
                     sh
"""
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                           
                        stat -t . > folderStat1.txt
                        for (String moduleName : moduleList) {
                           ncu -u -f "
${moduleName}"
                        }
                       
                        stat -t . > folderStat2.txt
                     """

                     
                     
def folderStat1 = readFile('folderStat1.txt').trim()
                     
def folderStat2 = readFile('folderStat2.txt').trim()
                     
                     
if (folderStat1 == folderStat2) {
                        slackSend
(
                           color
: '#199515',
                           message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                       
)
                     
}
                     
else {
                        sh
"""
                           cd ${repoName}
                           
                           git config --global user.name "
****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master
                       
                           cd ..
                           rm -rf ${repoName}
                        """

                       
                        slackSend
(
                           color
: '#199515',
                           message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                       
)
                     
}
                 
}
               
}
           
}
         
}
     
}
   
}
   
   post
{
      failure
{
         slackSend
(
            color
: '#F01717',
            message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         
)
     
}
   
}
}



On Sunday, July 30, 2017 at 10:48:39 PM UTC+3, Joachim Nilsson wrote:
As I understand, it is written in Groovy. That means you can access a variable in a (Groovy)string using dollar and optionally curly brackets.

"  variable value is ${variable} "

Från: Idan Adar
Skickat: söndag 30 juli 13:56
Ämne: Accessing a variable in shell
Till: Jenkins Users


Given the following script block in a stage (Declarative pipeline), how can I access the repoName variable?

stages {
    stage("...") {
        script {
            for (String repoName: repoList) {
                sshagent (credentials: ['e276113e-0ec9-4eaa-88f9-a7db5c9635b6']) {
                    sh """
                        git clone -b master git@****.com:****/repoName.git
                        cd repoName
                        ....
                    """
                }
            }
         }
     }
}

--
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 jenkinsci-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
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/f5ba050e-2d42-47d4-8183-89e43edc6fec%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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/CAPO77c15ckcvjR972ppqTUp3V7G%3DGCxLTptA2fvnNe6tWgM%2Bsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Idan Adar
Hm, failed with the same error... full stack: https://pastebin.com/AL2J5MMS


On Tuesday, August 8, 2017 at 9:49:28 AM UTC+3, mpapo - Michael Pailloncy wrote:
The for loop is in Java syntax instead of script shell syntax here :

sh """
    git clone -b master git@****.com:****/${repoName}.git
    cd ${repoName}
       
    stat -t . > folderStat1.txt
    for (String moduleName : moduleList) {
       ncu -u -f "${moduleName}"
    }
    
    stat -t . > folderStat2.txt
"""

IIUC, you want to do something like :

sh "git clone -b master git@****.com:****/${repoName}.git"

dir(repoName) {
    sh "stat -t . > folderStat1.txt"
    for (int i = 0; i < moduleList.size(); i++) {
        def moduleName = moduleList[i]
        sh "ncu -u -f \"${moduleName}\""
    }
    sh "stat -t . > folderStat2.txt"
}

Hopefully it helps.

2017-08-07 14:49 GMT+02:00 Idan Adar <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="xPWXgeFIBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">id...@...>:
I have tried this, but it complains: 
groovy.lang.MissingPropertyException: No such property: moduleName for class: WorkflowScript

Here is the full Jenkinsfile:
The majority of it can be disregarded... the issue at hand is the use of the variables, ${repoName}, ${moduleName}, etc...

ReposToUpdate and npmDependencies are Extended choice parameters e.g.:
myrepo1,myrepo2,myrepo3,...

def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline
{
   agent
{
      label
'cert_mgmt'
   
}

   stages
{
      stage
("Update package.json") {
         steps
{
            script
{
               
for (String repoName : repoList) {

                  sshagent
(credentials: ['credentials-ID']) {
                     sh
"""
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                           
                        stat -t . > folderStat1.txt
                        for (String moduleName : moduleList) {
                           ncu -u -f "
${moduleName}"
                        }
                       
                        stat -t . > folderStat2.txt
                     """

                     
                     
def folderStat1 = readFile('folderStat1.txt').trim()
                     
def folderStat2 = readFile('folderStat2.txt').trim()
                     
                     
if (folderStat1 == folderStat2) {
                        slackSend
(
                           color
: '#199515',
                           message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                       
)
                     
}
                     
else {
                        sh
"""
                           cd ${repoName}
                           
                           git config --global <a href="http://user.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fuser.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFu0SPm6QpHZ5742pOucYZFgv7vIQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fuser.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFu0SPm6QpHZ5742pOucYZFgv7vIQ&#39;;return true;">user.name "
****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master
                       
                           cd ..
                           rm -rf ${repoName}
                        """

                       
                        slackSend
(
                           color
: '#199515',
                           message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                       
)
                     
}
                 
}
               
}
           
}
         
}
     
}
   
}
   
   post
{
      failure
{
         slackSend
(
            color
: '#F01717',
            message
: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         
)
     
}
   
}
}



On Sunday, July 30, 2017 at 10:48:39 PM UTC+3, Joachim Nilsson wrote:
As I understand, it is written in Groovy. That means you can access a variable in a (Groovy)string using dollar and optionally curly brackets.

"  variable value is ${variable} "

Skaffa <a href="https://aka.ms/ghei36" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Faka.ms%2Fghei36\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFU8yON1EsfNLPhgUtO6Jc_yaEEpA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Faka.ms%2Fghei36\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFU8yON1EsfNLPhgUtO6Jc_yaEEpA&#39;;return true;">Outlook för Android



Från: Idan Adar
Skickat: söndag 30 juli 13:56
Ämne: Accessing a variable in shell
Till: Jenkins Users


Given the following script block in a stage (Declarative pipeline), how can I access the repoName variable?

stages {
    stage("...") {
        script {
            for (String repoName: repoList) {
                sshagent (credentials: ['e276113e-0ec9-4eaa-88f9-a7db5c9635b6']) {
                    sh """
                        git clone -b master git@****.com:****/repoName.git
                        cd repoName
                        ....
                    """
                }
            }
         }
     }
}

--
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 jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;"> https://groups.google.com/d/msgid/jenkinsci-users/d9d247a5-8ab9-4154-99c5-8c77b0861ec2%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


--
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="xPWXgeFIBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/f5ba050e-2d42-47d4-8183-89e43edc6fec%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/f5ba050e-2d42-47d4-8183-89e43edc6fec%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/f5ba050e-2d42-47d4-8183-89e43edc6fec%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/f5ba050e-2d42-47d4-8183-89e43edc6fec%40googlegroups.com.

For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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/afd21250-1d83-4f7f-a0fb-a1e97cec0298%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Idan Adar
I've made some changes and now I'm getting:

java.lang.NullPointerException: Cannot get property '$repoName' on null object




def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline {
   agent {
      label '****' 
   }

   stages {
      stage ("Update package.json") {
         steps {
            script {
               for (String repoName : repoList) {
                  sshagent (credentials: ['****']) {
                     sh '''
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                        stat -t . > folderStat1.txt
                     '''

                     for (String moduleName : moduleList) {
                        sh '''
                           cd ${repoName}
                           ncu -u -f "${moduleName}"
                           stat -t . > folderStat2.txt
                        '''
                     }

                     def folderStat1 = readFile('folderStat1.txt').trim()
                     def folderStat2 = readFile('folderStat2.txt').trim()

                     if (folderStat1 == folderStat2) {
                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                        )
                     }
                     else {
                        sh '''
                           cd ${repoName}

                           git config --global user.name "****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master

                           cd ..
                           rm -rf ${repoName}
                        '''

                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                        )
                     }
                  }
               }
            }
         }
      }
   }

   post {
      failure {
         slackSend (
            color: '#F01717',
            message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         )
      }
   }
}

--
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/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Michael Pailloncy
This syntax doesn't exist in Groovy : for (String repoName : repoList) { ... }
The direct equivalent is : for (String repoName in repoList) { ... }

However, it's better to use the C-style for loop syntax when using pipeline (that's why I've used it in my previous example) :

for (int i = 0; i < moduleList.size(); i++) {
   def moduleName = moduleList[i]
   ...
}


Cheers

2017-08-08 14:31 GMT+02:00 Idan Adar <[hidden email]>:
I've made some changes and now I'm getting:

java.lang.NullPointerException: Cannot get property '$repoName' on null object




def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline {
   agent {
      label '****' 
   }

   stages {
      stage ("Update package.json") {
         steps {
            script {
               for (String repoName : repoList) {
                  sshagent (credentials: ['****']) {
                     sh '''
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                        stat -t . > folderStat1.txt
                     '''

                     for (String moduleName : moduleList) {
                        sh '''
                           cd ${repoName}
                           ncu -u -f "${moduleName}"
                           stat -t . > folderStat2.txt
                        '''
                     }

                     def folderStat1 = readFile('folderStat1.txt').trim()
                     def folderStat2 = readFile('folderStat2.txt').trim()

                     if (folderStat1 == folderStat2) {
                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                        )
                     }
                     else {
                        sh '''
                           cd ${repoName}

                           git config --global user.name "****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master

                           cd ..
                           rm -rf ${repoName}
                        '''

                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                        )
                     }
                  }
               }
            }
         }
      }
   }

   post {
      failure {
         slackSend (
            color: '#F01717',
            message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         )
      }
   }
}

--
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/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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/CAPO77c0m30aQaGft62iBKmAvk4pDWmCPDEywUir_Yv-Kh03DoQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Michael Pailloncy
Oh just realized that this for loop syntax exists even in Groovy :-) (I'm used to using each instead of for loops)
But please try using C-style for loop syntax.

2017-08-08 14:45 GMT+02:00 Michael Pailloncy <[hidden email]>:
This syntax doesn't exist in Groovy : for (String repoName : repoList) { ... }
The direct equivalent is : for (String repoName in repoList) { ... }

However, it's better to use the C-style for loop syntax when using pipeline (that's why I've used it in my previous example) :

for (int i = 0; i < moduleList.size(); i++) {
   def moduleName = moduleList[i]
   ...
}


Cheers

2017-08-08 14:31 GMT+02:00 Idan Adar <[hidden email]>:
I've made some changes and now I'm getting:

java.lang.NullPointerException: Cannot get property '$repoName' on null object




def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline {
   agent {
      label '****' 
   }

   stages {
      stage ("Update package.json") {
         steps {
            script {
               for (String repoName : repoList) {
                  sshagent (credentials: ['****']) {
                     sh '''
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                        stat -t . > folderStat1.txt
                     '''

                     for (String moduleName : moduleList) {
                        sh '''
                           cd ${repoName}
                           ncu -u -f "${moduleName}"
                           stat -t . > folderStat2.txt
                        '''
                     }

                     def folderStat1 = readFile('folderStat1.txt').trim()
                     def folderStat2 = readFile('folderStat2.txt').trim()

                     if (folderStat1 == folderStat2) {
                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                        )
                     }
                     else {
                        sh '''
                           cd ${repoName}

                           git config --global user.name "****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master

                           cd ..
                           rm -rf ${repoName}
                        '''

                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                        )
                     }
                  }
               }
            }
         }
      }
   }

   post {
      failure {
         slackSend (
            color: '#F01717',
            message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         )
      }
   }
}

--
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/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
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/CAPO77c27cxQnf7zRg5UEODMOWRO3BtNRQ0RjBPFHp%2B6Q88oMvA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Idan Adar
Still failing:

[Commons Updater] Running shell script
+ git clone -b master git@****.com:****/.git
Cloning into 'security-services'...
ERROR: Repository not found.
fatal: Could not read from remote repository.

This happens because the the variable is missing when trying to execute the shell command:


for (int i = 0; i < repoList.size(); i++) {
                 
def repoName = repoList[i]
                 
                  sshagent
(credentials: ['credential-id']) {
                     sh
'''
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                        ...

Note how it attempts to clone /.git instead of /the-repo-name.git.
I know that the repoList is occupied with the repo names because if I will echo it, I see the repo name.

def repoList = ReposToUpdate.tokenize(",");

echo repoList
[0]


On Tuesday, August 8, 2017 at 3:54:17 PM UTC+3, mpapo - Michael Pailloncy wrote:
Oh just realized that this for loop syntax exists even in Groovy :-) (I'm used to using each instead of for loops)
But please try using C-style for loop syntax.

2017-08-08 14:45 GMT+02:00 Michael Pailloncy <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="F0mB1MlcBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">michael....@...>:
This syntax doesn't exist in Groovy : for (String repoName : repoList) { ... }
The direct equivalent is : for (String repoName in repoList) { ... }

However, it's better to use the C-style for loop syntax when using pipeline (that's why I've used it in my previous example) :

for (int i = 0; i < moduleList.size(); i++) {
   def moduleName = moduleList[i]
   ...
}

See <a href="https://github.com/jenkinsci/pipeline-examples/blob/master/docs/BEST_PRACTICES.md#groovy-gotchas" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fjenkinsci%2Fpipeline-examples%2Fblob%2Fmaster%2Fdocs%2FBEST_PRACTICES.md%23groovy-gotchas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFueR6S3IEnh9zxVohOm12pNRaG2Q&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fjenkinsci%2Fpipeline-examples%2Fblob%2Fmaster%2Fdocs%2FBEST_PRACTICES.md%23groovy-gotchas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFueR6S3IEnh9zxVohOm12pNRaG2Q&#39;;return true;">https://github.com/jenkinsci/pipeline-examples/blob/master/docs/BEST_PRACTICES.md#groovy-gotchas

Cheers

2017-08-08 14:31 GMT+02:00 Idan Adar <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="F0mB1MlcBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">id...@...>:
I've made some changes and now I'm getting:

java.lang.NullPointerException: Cannot get property '$repoName' on null object




def repoList = ReposToUpdate.tokenize(",");
def moduleList = npmDependencies.tokenize(",");

pipeline {
   agent {
      label '****' 
   }

   stages {
      stage ("Update package.json") {
         steps {
            script {
               for (String repoName : repoList) {
                  sshagent (credentials: ['****']) {
                     sh '''
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                        stat -t . > folderStat1.txt
                     '''

                     for (String moduleName : moduleList) {
                        sh '''
                           cd ${repoName}
                           ncu -u -f "${moduleName}"
                           stat -t . > folderStat2.txt
                        '''
                     }

                     def folderStat1 = readFile('folderStat1.txt').trim()
                     def folderStat2 = readFile('folderStat2.txt').trim()

                     if (folderStat1 == folderStat2) {
                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies match the latest package versions."
                        )
                     }
                     else {
                        sh '''
                           cd ${repoName}

                           git config --global <a href="http://user.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fuser.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFu0SPm6QpHZ5742pOucYZFgv7vIQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fuser.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFu0SPm6QpHZ5742pOucYZFgv7vIQ&#39;;return true;">user.name "****"
                           git config --global user.email ****
                           git commit -am 'Bump common packages version number [ci skip]'
                           git push origin master

                           cd ..
                           rm -rf ${repoName}
                        '''

                        slackSend (
                           color: '#199515',
                           message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> ${repoName}: Common code dependencies successfully updated to the latest package versions."
                        )
                     }
                  }
               }
            }
         }
      }
   }

   post {
      failure {
         slackSend (
            color: '#F01717',
            message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, Update failed. Review the build logs."
         )
      }
   }
}

--
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="F0mB1MlcBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/54962dc1-e80e-4ce3-8583-652a1fd714fe%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/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com.

For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


--
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/0ef4f753-7026-4794-a162-d882d8b2701e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Jenkins users mailing list

 
                    sh '''
 

                       ^^^

You are using a triple-single-quoted string. Replacement of groovy vars happens only in single- or triple-double-quoted strings.
 
 So use:
 sh """





--
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/1c8c7660-8963-4ad7-8bf2-f2e6ef50a099%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Accessing a variable in shell

Idan Adar
Damn. I had those there before...

Thanks. The combination of the more standard loop and the return of the double-quotes now made it work.

On Tuesday, August 8, 2017 at 6:37:28 PM UTC+3, Björn Pedersen wrote:

 
                    sh '''
 

                       ^^^

You are using a triple-single-quoted string. Replacement of groovy vars happens only in single- or triple-double-quoted strings.
 
 So use:
 sh """





--
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/54de66df-3616-4b42-a66c-13a0bb8e3a25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...