how to execute 2 newman tasks in parallely using jenkins.? - jenkins

How to Execute this below code in paralley.. please help iam new to jenkins.
parallel firstBranch: {
node{
newman run xyz.json
}
},
secondBranch:{
node{
newman run xyz123.json
}
}
},
failFast: true|false
for this i am gettig below error:
Also:
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution.cancel(CpsBodyExecution.java:244)
at org.jenkinsci.plugins.workflow.steps.BodyExecution.cancel(BodyExecution.java:76)
at org.jenkinsci.plugins.workflow.cps.steps.ParallelStepExecution.stop(ParallelStepExecution.java:67)
at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.checkAllDone(ParallelStep.java:147)
at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.onFailure(ParallelStep.java:134)
at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution$FailureAdapter.receive(CpsBodyExecution.java:349)
at com.cloudbees.groovy.cps.impl.ThrowBlock$1.receive(ThrowBlock.java:68)

try the Jenkins documentaiton exemple click in the link bellow :
Parallel stages with Declarative Pipeline 1.2
to use new man with jenkins it's very easy
install nodeJS and npm
install newman by `npm install -g newman
create json file newman-test:
{
"name": "postman-newman-jenkins",
"version": "1.0",
"description": "Project postman test",
"directories": {
"test": "tests"
},
"scripts": {
"api-tests-production": "newman run postman_collection.json --reporters cli,html --reporter-html-export ${Environment}-$BUILD_NUMBER-newman.html --reporter-html-template template.hbs
},
"author": "me or you",
"dependencies": {
"newman": "^3.5.2"
}
}
in you jenkins
try {
sh 'npm install'
sh 'npm run newman-test'
}
catch (Exception err) {
currentBuild.result = 'UNSTABLE'
}
to archive your report
archiveArtifacts 'jenkins/${Environment}-$BUILD_NUMBER-newman.html'
in the last you can format this code to respect declarative pipeline code

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.

Jenkinsfile: publish test results even if test step fails

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/

How to setup a Jenkins pipeline for postman tests

Postman is used as a popular testing tool for API testing, you can write bunch of unit tests using Postman and execute them as a part of your build process to perform unit testing. The following covers the Jenkins integration of Postman tests.
In order to do that you should have
Exported Postman tests as a collection
Make the APIs available at run time when the tests are performed. (Using docker or by creating a separate build pipeline.)
Node module newman can be used to execute Postman collections. Refer the following Package.json file. Here we are executing the postman collection inside the unit_tests folder using newman, also newman dependency is defined.
package.json
{
"name": "postman-newman-jenkins",
"version": "1.0.0",
"description": "My Test Project",
"directories": {
"tests": "tests"
},
"scripts": {
"newman-tests": "newman run unit_tests/my-collection.postman_collection.json --reporters cli,junit --reporter-junit-export newman.xml --insecure"
},
"author": "Test Author",
"dependencies": {
"newman": "^3.5.2"
}
}
The following is the content of the Jenkinsfile. We are using NPM to install the dependencies and execute tests.
Jenkinsfile
pipeline {
agent { label 'LinuxSlave' }
stages {
stage ('Checkout') {
steps {
checkout scm
}
}
stage('Test'){
steps {
sh 'npm install'
sh 'npm run newman-tests'
junit 'newman.xml'
}
}
}
}
you can avoid installing newman on the machine (slave/ master) and use docker
example pipeline script:
pipeline {
agent any stages {
stage('Test') {
steps {
sh 'docker run -t postman/newman_ubuntu1404 run https://www.getpostman.com/collections/8a0c9bc08f062d12dcda'
}
}
}
}
more info on docker & newman here

How to build a pipeline as shown in blue oceans beta project page

I am new to Jenkins and looking for ways to automate and visualise workflows. I am able to chain few workflows/jobs together.
I like to learn how to run workflows in parallel, like the picture shown in jenkins blue ocean beta page.
Many thanks !
Building Pipelines with parallel steps is well document in a few guides, I've found these to be the most effective places to find info for me personally:
cloudbees.com
jenkins pipeline examples
I've also answered questions on how to properly set it up here (I know, shameless).
For the fun it here is the pipeline groovy script to build out the example [obviously missing the actual build commands].
node('master') {
stage('Build') {
sh "echo Build"
}
stage('Test'){
parallel (
"JUnit": {
sh "echo JUnit"
},
"DBUnit": {
sh "echo DBUnit"
},
"Jasmine": {
sh "echo Jasmine"
},
)
}
stage('Browser Tests'){
parallel (
"Firefox": {
sh "echo Firefox"
},
"Edge": {
sh "echo Edge"
},
"Safari": {
sh "echo Safari"
},
"Chrome": {
sh "echo Chrome"
},
)
}
stage('Dev'){
sh "echo Dev"
}
stage('Staging'){
sh "echo Staging"
}
stage('Production'){
sh "echo Production"
}
}
UI in action
Cheers, and good luck.

Resources