Jenkins Pipeline Error - jenkins

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.

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

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

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.

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"
}
}
}

Jenkins Multibranch Pipeline: How to checkout only once?

I have created very basic Multibranch Pipeline on my local Jenkins via BlueOcean UI. From default config I removed almost all behaviors except one for discovering branches. The config looks line follows:
Within Jenkinsfile I'm trying to setup following scenario:
Checkout branch
(optionally) Merge it to master branch
Build Back-end
Build Front-end
Snippet from my Jenkinsfile:
pipeline {
agent none
stages {
stage('Setup') {
agent {
label "master"
}
steps {
sh "git checkout -f ${env.BRANCH_NAME}"
}
}
stage('Merge with master') {
when {
not {
branch 'master'
}
}
agent {
label "master"
}
steps {
sh 'git checkout -f origin/master'
sh "git merge --ff-only ${env.BRANCH_NAME}"
}
}
stage('Build Back-end') {
agent {
docker {
image 'openjdk:8'
}
}
steps {
sh './gradlew build'
}
}
stage ('Build Front-end') {
agent {
docker {
image 'saddeveloper/node-chromium'
}
}
steps {
dir ('./front-end') {
sh 'npm install'
sh 'npm run buildProd'
sh 'npm run testHeadless'
}
}
}
}
}
Pipeline itself and building steps works fine, but the problem is that Jenkins adds "Check out from version control" step before each stage. The step looks for new branches, fetches refs, but also checks out current branch. Here is relevant output from full build log:
// stage Setup
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh git checkout -f my-branch
// stage Merge with master
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh git checkout -f origin/master
sh git merge --ff-only my-branch
// stage Build Back-end
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh ./gradlew build
// stage Build Front-end
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh npm install
sh npm run buildProd
sh npm run testHeadless
So as you see it effectively resets working directory to particular commit before every stage git checkout -f f067...595.
Is there any way to disable this default checkout behavior?
Or any viable option how to implement such optional merging to master branch?
Thanks!
By default, git scm will be executed in a Jenkins pipeline. You can disable it by doing:
pipeline {
agent none
options {
skipDefaultCheckout true
}
...
Also, I'd recommend take a look to other useful pipeline options https://jenkins.io/doc/book/pipeline/syntax/#options

How to copy Jenkins config files in a jenkins pipeline to Web server

I have some files.properties in Jenkins config File that I need to copy to a server during the jenkins pipeline.
pipeline code is more a less as showed, just to get an idea.
How can I add a step that copy this config file from jenkins on a destination server after las step after step DEPLOY WAR TO SERVER in pipeline like for example : "sh Scp file.properties jenkins#destinationserver:/destination/path/file.properties"
code {
stage ('Code Checkout') {
git branch: 'master',
credentialsId: 'b346fbxxxxxxxxxxxxxxxxxxx',
url: 'https://xxxxxxx#bitbucket.org/gr/code.git'
}
stage ('Check Branch') {
sh 'git branch'
}
stage('Compile and Build WAR') {
sh 'mvn clean compile war:war'
stage ('Deploy WAR to server') {
sh "scp .war jenkins#serverIp:/var/lib/tomcat/.war"
}
This is quite easy. You need to install the Config File Provider Plugin and then you can generate the appropriate line by visiting htts://localhost/jenkins/pipeline-syntax/. From there in the dropdown you can choose configFileProvider and fill the rest of the form.
The end result will be something like this:
configFileProvider(
[configFile(fileId: 'maven-settings-or-a-UUID-to-your-config-file', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS clean package'
}

Resources