Jenkinsfile: publish test results even if test step fails - jenkins

In my Jenkinsfile I have a stage Test where I run a npm test command step as well as a junit step to archive test results.
stage('Test') {
steps {
sh 'npm run test-ci'
junit 'test-results.xml'
}
}
How can I use try/finally correctly to run the junit step even if the sh 'npm run test-ci' step fails?

You want to use the post stage, https://jenkins.io/doc/book/pipeline/syntax/#post.
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'npm run test-ci'
}
}
post {
always {
junit 'test-results.xml'
}
}
}
Also have a look at this blog post, it explains it further, https://jenkins.io/blog/2017/02/10/declarative-html-publisher/

Related

Running playwright tests stage stuck in running state endlessly on Jenkins

I created a Jenkins pipeline that runs dockerize the frontend app, build it and run playwrite test cases.
My problem is that, the running tests stage doesn't move to the next step after running all tests.
Jenkins file:
#!groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo 'Clean workspace'
cleanWs()
echo 'Checking out the PR'
checkout scm
}
}
stage('Build') {
steps {
echo 'Destroy Old Build'
echo 'Building'
sh 'make upbuild_d'
}
}
stage('Lint') {
steps {
echo 'Checking Lint'
sh 'make lint'
}
}
stage('Test') {
steps {
echo 'Running Tests ...'
sh 'make test-e2e'
}
}
}
// [StagePost] Clean after finishing
post {
always {
echo '## BEGIN ALWAYS BLOCK ##'
echo 'Destroy Build'
sh 'make destroy'
cleanWs()
echo '## END ALWAYS BLOCK ##'
}
}
}
Here is the make test-e2e in Makefile
test-e2e:
docker exec my-container bash -c 'npm run test:e2e'
And this is the test:e2e script npx playwright test --project=chromium
How can Jenkins detect that all tests are already run to execute the post steps?
This issue occurred because of this line in playwright.config.js reporter: 'html'.
This results in trying to open the test report in a browser that requires a GUI which isn't found inside the container, so the process hangs. It is fixed by updating the reporter config as reporter: [['html', { open: 'never' }]]

SonarQube Access denied in Jenkins Pipeline

I'm trying to set up my Jenkins Pipeline to work along with SonarQube, However at the building stage Im getting the following error message:
/var/jenkins_home/workspace/practica3/node_modules/sonar-scanner/bin/sonar-scanner: exec: line 59: : Permission denied
I have the next Jenkins pipeline file:
pipeline {
agent {
docker {
image 'node:17-alpine3.14'
args '-p 3001:3000'
}
}
stages {
stage('Build') {
steps {
sh 'npm install'
withSonarQubeEnv('SonarQube'){
sh "npm install sonar-scanner"
sh "npm run sonar"
}
}
}
stage('Test') {
steps {
sh 'npm run test'
}
}
}
}
My sonar-project.properties looks like this:
sonar.projectKey=nodejs-app
sonar.projectName=nodejs-app
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.host.url=http://localhost:9000
sonar.login=MyToken
sonar.javascript.lcov.reportPaths=coverage/lcov.info
Any Ideas? I will appreciate it.

Run unit tests and make unit test report available within jenkins

Newbie to jenkins.
Experimenting to run unit tests (not even sure if the step to run is correct)
and also need to make a report available within jenkins (any suggestions how i can make this possible?)
pipeline {
agent any
stages
{
stage("Build") {
steps {
echo 'Building the appication...'
}
}
stage ('Unit test') {
steps {
sh 'npm run test'
}
}
stage ("Deploy") {
steps {
echo 'Deploying the appication...'
}
}
}
}
Use for example xunit tool (can be invoked on post actions or simply as step) to record a report from tests.
Here is the documentation:
https://plugins.jenkins.io/xunit/
To create a report with npm run test command you have to define it in package.json of the project or wherever the command is defined
Rwmeber also to add script { after steps { in the stage where you call Bash script

How to run a stage only when build is launched manually

My team owns a non regression testing project. In this project, there is code and non regression test. Like classic project we want to analyse our code with linter or other tools. But we don't want to run our tests on each branch for each commit, they last hours.
We want to launch these tests manually.
To run test exclusively on master we have this in our Jenkinsfile:
stage("Test") {
when {branch "master"}
steps {
sh 'pipenv run pytest -n5 --dist=loadscope --junitxml report.xml |
}
post {
always {
junit 'report.xml'
}
}
}
But once we merge our branch into master, a build on master is triggered and the tests are launched.
To avoid this, I think I have to play with the triggeredBy parameter of the when block: https://jenkins.io/doc/book/pipeline/syntax/
But I cannot find which triggeredBy map a manual launch event (event that is sent when we click on the run button within Jenkins interface).
Thx for your help. The following code behaves as expected.
stage("Test") {
when {allOf {branch "master"; triggeredBy 'UserIdCause'}}
steps {
sh 'pipenv run pytest -n5 --dist=loadscope --junitxml report.xml '
}
post {
always {
junit 'report.xml'
}
}
}
You can use this:
stage('Test') {
when {
expression {
currentBuild.buildCauses.toString().contains('UserIdCause')
}
}
steps {
sh 'pipenv run pytest -n5 --dist=loadscope --junitxml report.xml
}
}

Jenkins Pipeline: How to archive artifacts when the build fails?

When our browser based tests fail, we take a screenshot of the browser window to better illustrate the problem. However, I don't understand how to archive them in my pipeline, because the pipeline stops after the failure. Same for the junit.xml, I'd also like to use it in error cases.
I've checked, the screenshots are generated and stored correctly.
My definition looks like this (irrelevant things mostly trimmed):
node {
stage('Build docker container') {
checkout([$class: 'GitSCM', ...])
sh "docker build -t webapp ."
}
stage('test build') {
sh "mkdir -p rspec screenshots"
sh "docker run -v /var/jenkins_home/workspace/webapp/rspec/junit.xml:/myapp/junit.xml -v /var/jenkins_home/workspace/webapp/screenshots:/myapp/tmp/capybara -v webapp bundle exec rspec"
}
stage('Results') {
junit 'rspec/junit*.xml'
archive 'screenshots/*'
}
}
You can use simple Java try/catch to avoid pipeline failure on test failure, or Jenkins catchError like this :
node {
catchError {
// Tests that might fail...
}
// Archive your tests artifacts
}
From here, you can use the post section in your pipeline:
pipeline {
agent any
stages {
stage('Build') {
...
}
stage('Test') {
...
}
}
post {
always {
archive 'build/libs/**/*.jar'
}
}
}

Resources