step not run in jenkins pipeline, and exit code -2 - jenkins

Result:
npm config set registry https://registry.npm.taobao.org
npm config set 'sass-binary-site=http://npm.taobao.org/mirrors/node-sass'
process apparently never started in /home/jenkins/workspace/demo_nodejs#tmp/durable-a15f6a06
ERROR: script returned exit code -2
Finished: FAILURE
And there is my Pipeline script(SECRET is my secret message):
def registry = SECRET
def library = 'demo'
def name = 'nodejs_demo'
podTemplate(){
node('nodejs') { // my podtemplate is defined in global config, and can run well.
echo 'ready go'
def path = pwd()
def branch_ = ''
def author = ''
def version = ''
def image
branch_ = 'master'
echo 'branch_ = ' + branch_
// clone git
stage("clone code") {
git credentialsId: SECRET, branch: branch_, url: SECRET
sh 'git log --no-merges --pretty=format:"%an" -1 > author.txt'
sh 'git log --no-merges --pretty=format:"%h" --abbrev=8 -1 > version.txt'
sh 'url=`cat .git/config|grep git`&&url=${url##*/}&&echo ${url%.*} > name.txt'
author = readFile("author.txt")
version = readFile("version.txt")
image = "${registry}/${library}/${name}"
echo "${image}"
echo 'clone code complete'
}
# enter container.
container('nodejs') {
stage("nodejs install") { // my Step
sh 'npm config set registry https://registry.npm.taobao.org'
sh 'npm config set sass-binary-site=http://npm.taobao.org/mirrors/node-sass'
sh 'npm install' // not execute it.
}
stage("nodejs build") {
sh 'npm run build'
}
stage('copy dockerfile') {
input "Exit"
}
}
}
}
Status:
"npm install" or "npm build" is defined in my jenkinsfile.
When script run "npm config" In my jenkinsfile, the next step is "npm install", but it was not run, and my workflow is exit.
I use same jenkinsfile, I can receive success message if I am full luck.
The Error will be occur in other location, example after "npm install" or after "npm build" and soon.

Please check if you have an empty environment variable in your global configuration or node configuration.

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' }]]

Jenkins parallel stages - enoent ENOENT: no such file or directory

I am having a problem running some stages in parallel across different agents/nodes in Jenkins. I am creating stages dynamically from a list of available agents with the following code:
// Create empty collection to store parallel stages:
def parallelStages = [:]
// Define list of agents to be used for cypress parallel stages:
agents = [
"agent1",
"agent2",
...
]
// Add as many parallel stages as defined in agents list:
agents.eachWithIndex { label, index ->
parallelStages["Parallel Stage ${index + 1}"] = {
stage("Parallel Stage ${index + 1}") {
node(label) {
sh 'npm install --silent'
sh 'npm start & npx wait-on http://localhost:3000'
sh "npm run another_command"
}
}
}
}
Then I'm using these stages in a parallel block:
pipeline {
agent {
node {
label 'agent1'
}
}
stages {
stage('first-non-parallel-stage'){
steps {
sh 'npm install --silent'
sh 'npm run lint'
sh 'npm run build'
sh "npm run storybook:build"
sh 'npm run test -- --watchAll=false'
}
}
stage ('Parallel stuff'){
steps {
script {
parallel parallelStages
}
}
}
}
}
This is working. However, for the stage on agent1, I get the following errors in the Jenkins log:
+ npx wait-on http://localhost:3000
+ npm start
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/vine/workspace/tend-multibranch_jenkins-testing#3/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/vine/workspace/tend-multibranch_jenkins-testing#3/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
Some details that might be pertinent, but I'm not sure:
Prior to running the parallel stage, you can see I'm running a first-non-parallel-stage, on the agent that is having problems - could that be related? I don't have problems in first-non-parallel-stage, just in the parallel block on that agent. Shouldn't I be able to reuse agent1 in my parallel block after first-non-parallel-stage is done?
I don't get this issue when I use a repetetive verbose parallel block, i.e. instead of creating a function that populates a collection with stage blocks, I write them out by hand, like this:
parallel {
stage { ...stage_code }
stage { ...stage_code }
stage { ...stage_code }
}
But obviously this is verbose and doesn't lend to adding more nodes easily.
Why might this be happening?
The error occurs at the npm side as it is not able to find the file.May be would be a good idea to add npm init which creates the package.json file or delete the package-lock.json file after the non-parallel stage execution is done, so that when you enter the parallel block everything is clean. More details about this is available here : StackOverflow Answer
Thanks to comments from Noam Hemler and Altaf, I figured this out.
The crucial issue was that npm could indeed not find my project. What was being missed in some of the comments is the workspace should be cleaned before or after running the job, but the git repo scm must also be checked out fresh before trying to run any npm commands. I created this setup function:
def setup(){
// clean workspace and do a fresh checkout
deleteDir()
git credentialsId: 'id', url: 'url'
sh 'git checkout ' + env.Branch_Name
}
And now at the start of each stage:
// Add as many parallel stages as defined in agents list:
agents.eachWithIndex { label, index ->
parallelStages["Parallel Stage ${index + 1}"] = {
stage("Parallel Stage ${index + 1}") {
node(label) {
setup()
sh 'npm install --silent'
sh 'npm start & npx wait-on http://localhost:3000'
sh "npm run another_command"
}
}
}
}
And I'm using the setup function before any steps on any other agent too, so when a stage starts, it always cleans the space, pulls a fresh copy of the code, and begins from there. Seems to be working consistently.

jenkins pipeline script to deal module in subdirectory

I have a git url maven project which I want to only deal one of its submodule.
I write in pipeline script :
...
stage("mvn build") {
steps {
script {
sh "mvn package -DskipTests=true"
}
}
}
error arise: The goal you specified requires a project to execute but there is no POM in this directory (/xx/jenkins/workspace/biz-commons_deploy). so I add command :
sh "cd cmiot-services/comm" # subdir of biz-commons_deploy
def PWD = pwd();
echo "##=${PWD} "
sh "mvn package -DskipTests=true"
not work, print ##=/root/.jenkins/workspace/biz-commons_deploy, the error is the same as before .
how can I solve this problem and why the echo and error use different user space?
I make it using sh "mvn -f cmiot-services/comm/pom.xml package -DskipTests=true",still not know where this two user path come from and why sh cd not work.
steps {
sh '''
# list items in current directory to see where is your pom.xml
ls -l
# run job by comment out following two lines, if you don't know the
# relative path of folder where pom.xml insides exactly
cd <folder where pom.xml insides>
mvn package -DskipTests=true
'''
}
As Yong answered, every sh steps are independent, imagine Jenkins is opening a new ssh connection on your slave each time.
For your script, instead of a workaround with sh, why not using build in dir step ?
Something like this should do it :
stage("mvn build") {
steps {
script {
dir('cmiot-services/comm') {
sh "mvn package -DskipTests=true"
}
}
}
}
when you are executing Jenkins Pipline, the current directory is the Jenkins workspace directory.
You can add a step to clone the repo that your code is in (granted that the environment you are running the Jenkins instance is able to connect to your repo and clone).
You can then navigate into the directory that has the pom.xml. And finally execute the maven command.
...
stage("Clone Repo") {
steps {
script {
sh "git clone ssh://git#bitbucket.org:repo/app.git"
}
}
}
stage("mvn build") {
steps {
script {
sh "cd app/"
sh "pwd"
sh "mvn package -DskipTests=true"
}
}
}

How do I correctly use the jenkins 'publish over ssh' plugin in a jenkinsfile?

The documentation on https://jenkins.io/doc/pipeline/steps/publish-over-ssh/#code-sshpublisher-code-send-build-artifacts-over-ssh is not too clear...
sshPublisher publishers transfer sourceFiles: '/appdata/jenkins/jobs/project/otcm-${pom.version}.tar.gz'
The above gives me 'no such property' errors. What's the proper syntax for this?
My jenkinsfile:
node {
stage 'Clean Up'
deleteDir()
stage 'Env Setup'
env.JAVA_HOME = "${tool 'JDK 8'}"
def mvnHome = tool '3.0.3'
stage 'Compile/Package'
checkout scm
stage 'Compile/Package - Get app version'
def pom = readMavenPom file: 'pom.xml'
echo "${pom.version}"
stage 'Compile/Package - Compile and upload to S3'
sh "${mvnHome}/bin/mvn clean install -DskipTests"
/* should i bother with pushing to s3 since snapshot? */
stage 'Push to S3'
stage 'Install'
sh "ssh devci#server 'rm -rf /appdata/devci/app'"
sh "ssh devci#server 'mkdir /appdata/devci/app'"
/* ssh push the tar to remote dir */
/* use the jenkins sshPublisher??? */
/* Remote - explode tar */
/* Remote - download project properties */
/* Remove - start app without new relic using custom port 8007 */
}
so there's this pipeline snippet generator!
https://jenkins.io/blog/2016/05/31/pipeline-snippetizer/
very useful for beginners!

Jenkins Pipeline Error

I am using the pipeline plugin in Jenkins, but unable to run shell commands. I am receiving the following error:
[develop - pipeline] Running shell script
nohup: failed to run command ‘sh’: No such file or directory
The node is an Ubuntu instance.
node ('aws-ondemand') {
//println env.BUILD_NUMBER
try {
stage 'Checkout and Build'
git url: 'git#github.com:MyAndroidRepo.git',
branch: 'develop'
sh 'git submodule init'
sh 'git submodule update'
sh './gradlew clean build'
}catch (e) {
//currentBuild.result = "FAILED"
//notifyFailed()
throw e
}
}
Nevermind. Script is fine. I was injecting env variables in the build step. I removed it and now its working.

Resources