Regarding Junit and Jacoco report publishing

I have one git repository from which I am building artifacts using Dynamic agent as ECS:
Project1 having top folder has Services/UI/DB and Automation
Automation contain jenkinsfile having maven test job.

  • Service
    • SRC
    • POm.xml
  • UI
  • DB
  • Automation
    • jenikinsfile-services
      My query is :slight_smile:
  1. Not able to publish the result
  2. How to use multiline shell command inside stage block

Hello @dxm007 and welcome to this community :wave:

To use a multiline shell command inside a stage block, you maybe could use the triple quotes ''' to define the shell script. Here’s an example:

stage('Publish Results') {
  steps {
    sh '''
      cd Automation
      mvn clean test
      cd target/surefire-reports
      cp -r * ${JENKINS_HOME}/test-results
    '''
    junit 'target/surefire-reports/*.xml'
  }
}

In this example, the cd command changes the directory to the Automation directory, then the mvn clean test command runs the Maven test job. The next cd command changes the directory to the target/surefire-reports directory, and the cp command copies the test result files to the ${JENKINS_HOME}/test-results directory.

The junit command then uses the *.xml pattern to find the test result files and publish them as JUnit test results.

Note that you would need to adjust the paths in this example to match the actual paths in your project.
Please note I did not take the time to test this example, so take it with a grain of salt.

@poddingue …thanks for your replay…
Looking for further support…i have one stage on my pipeline as mentioned below:
Stage (sonaranalysis){
Steps{
Sh ‘’’
Cd system/services
Sonarq analysis(
Env: ‘dev’,
Language: ‘java’,
Coverage: ‘test.xml’,)
‘’’
Error while execution: unexpected newline expecting (“)”)

Please assist me on this…thanks in advance

1 Like

@dxm007
can post the pipeline script if possible?

is this the script you have ?

stages {
        Stage('sonaranalysis') {
            Steps {
                sh '''
                cd system/services
                Sonarq analysis(
                Env: ‘dev’,
                Language: ‘java’,
                Coverage: ‘test.xml’,)
        '''
            }
}

stages {
Stage(‘sonaranalysis’) {
Steps {
sh ‘’’
cd system/services
Sonarq.analysis(
Env: ‘dev’,
Language: ‘java’,
Coverage: ‘test.xml’,)
‘’’
}
}
}

It is the script…please help me on this

what is Sonarq.analysis here? and you are passing some data into it

It is a function described in shared library and i am passing the argument to this function with different values required for sonar scanner.

When i build the pipeline and got shell error : unexpectedly new line…expected (“)”).

1 Like

It is a function described in shared library…i am using this funtion to do sonar analysis and passing multiple arguments to this.

While building the job , this stage throw below error:

Unexpected new line expecting (“)”).

try this @dxm007

stage('sonaranalysis') {
            steps {
                sh '''
                cd system/services
                Sonarq.analysis(
                Env: 'dev',
                Language: 'java',
                Coverage: 'test.xml',)
            '''
            }
        }

Hi,

Still facing the same issue…expected (“)”)…
Not able to proceed further…

Hi @dxm007 try this, if it doesn’t help then i will to reproduce the issue in my jenkins

Sonarq.analysis(
                Env: 'dev',
                Language: 'java',
                Coverage: 'test.xml').trim()

Hi Jana,

I tried all the permutation and combination, but no success.

Please help me on this.

Hm… something wrong with the script only
@dxm007 can you also post the screenshot of the pipeline stage which is failing

Hi JS,

Please find the attached screenshots and suggest me whats need to be done for issue …

Thank in advance

Thanks in advance

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:

  1. cd Services/refinery-production
  2. ls -l
  3. 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!

I’m going to disagree, this does not look like shell at all, it doesn’t have named parameters, and i’m not sure you can have dots in functions. This feels like a plugin or shared library.

stage ('SonarQube Analysis'){
  steps{
    dir('Services/refinery-production') {
       echo "Performing Sonar Analysis"
       sh('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', // <-- this probably won't work, plugins can't often escape $WORKSPACE
        coverageExclusions: './src/test/**', // <--  this was missing a quotes
        tests; './src/directory',
        testInclusions: './src/texts',)
      }
   }
}

That’s a good point, I didn’t even consider that. Looking back at the conversation, @dxm007 mentioned that “[sonarqube.sonarQubeAnalysis] is a function described in shared library”, which would make it a pipeline code to be executed. I had thought it was said that this was a shell function, but I must have misremembered that.

@dxm007, use @halkeye’s code above. Your issue was that you were including the function call to sonarqube.sonarQubeAnalysis, which is a Groovy function defined in a shared library, in your shell script. It needs to be executed within the pipeline itself. Also, you cannot execute cd in your shell script and then call a Groovy function; the cd call will only affect the shell session run from the sh step and won’t change the directory that Jenkins is operating in. Using the dir step as @halkeye has done does change the directory that Jenkins operates in and so your shared library’s function will execute in the Services/refinery-production directory with it.

Thanks everyone for their response