Basic jenkins pipeline bash error - jenkins

Jenkins pipeline versus Jenkins gui.
I have a basic jenkins job - it contain a bash step :
export CHROME_BIN=/usr/bin/google-chrome-stable
git --version
node --version
npm -version
java -version
npm install
Xvfb :99 &
export DISPLAY=:99
npm run ci
it works fine with no errors.
I tried to covert it to a new jenkins pipeline -
node ('ubuntu-aws'){
env.JAVA_HOME="${tool '1.8.92'}"
env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
sh 'java -version'
timestamps {
//sh "docker pull main-virtual.docker.vidible.aolcloud.net/main/travis-test"
sh 'whoami'
sshagent(['24195acf-44c2-4f07-98e4-13365b2e49dc']) {
stage "git checkout"
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'YYYYYYY', url: 'XXXXXXXX']]])
sh '''git --version
node --version
npm --version'''
withEnv(['CHROME_BIN=/usr/bin/google-chrome-stable', 'CONTINUOUS_INTEGRATION=true']) {
sh 'env'
sh 'java -version'
stage "npm install"
sh "npm install"
stage "npm run ci"
sh '''Xvfb :99 &'''
sh 'export DISPLAY=:99'
sh "npm run ci"
//sh 'npm run build'
sh 'ls'
}
}//ssh agent
archiveArtifacts allowEmptyArchive: true, artifacts: 'dist/*.*', excludes: null
}
}
and it failed
07:09:03 [31m03 01 2017 07:08:14.838:ERROR [launcher]: [39mCannot start Chrome
07:09:03
07:09:03 [31m03 01 2017 07:08:17.103:ERROR [launcher]: [39mCannot start Chrome
07:09:03
07:09:03 [31m03 01 2017 07:08:18.887:ERROR [launcher]: [39mCannot start Chrome
both jobs run on the same Jenkins server and on the same slave.
any idea for this error ?

Related

Caching downloaded browser in Jenkins declarative pipeline

Hi I have a project with e2e tests. The goal is to run these tests in jenkins many times. Before actuall running I have to install every time chrome browser. I mean exactly commands in JenkinsFile:
sh 'wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb'
sh 'apt-get update && apt-get install -y ./google-chrome-stable_current_amd64.deb'
In case when I will run this pipeline let's say 30 times in the minute then the browser will be downloaded 30 times from scratch. I would like to cache this browser. As I know I can achieve that with volumes.
My whole JenkinsFile with declarative syntax is:
pipeline {
agent {
docker {
registryCredentialsId 'dockerhub-read'
image 'node:17.3-buster'
args '-v $HOME/google-chrome-stable_current_amd64.deb:/root/google-chrome-stable_current_amd64.deb'
reuseNode true
}
}
parameters {
string(name: 'X_VAULT_TOKEN', defaultValue: '', description: 'Token for connection with Vault')
string(name: 'SUITE_ACCOUNT', defaultValue: '', description: 'Account on which scenario/scenarios will be executed')
string(name: 'Scenario', defaultValue: '', description: 'Scenario for execution')
choice(name: 'Environment', choices:
['latest', 'sprint', 'production (EU1)', 'production (EU3)', 'production (US2)', 'production (US8)', 'production (AU3)'],
description: 'Environment for tests')
}
options {
disableConcurrentBuilds()
}
stages {
stage("Initialize") {
steps {
sh 'wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb'
sh 'apt-get update && apt-get install -y ./google-chrome-stable_current_amd64.deb'
sh 'yarn install'
sh "./init.sh ${params.Environment} ${params.X_VAULT_TOKEN} ${params.SUITE_ACCOUNT}"
}
}
stage("Run Feature tests") {
steps {
echo 'Running scenario'
sh 'yarn --version'
sh 'node --version'
sh """yarn test --tags "#${params.Scenario}" """
}
}
}
}
I'm trying to add in docker section:
args '-v $HOME/google-chrome-stable_current_amd64.deb:/root/google-chrome-stable_current_amd64.deb'
based on section Caching data for containers in the article https://www.jenkins.io/doc/book/pipeline/docker/
This dosen't work. Browser downloads again and again. What's wrong?

How to view cypress mochawesome reports in jenkins after running test step inside docker container?

I'm running my cypress tests on Jenkins inside a dockerized container and I generate cypress mocha awesome report, but I don't know how to display it inside Jenkins.
This is my cypress.json content
{
"integrationFolder": "test/specs",
"supportFile": "test/support/index.js",
"video": true,
"reporter": "node_modules/cypress-multi-reporters",
"reporterOptions": {
"reporterEnabled": "mochawesome",
"mochawesomeReporterOptions": {
"reportDir": "results/mocha",
"overwrite": false,
"html": false,
"json": true,
"timestamp": "mmddyyyy_HHMMss",
"showSkipped": true,
"charts": true,
"quite": true,
"embeddedScreenshots": true
}
},
"screenshotOnRunFailure": true,
"screenshotsFolder": "results/mochareports/assets/screenshots",
"videosFolder": "results/mochareports/assets/videos",
"baseUrl": "http://testurl.com",
"viewportWidth": 1920,
"viewportHeight": 1080,
"requestTimeout": 10000,
"responseTimeout": 10000,
"defaultCommandTimeout": 10000,
"watchForFileChanges": true,
"chromeWebSecurity": false
}
And here is my scripts which I run locally.
"clean:reports": "rm -R -f results && mkdir results && mkdir results/mochareports",
"pretest": "npm run clean:reports",
"cypress:interactive": "cypress open",
"scripts:e2e": "cypress run",
"combine-reports": "mochawesome-merge results/mocha/*.json > results/mochareports/report.json",
"generate-report": "marge results/mochareports/report.json -f report -o results/mochareports -- inline",
"posttest": "npm run combine-reports && npm run generate-report",
"test:e2e": "npm run pretest && npm run scripts:e2e || npm run posttest",
I can view my generated report successfully in the local environment.
Here is my jenkinsfile content
#!groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo 'Checking out the PR'
checkout scm
}
}
stage('Build') {
steps {
echo 'Destroy Old Build'
sh 'make destroy'
echo 'Building'
sh 'make upbuild_d'
}
}
stage('Test') {
steps {
echo 'Running Tests'
sh 'make test-e2e'
}
}
stage('Destroy') {
steps {
echo 'Destroy Build'
sh 'make destroy'
}
}
}
}
The make test-e2e actually runs the test:e2e script inside a docker container, the tests actually run and I can see the reports get generated on Jenkins but I don't know how to view it.
I need to view it in a separate inside Jenkins, also I don't know why I can't access it via Jenkins workspace.
btw. I'm adding the results file in .gitignore
This is my local report preview
You can use the HTML publisher plugin for Jenkins for this:
https://plugins.jenkins.io/htmlpublisher/
Within your Jenkinsfile add a stage to publish the HTML reports
e.g.
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'cypress/cypress/reports/html',
reportFiles: 'index.html',
reportName: 'HTML Report',
reportTitles: ''])
I used the HTML Publisher plugin as the mentioned solution above however my problem was that my results file was in the docker container not in Jenkins workspace and I fixed this problem by copying the folder from a docker container to Jenkins workspace.
docker cp container_name:/app/results ./results

./jmeter: not found error when running Jmeter on Jenkins scripted pipeline

I have a Jenkins pipeline for .Net Core REST API and I am getting an error on the command for executing Jmeter tests :
[Pipeline] { (Performance Test)
[Pipeline] sh
+ docker exec 884627942e26 bash
[Pipeline] sh
+ /bin/sh -c cd /opt/apache-jmeter-5.4.1/bin
[Pipeline] sh
+ /bin/sh -c ./jmeter -n -t /home/getaccountperftest.jmx -l /home/golide/Reports/LoadTestReport.csv -e -o /home/golide/Reports/PerfHtmlReport
-n: 1: -n: ./jmeter: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Performance Test Report)
Stage "Performance Test Report" skipped due to earlier failure(s)
I have jmeter running as a Docker container on the server as per this guide Jmeter On Linux and I am able to extract the reports but this same command fails when I run within Jenkins context :
/bin/sh -c ./jmeter -n -t /home/getaccountperftest.jmx -l /home/golide/Reports/LoadTestReport.csv -e -o /home/golide/Reports/PerfHtmlReport
This is my pipeline :
pipeline {
agent any
triggers {
githubPush()
}
environment {
NAME = "cassavagateway"
REGISTRYUSERNAME = "golide"
WORKSPACE = "/var/lib/jenkins/workspace/OnlineRemit_main"
VERSION = "${env.BUILD_ID}-${env.GIT_COMMIT}"
IMAGE = "${NAME}:${VERSION}"
}
stages {
.....
.....
stage ("Publish Test Report") {
steps{
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: '/var/lib/jenkins/workspace/OnlineRemit_main/IntegrationTests/BuildReports/Coverage',
reportFiles: 'index.html',
reportName: 'Code Coverage'
]
archiveArtifacts artifacts: 'IntegrationTests/BuildReports/Coverage/*.*'
}
}
stage ("Performance Test") {
steps{
sh 'docker exec 884627942e26 bash'
sh '/bin/sh -c cd /opt/apache-jmeter-5.4.1/bin'
sh '/bin/sh -c ./jmeter -n -t /home/getaccountperftest.jmx -l /home/golide/Reports/LoadTestReport.csv -e -o /home/Reports/HtmlReport'
sh 'docker cp 884627942e26:/home/Reports/HtmlReport /var/lib/jenkins/workspace/FlexToEcocash_main/IntegrationTests/BuildReports/Coverage bash'
}
}
stage ("Publish Performance Test Report") {
steps{
step([$class: 'ArtifactArchiver', artifacts: '**/*.jtl, **/jmeter.log'])
}
}
stage ("Docker Build") {
steps {
sh 'cd /var/lib/jenkins/workspace/OnlineRemit_main/OnlineRemit'
echo "Running ${VERSION} on ${env.JENKINS_URL}"
sh "docker build -t ${NAME} /var/lib/jenkins/workspace/OnlineRemit_main/OnlineRemit"
sh "docker tag ${NAME}:latest ${REGISTRYUSERNAME}/${NAME}:${VERSION}"
}
}
stage("Deploy To K8S"){
sh 'kubectl apply -f {yaml file name}.yaml'
sh 'kubectl set image deployments/{deploymentName} {container name given in deployment yaml file}={dockerId}/{projectName}:${BUILD_NUMBER}'
}
}
}
My issues :
What doI need to change for that command to execute ?
How can I incorporate a condition to break the pipeline if the tests fail?
Jenkins Environment : Debian 10
Platform : .Net Core 3.1
The Shift-Left.jtl is a results file which JMeter will generate after execution of the `Shift-Left.jmx
By default it will be in CSV format, depending on what you're trying to achieve you can:
Generate charts from the .CSV file
Generate HTML Reporting Dashboard
If you have Jenkins Performance Plugin you can get performance trend graphs, possibility to automatically fail the build depending on various criteria, etc.

gradle jenkins pipeline with codecov

I have a pipeline job for Spring and gradle:
pipeline {
agent any
triggers {
pollSCM '* * * * *'
}
tools {
jdk 'jdk-16'
}
stages {
stage('Build') {
steps {
sh 'java -version'
sh "chmod +x gradlew"
sh './gradlew assemble'
}
}
stage('Test') {
steps {
sh 'java -version'
sh "chmod +x gradlew"
sh './gradlew test'
}
}
stage('Publish Test Coverage Report') {
steps {
step([$class: 'JacocoPublisher',
execPattern: '**/build/jacoco/*.exec',
classPattern: '**/build/classes',
sourcePattern: 'src/main/java',
exclusionPattern: 'src/test*'
])
}
}
}
}
I am uploading the coverage it is available on the jenkins server, but I also want to upload it to codecov on the codecov page for jenkins and java there is a guide for freestyle job: https://about.codecov.io/blog/how-to-set-up-codecov-with-java-and-jenkins/
name: Jenkins CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 11
uses: actions/setup-java#v2
with:
java-version: '11'
distribution: 'adopt'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Run tests
run: ./gradlew clean build
- name: Coverage Report
run: ./gradlew jacocoTestReport
- name: Upload coverage to Codecov
uses: codecov/codecov-action#v1
with:
fail_ci_if_error: false
How can I integrate this in my pipline flow instead of a jenkins.yml file?
I ended up adding codecov commands to the Publish Test Coverage Report stage:
sh 'curl -Os https://uploader.codecov.io/latest/linux/codecov'
sh 'chmod +x codecov'
sh './codecov -t ${token}'
The Report Stage:
stage('Publish Test Coverage Report') {
steps {
step([$class: 'JacocoPublisher',
execPattern: '**/build/jacoco/*.exec',
classPattern: '**/build/classes',
sourcePattern: 'src/main/java',
exclusionPattern: 'src/test*'
])
sh 'curl -Os https://uploader.codecov.io/latest/linux/codecov'
sh 'chmod +x codecov'
sh './codecov -t ${TOKEN}'
}
}
It is the new beta Uploader that is replacing the deprecating bash. Commands for other OS: https://about.codecov.io/blog/introducing-codecovs-new-uploader/

Jenkins Pipeline "yarn install" command not found

This is my first Jenkins script, it currently operates well on Linux but I migrate to MacOS (High Sierra) with the result of getting shell script error.
Node and yarn packages are installed on local Jenkins user. I can't figure out why this error just happens, could anyone give me a hand on this?
Here is my Jenkins file:
node {
stage('Check out') {
checkout scm
}
stage('Prepare') {
sh "yarn install"
}
stage('Test') {
sh "yarn test"
}
stage('Sonar') {
if (env.BRANCH_NAME == 'dev') {
def scannerHome = tool 'sonar scanner';
withSonarQubeEnv('sonar') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
And full log:
14:43:11 Connecting to https://api.github.com using hariklee/******
Obtained Jenkinsfile from 6c639bd70ac86cbe6a49ac0b58bcc10e3c64a375
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in
/Users/Shared/Jenkins/Home/workspace/wingman_423_ci_cd-7PSSGRAMBTXUQRESYCNVODXU7IZJLJLPHQOE3KYEPCSAAYAFFD4A
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Check out)
[Pipeline] checkout
git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
git config remote.origin.url https://github.com/wingman-xyz/app.git # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/wingman-xyz/app.git
git --version # timeout=10
using GIT_ASKPASS to set credentials
git fetch --no-tags --progress https://github.com/wingman-xyz/app.git +refs/heads/423_ci_cd:refs/remotes/origin/423_ci_cd
Checking out Revision 6c639bd70ac86cbe6a49ac0b58bcc10e3c64a375 (423_ci_cd)
git config core.sparsecheckout # timeout=10
git checkout -f 6c639bd70ac86cbe6a49ac0b58bcc10e3c64a375
Commit message: "jenkins test"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] sh
[wingman_423_ci_cd-7PSSGRAMBTXUQRESYCNVODXU7IZJLJLPHQOE3KYEPCSAAYAFFD4A] Running shell script
yarn install
/Users/Shared/Jenkins/Home/workspace/wingman_423_ci_cd-7PSSGRAMBTXUQRESYCNVODXU7IZJLJLPHQOE3KYEPCSAAYAFFD4A#tmp/durable-cf573520/script.sh: line 2: yarn: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
GitHub has been notified of this commit’s build result
ERROR: script returned exit code 127
Finished: FAILURE
There is no yarn command in your PATH variable.
Do npm install -g yarn before
stage('Prepare') {
sh "npm install -g yarn"
sh "yarn install"
}
If you get an error about not found npm command then you will have to add npm explicitly to your PATH using withEnv() {}
withEnv(['PATH+NODE=/something=/path/to/node/bin']) {
stage('Prepare') {
sh "npm install -g yarn"
sh "yarn install"
}
}

Resources