简体   繁体   中英

Jenkins pipeline: java.lang.NoSuchMethodError: No such DSL method, while executing shell script inside groovy

Being new to groovy, I was trying to execute shell script as part of build pipeline in Jenkins inside groovy like below :

stage('AMI ID EXTRACTION') {
        sh 'AMI_ID=$(grep artifact_id /opt/repository/jenkins/workspace/crspng-CCPDev-ccp-ft-AMI/manifest.json | awk -F "\"" '{print $4}'| awk -F ":" '{print $2}')'
        echo $AMI_ID
      }

stage ('ft-ami-extraction')
{
      build job: 'crspng-CCPDev-ami-extraction'
}

But ending up getting the exception like below:

java.lang.NoSuchMethodError: No such DSL method 'AMI_ID=$(grep artifact_id /opt/repository/jenkins/workspace/crspng-CCPDev-ccp-ft-AMI/manifest.json | awk -F """ '

No luck even after trying many solutions over internet, shell script inside groovy is the challenge here. Any issue with syntax ?

Yes, the syntax is bad. Mostly the problem I see has to do with your quoting of the shell command. This is not valid:

sh 'AMI_ID=$(grep artifact_id /opt/repository/jenkins/workspace/crspng-CCPDev-ccp-ft-AMI/manifest.json | awk -F "\"" '{print $4}'| awk -F ":" '{print $2}')'

You are breaking out of the single quoted string for {print $4} , which is probably being interpreted as a groovy closure.

I am not sure I understand why it is giving the error it is, but I think if you could most easily solve it by triple single quoting your shell command:

sh '''AMI_ID=$(grep artifact_id /opt/repository/jenkins/workspace/crspng-CCPDev-ccp-ft-AMI/manifest.json | awk -F "\"" '{print $4}'| awk -F ":" '{print $2}')'''

I am not sure if the next echo line will work, though, either. First of all, AMD_ID doesn't exist where you are running the echo . it only existed in the shell. Also, $AMD_ID doesn't exist as a valid groovy variable. I'm not completely sure what you are attempting to do with that echo statement, but if it actually runs, it is not going to do what you expect it to do.

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