How to add a table to an email with the build result?

I have a problem. In Jenkins, I run automatic tests using Postman. When the build is done, an e-mail notification is sent with an attachment with the logs. In Jenkins, Newman returns me a table with the results:

I would like only such a table with the result to be in the e-mail. table

I did more or less as it is given here, but I don’t know what to enter in the attachemnt field. I typed testreport.html and created a Workspace file with the same name, but after building it is empty

I don’t know about about sending out emails, so I can’t directly help.

That being said, more information would be useful.

Are you using freestyle or pipeline?
What step/script/etc are you using to send out emails (there’s a few options for sending them out).
Do you have the “table” writing to a file already? or do you need help with that?

More data is more better.

hi @halkeye, thanks for the quick reply.

The project looks exactly the same as described here:

Are you using freestyle or pipeline? - freestyle

What step/script/etc are you using to send out emails (there’s a few options for sending them out). - Email Extension - Jenkins Plugins

Do you have the “table” writing to a file already? or do you need help with that? - I made the table as it is given here (I copied and edited a little)

I created a file in the workspace called testrpr.html but it creates an empty one and the e-mail has a “white field”

testrepr

The main goal: so after a build I get a log file, from that file I want to extract a table of fail/pass tests and place it inside the mail. The whole log file could be attached to a mail if any of the test scipts would fail.

We will have to reach out to them. Freestyle is way less flexible and hasn’t been the recommended solution for a while now. Highly recommend pipeline. Pipeline is pretty simple with node. If you have a public repo I might even be up for submitting a pr with a jenkinsfile

I’m still confused. Can you confirm via shell (I think it’s called execute shell step) that the file exists and isn’t 0 bytes.

Cause I’m not sure if the problem is that it’s not emailing correctly or not generating the file correctly.

Also can you screenshot your email section of the job and post it for us to see?

@halkeye

hmm, maybe i’ll use a plugin to convert freestyle to pipeline and then send you a jenkinsfile? I don’t know where I can find (and is there any jenksinfile in the freestyle project)

I’m still confused. Can you confirm via shell (I think it’s called execute shell step) that the file exists and isn’t 0 bytes. - -rw-r--r-- 1 root root 0 03-02 12:40 testrepr.html

Cause I’m not sure if the problem is that it’s not emailing correctly or not generating the file correctly. - after a build I get a log file, from that file I want to extract a table of fail/pass tests and place it inside the mail. The whole log file could be attached to a mail if any of the test scipts would fail

Also can you screenshot your email section of the job and post it for us to see? -

So that looks looks like the issue.your file is 0 bytes. The email is working fine.

I think there has been a few attempts but I dont know of any good reliable converters. Pipeline isn’t hard learn.

Ok, let’s start from the beginning. I wrote a pipeline, downloaded the HTML Publisher plugin. Unfortunately I couldn’t find any report on the server /var/reports/newman/html

Pipeline:

pipeline {
    stage('Testy administracja DEV EU2') {
        steps {
            sh 'ssh -T -i /var/lib/jenkins/.ssh/id_rsa jenkins@127.0.0.1 newman run /files/testy_administracja.json -e /files/DEV.postman.environment.json -k  -r junit,html --reporter-junit-export var/reports/newman/junit/newman.xml --reporter-html-export var/reports/newman/html/index.html'
            
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'var/reports/newman/html', reportFiles: 'index.html', reportName: 'Testy automatyczne', reportTitles: ''])
        }
    }
}

Zrzut ekranu 2022-03-03 153727
So now I need to add the e-mail code in the pipeline, but why the report hasn’t been done (on the server)?

so it connects to another machine (jenkins@localhost, so itself?) then runs a command, and puts the files in var/reports/newman based on the home directory of the jenkins user?

Is the goal to escape a jenkins docker container or something? if so, you may want to add jenkins@127.0.0.1 as a ssh agent/node, and then you can select that agent via node, and not have to worry about passing data across ssh connection

then you publish the HTML of the files based on the $WORKSPACE/var/reports/newman/html of the job

Those paths are unlikely to match up.

If you can get away with not sshing things should work fine, if you must ssh, then you need to write to somewhere jenkins has access to, maybe somewhere related to $WORKSPACE?

As for the email, there’s a pipeline syntax generator link on the sidebar that will help you write new steps like email.

@halkeye

I deleted the ssh path (it’s on one machine).

Unfortunately in jenkins’ home directory I do not see the WORKSPACE of this pipeline. When I create a freestyle project, a directory in workspace is created.

If I want this report to be created in the pipeline workspace directory, it will be ok this way?


pipeline {
    stage('Postman tests') {
        steps {
            sh 'newman run /uusia/testy_administracja.json -e /uusia/DEV.postman.environment.json -k  -r junit,html --reporter-junit-export var/reports/newman/junit/newman.xml --reporter-html-export var/reports/newman/html/index.html'
            
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '$WORKSPACE', reportFiles: 'index.html', reportName: 'Testy automatyczne', reportTitles: ''])
        }
    }
}

I don’t know how to complete these paths yet.

--reporter-junit-export var / reports / newman / junit / newman.xml
 --reporter-html-export var / reports / newman / html / index.html

this probably won’t work. Single quotes means that groovy won’t interpret it, and let it gets passed into the next level. I doubt publishHTML handles variables. I don’t think you need the parameter. I think you should just have

reportDir: 'var/reports/newman/html'

Otherwise it seems okay to me.

Edit: Arg, i don’t have publishHTML installed to test it out

Okay, got it tested, took some creative liberties. The HTML is blank in the example collection i found online, but the junit XML works great

https://ci.g4v.dev/job/demos/job/newman/

pipeline {
    agent {
        // // I don't have newman on my jenkins machine so using docker
        docker { 
            image 'node:lts'
            args '-e "HOME=$WORKSPACE"'
        }
    }
    stages {
        stage('Postman tests') {
            steps {
                sh '''
                  npm init -y
                  npm install newman newman-reporter-html
                  npx newman run \
                    "https://www.getpostman.com/collections/8a0c9bc08f062d12dcda" \
                    -r junit,html \
                    --reporter-junit-export var/reports/newman/junit/newman.xml \
                    --reporter-html-export var/reports/newman/html/index.html
                '''
            }
            // make sure we always attempt to parse the reports, even newman returns exit code other than 0
            post {
                always {
                    publishHTML([
                        allowMissing: false,
                        alwaysLinkToLastBuild: false,
                        keepAll: false,
                        reportDir: 'var/reports/newman',
                        reportFiles: 'index.html',
                        reportName: 'Testy automatyczne',
                        reportTitles: ''
                    ])
                    
                    junit allowEmptyResults: true, testResults: ' var/reports/newman/junit/newman.xml'
                }
            }
        }
    }
}

Why can’t I see the directory “Tests administracja DEV EU 2 - Pipeline” in workspace (bash)? Others I can see (e.g. created by a freestyle project)

When I go to console output, I also do not see the tests being carried out. I can see them in the Freestyle project.

I also can’t see the HTML Reports tab: / …

what does this mean? how are you checking? I’m not really sure I understand what isn’t working so can’t really give help.

When I go to build I don’t see more steps than “Start of Pipeline - (no timing in block)”.

in Bash in path /var/lib/jenkins/workspace I don’t see the directory of this pipeline -
“Tests administracja DEV EU 2 - Pipeline”

I’m sorry, I assumed this was part of a larger pipeline that you spliced up, but as is, it isn’t valid. If you look at the console log for the build you should see error messages like below

https://ci.g4v.dev/job/demos/job/dawid-newman/1/console

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 2: Undefined section "stage" @ line 2, column 5.
       stage('Postman tests') {
       ^

WorkflowScript: 1: Missing required section "stages" @ line 1, column 1.
   pipeline {
   ^

WorkflowScript: 1: Missing required section "agent" @ line 1, column 1.
   pipeline {
   ^

At the very least, you need an agent and stages

pipeline {
    agent any
    
    stages {
        stage('Postman tests') {
            steps {
                sh 'newman run /uusia/testy_administracja.json -e /uusia/DEV.postman.environment.json -k  -r junit,html --reporter-junit-export var/reports/newman/junit/newman.xml --reporter-html-export var/reports/newman/html/index.html'
                
                publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '$WORKSPACE', reportFiles: 'index.html', reportName: 'Testy automatyczne', reportTitles: ''])
            }
        }
    }   
}

The above runs on my server - demos » dawid-newman #3 Console [Jenkins] - though I don’t have newman installed, so it fails on the sh() step.

My version of the pipeline (in the previous post, using docker) does run - demos » halkeye-newman #1 Console [Jenkins] but I don’t have your newman configs to confirm anything else.

I’d really appreciate your full pipeline and console logs if your comfortable sharing because something is breaking and its hard to guess what.

Just on Friday I added the agent any section to the pipeline. My pipeline is what you have in total, I post here how it looks:

 pipeline {
    agent { any
        }
    }
    stages {
        stage('Postman tests') {
            steps {
                sh '''
                  npm init -y
                  npm install newman newman-reporter-html
                  npx newman run \
                    "/uusia/testy_administracja.json -e /uusia/DEV.postman.environment.json -k 
" \
                    -r junit,html \
                    --reporter-junit-export var/reports/newman/junit/newman.xml \
                    --reporter-html-export var/reports/newman/html/index.html
                '''
            }
            post {
                always {
                    publishHTML([
                        allowMissing: false,
                        alwaysLinkToLastBuild: false,
                        keepAll: false,
                        reportDir: '/var/reports/newman',
                        reportFiles: 'index.html',
                        reportName: 'Testy automatyczne',
                        reportTitles: ''
                    ])
                    
                    junit allowEmptyResults: true, testResults: '/var/reports/newman/junit/newman.xml'
                }
            }
        }
    }

Log: java.lang.NoSuchMethodError: No such DSL method 'stages' found among steps [addBadge, addErrorBadge, addHtmlBadge, addInfoBadge, addShortText, addWarningBadge, archive, bat, build, catchError, checkout, createSummary, deleteDir, dir, echo, emailext, emailextrecipients, error, fileExists, getContext, git, input, isUnix, junit, load, lock, mail, milestone, node, parallel, powershell, publishChecks, publishHTML, pwd, pwsh, readFile, release, removeBadges, removeHtmlBadges, removeSummaries, retry, sh, sleep, stage, stash, step, svn, timeout, timestamps, tm, tool, unarchive, unstable, unstash, waitUntil, warnError, withChecks, withContext, withCredentials, withEnv, withGroovy, wrap, writeFile, ws] or symbols [GitUsernamePassword, all, allBranchesSame, always, ant, antFromApache, antOutcome, antTarget, apiToken, architecture, archiveArtifacts, artifactManager, audit-trail, authorizationMatrix, batchFile, bitbucketServer, booleanParam, brokenBuildSuspects, brokenTestsSuspects, buildButton, buildDiscarder, buildDiscarders, buildRetention, buildTimestamp, buildTimestampExtraProperties, buildUser, builtInNode, caseInsensitive, caseSensitive, certificate, chlAtuoAction, choice, choiceParam, cleanWs, clock, command, configFile, configFileProvider, credentials, cron, crumb, culprits, default, defaultFolderConfiguration, defaultView, demand, developers, disableConcurrentBuilds, disableResume, dockerCert, dockerServer, dockerTool, downstream, dumb, durabilityHint, email-ext, envInject, envVars, envVarsFilter, executor, extendedEmailPublisher, file, fileParam, filePath, fingerprint, fingerprints, frameOptions, freeStyle, freeStyleJob, fromDocker, fromScm, fromSource, git, gitBranchDiscovery, gitHubBranchDiscovery, gitHubBranchHeadAuthority, gitHubExcludeArchivedRepositories, gitHubExcludeForkedRepositories, gitHubExcludePublicRepositories, gitHubForkDiscovery, gitHubIgnoreDraftPullRequestFilter, gitHubPullRequestDiscovery, gitHubSshCheckout, gitHubTagDiscovery, gitHubTopicsFilter, gitHubTrustContributors, gitHubTrustEveryone, gitHubTrustNobody, gitHubTrustPermissions, gitParameter, gitTagDiscovery, gitUsernamePassword, github, githubProjectProperty, githubPush, globalConfigFiles, groovy, headRegexFilter, headWildcardFilter, hyperlink, hyperlinkToModels, inheriting, inheritingGlobal, installSource, javadoc, jdk, jdkInstaller, jgit, jgitapache, jnlp, jobBuildDiscarder, jobName, junitTestResultStorage, lastDuration, lastFailure, lastGrantedAuthorities, lastStable, lastSuccess, legacy, list, local, location, logRotator, loggedInUsersCanDoAnything, mailer, masterBuild, maven, maven3Mojos, mavenErrors, mavenGlobalConfig, mavenMojos, mavenWarnings, myView, namedBranchesDifferent, nodeProperties, nodejs, nodejsci, nonInheriting, none, organizationFolder, overrideIndexTriggers, paneStatus, parameters, password, pattern, permanent, pipeline, pipelineTriggers, plainText, plugin, pollSCM, projectNamingStrategy, proxy, pruneTags, queueItemAuthenticator, quietPeriod, rateLimit, rateLimitBuilds, recipients, requestor, resourceRoot, retainOnlyVariables, run, runParam, sSHLauncher, schedule, scmRetryCount, scriptApproval, scriptApprovalLink, search, security, shell, simpleBuildDiscarder, slave, sourceRegexFilter, sourceWildcardFilter, ssh, sshPublicKey, sshPublisher, sshPublisherDesc, sshTransfer, sshUserPrivateKey, standard, status, string, stringParam, suppressAutomaticTriggering, swapSpace, teamSlugFilter, text, textParam, timestamper, timestamperConfig, timezone, tmpSpace, toolLocation, unsecured, untrusted, upstream, upstreamDevelopers, userSeed, usernameColonPassword, usernamePassword, viewsTabBar, weather, withAnt, workspace, x509ClientCert, zip] or globals [currentBuild, env, manager, params] `

You have 1 open and 2 close braces here. If you clean that up it should run

Ok, build is successfulbut nothing happened

Log:
Uruchomiono przez użytkownika [Dawid] [Pipeline]
End of Pipeline
Finished: SUCCESS

only 1 step: Start of Pipeline - no timing in block.

Pipeline:


pipeline {
    agent { any
        }
    stages {
        stage('Postman tests') {
            steps {
                sh '''
                  npm init -y
                  npm install newman newman-reporter-html
                  npx newman run \
                    "/uusia/testy_administracja.json -e /uusia/DEV.postman.environment.json -k" \
                    -r junit,html \
                    --reporter-junit-export var/reports/newman/junit/newman.xml \
                    --reporter-html-export var/reports/newman/html/index.html
                '''
            }
            post {
                always {
                    publishHTML([
                        allowMissing: false,
                        alwaysLinkToLastBuild: false,
                        keepAll: false,
                        reportDir: '/var/reports/newman',
                        reportFiles: 'index.html',
                        reportName: 'Testy automatyczne',
                        reportTitles: ''
                    ])
                    
                    junit allowEmptyResults: true, testResults: '/var/reports/newman/junit/newman.xml'
                }
            }
        }
    }
}

This Pipeline has run successfully, but does not define any stages. Please use the stage step to define some stages in this Pipeline.