Accessing a variable in shell

已查看 590 次
跳至第一个未读帖子

Idan Adar

未读,
2017年7月30日 07:56:472017/7/30
收件人 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
                        ....
                    """

               
}
           
}
         
}
     
}
}

Joachim Nilsson

未读,
2017年7月30日 15:48:392017/7/30
收件人 jenkins...@googlegroups.com
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} "

--
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.


Idan Adar

未读,
2017年8月7日 08:49:092017/8/7
收件人 Jenkins Users、Joachim...@miljodata.se
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."
         
)
     
}
   
}
}

Michael Pailloncy

未读,
2017年8月8日 02:49:282017/8/8
收件人 jenkins...@googlegroups.com
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.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f5ba050e-2d42-47d4-8183-89e43edc6fec%40googlegroups.com.

Idan Adar

未读,
2017年8月8日 03:05:592017/8/8
收件人 Jenkins Users
Hm, failed with the same error... full stack: https://pastebin.com/AL2J5MMS

Idan Adar

未读,
2017年8月8日 08:31:352017/8/8
收件人 Jenkins Users
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}

Michael Pailloncy

未读,
2017年8月8日 08:46:052017/8/8
收件人 jenkins...@googlegroups.com
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]

--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/54962dc1-e80e-4ce3-8583-652a1fd714fe%40googlegroups.com.

Michael Pailloncy

未读,
2017年8月8日 08:54:172017/8/8
收件人 jenkins...@googlegroups.com
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.

Idan Adar

未读,
2017年8月8日 11:31:152017/8/8
收件人 Jenkins Users
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]
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

Björn Pedersen

未读,
2017年8月8日 11:37:282017/8/8
收件人 Jenkins Users

 
                    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 """





Idan Adar

未读,
2017年8月8日 12:56:032017/8/8
收件人 Jenkins Users
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.

Joachim Nilsson

未读,
2017年9月28日 06:18:452017/9/28
收件人 jenkins...@googlegroups.com

Did you get this working?

Your problem was caused by mixing Groovy code within a shell command block.

 

                     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
                     """

 

should probably be:

 

                     sh """
                        git clone -b master git@****.com:****/${repoName}.git
                        cd ${repoName}
                           
                        stat -t . > folderStat1.txt

                        stat -t . > folderStat2.txt

                     """

                     for (String moduleName : moduleList) {

                        sh """


                           ncu -u -f "${moduleName}"

                        """

                     }

                    
Good luck!

 

Joachim

Idan Adar

未读,
2017年9月28日 06:43:332017/9/28
收件人 Jenkins Users
Indeed. This is long resolve. 
Thanks!
回复全部
回复作者
转发
0 个新帖子