I think the issue here is confusing line breaking between Java and shell… I’m presuming that this is what exists currently, based on the screenshot above:
stage ('SonarQube Analysis'){
steps{
script {
sh " echo Performing Sonar Analysis"
sh '''
cd Services/refinery-production
ls -l
sonarqube.sonarQubeAnalysis(
type: 'java',
environment: 'dev',
sonarQubeEnv: 'Sonarqube Dev',
scannerHome: '/opt/sonarqube/sonnar-scanner',
appKey: '',
appName: '',
currentVersion: '1.0',
sources: './src',
coverage: '/home/jenkins/workspace/Metals/Code-Build-Deploy-Backend/Services/refinery-production/target/site/jacoco/jacoco.xml',
coverageExclusions: './src/test/**',
tests; './src/directory',
testInclusions: './src/texts',)
'''
}
}
}
If this is the case, the error message above kind of makes sense. Your shell will go through the script line by line and try to execute it. So it’ll see the following commands:
cd Services/refinery-production
ls -l
sonarqube.sonarQubeAnalysis(
And that’s where it’ll error out. That command is missing a closing bracket, so it’ll say that a closing bracket is needed. Running this on my local Ubuntu instance in the Bourne shell gives me the message sh: 2: Syntax error: newline unexpected (expecting ")")
which sounds extremely similar to what you posted. But you’re not actually done the command yet.
To fix this, we need to tell the shell that we’re not done yet. That’s using backslashes at the end of each line that continues to the next line, like so:
stage ('SonarQube Analysis'){
steps{
script {
sh " echo Performing Sonar Analysis"
sh '''
cd Services/refinery-production
ls -l
sonarqube.sonarQubeAnalysis( \
type: 'java', \
environment: 'dev', \
sonarQubeEnv: 'Sonarqube Dev', \
scannerHome: '/opt/sonarqube/sonnar-scanner', \
appKey: '', \
appName: '', \
currentVersion: '1.0', \
sources: './src', \
coverage: '/home/jenkins/workspace/Metals/Code-Build-Deploy-Backend/Services/refinery-production/target/site/jacoco/jacoco.xml', \
coverageExclusions: './src/test/**', \
tests; './src/directory', \
testInclusions: './src/texts',)
'''
}
}
}
This way, the shell will come to the line that says sonarqube.sonarQubeAnalysis( \
, see the backslash, and know you’re not done yet.
This isn’t needed in Java because Java knows that if there’s an unclosed bracket, there’s more coming and it’ll continue to read. Shell isn’t like that.
Edited to add missing single quote in my transcription of the screenshot above. Thanks @halkeye!