Jenkins: java.lang.NoSuchMethodError: There is no such DSL method when executing a shell script inside groovy

Being new to groovy, I tried to execute a shell script as part of the Jenkins build pipeline inside groovy, as shown 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 an exception, as shown below:

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

Unlucky even after many decisions over the internet, the shell script inside groovy is the problem here. Any problem with the syntax?

+5
source share
1 answer

Yes, the syntax is bad. Basically the problem that I see is related to your quoting shell command. This is not true:

 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 exit the single quote for {print $4} , which is probably interpreted as closing groovy.

I'm not sure I understand why it gives an error, but I think that 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'm not sure what the next echo line will work. Firstly, AMD_ID does not exist where you use echo . it existed only in a shell. In addition, $ AMD_ID does not exist as a valid groovy variable. I'm not quite sure what you are trying to do with this echo expression, but if it really works, it will not do what you expect from it.

+2
source

Source: https://habr.com/ru/post/1272930/


All Articles