Jenkins - groovy script returned exit code 126 - jenkins

I got a very simple Groovy script in Jenkins running in pipeline
For this code:
sh 'chmod +x gradlew'
sh './gradlew build --info'
I'm getting this error:
[Pipeline] sh
+ ./gradlew build --info
/var/jenkins_home/workspace/Pipeline with Gradle#tmp/durable-646d54ad/script.sh: line 1: ./gradlew: Permission denied
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 126
Finished: FAILURE
What am I missing here?

Looks like it does not have permission to run gradlew from your error message.
line 1: ./gradlew: Permission denied
You can do the following:
SSH into the jenkins machine
Switch to jenkins user
sudo su jenkins -
Check the permissions on the file gradlew using the command
ls -l /var/lib/jenkins/workspace/Pipeline with Gradle
Run the connand ./gradlew build --info from terminal and make sure it works before you run the job again
I use declarative pipelines using groovy and it works fine. I have grails installed on the machine and jenkins user can run the command. I use:
sh './gradlew dependencies && ./gradlew assemble && find ./ -name "*.war"'
Please add a comment if you have any more questions...

Related

how to go inside a specific directory and run commands inside it in a jenkins pipeline

I am trying to run a gradle command inside a jenkins pipeline and for that i should cd <location> where gradle files are.
I added a cd command inside my pipeline but that is not working. I did this
stage('build & SonarQube Scan') {
withSonarQubeEnv('sonarhost') {
sh 'cd $WORKSPACE/sonarqube-scanner-gradle/gradle-basic'
sh 'echo ${PWD}'
sh 'gradle tasks --all'
sh 'gradle sonarqube --debug'
}
}
But the cd is not working, I tried dir step as suggested in pipeline docs, but i want to cd inside $WORKSPACE folder.
How can i fix this?
Jenkins resets the directory after each command. So after the first sh, it goes back to the previous location. The dir command is the correct approach, but it should be used like this:
dir('') {
}
Similar to how you have used withSonarQubeEnv
Alternatively, you can simply chain all the commands
sh 'cd $WORKSPACE/sonarqube-scanner-gradle/gradle-basic & echo ${PWD} & ...'
But this is not recommended. Since this will all be in the same command, it will run fine though.

Jenkins cannot find sh script in git repo

In my git repo I have build script located in /Src/build.sh. I have set up simple Jenkins file which runs this script:
node {
stage 'Checkout'
checkout scm
stage 'Build'
sh "chmod +x ./Src/build.sh"
sh "./Src/build.sh"
}
Execution of chmod seems to be successfull, but running build.sh gives me this error:
+ ./Src/build.sh
/var/lib/jenkins/workspace/wrksp#tmp/durable-d3f345e7/script.sh: 1:
/var/lib/jenkins/workspace/wrksp#tmp/durable-d3f345e7/script.sh: ./Src/build.sh: not found
I have no idea what is wrong?

ssh-add make the Jenkins pipline job to fail

Jenkins (2.162), updated modules. I need to to add private github dependencies for cargo build. So, I need store SSH key into Jenkins container before cargo build.
I did:
stage('Build') {
steps{
script {
dir('api'){
withCredentials([string(credentialsId: 'GitKeyText', variable: 'ID_RSA')]) {
sh '''
set +x
eval `ssh-agent -s`
mkdir ~/.ssh
echo ${ID_RSA} >~/.ssh/id_rsa
chmod go-r ~/.ssh/id_rsa
ssh-add
cargo build
'''
}
}
input message: "wait"
}
}
}
All looks good and this sequence of command work well manually inside the docker container. But, Jenkins job had been failing at ssh-add without any error messages. Just ERROR: script returned exit code 1 at the end of the Jenkins console log.
add01:
I added echo comment to the code, and changed set +x to set -x
no output from ssh-add (console output)
.....
+ echo before ssh-add
before ssh-add
+ ssh-add
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // script
Post stage
.....
I used Jenkins SSH Agent Plugin.
All work as intended.
script {
dir('contract_api'){
sshagent(['GitSSHcred']) {
sh 'cargo build'
}
}
}

mvn command not found in jenkins scripted pipeline

I am new to jenkins. I am getting the below error when i am trying to trigger scripted pipeline through jenkins.
/Users/Shared/Jenkins/Home/workspace/pipelinedemo#tmp/durable-a4f2db2a/script.sh:
line 1: mvn: command not found
Below is the code snippet .
pipeline {
agent any
stages {
stage('clone repo and clean it ') {
steps {
sh "rm -rf my-app"
sh "git clone https://github.com/Testing/my-app"
sh "mvn clean -f my-app"
}
}
stage('Test') {
steps {
sh "mvn test -f my-app"
}
}
stage('Deploy') {
steps {
sh "mvn package -f my-app"
}
}
}
}
Please note that i am able to run mvn command through freestyle project. i am getting this error from scripted line. Please answer this. thanks in advance. Geeth
ssh to your linux server, then
1. Install maven on your linux machine.
2. Add it to your PATH variable. (you should able to run mvn -version command there)
3. Restart your jenkins service.
Then, you should be able to use it in your pipeline script.

How to configure Jenkins pipeline so that if there are multiple shell scripts and if one fails the jenkins jobs still runs instead of exiting

I want to configure a Jenkins pipeline job so that it should be able to run multiple shell script jobs. Even if one shell script fails the job should run the other two before failing the job.
You need to tweak your shell script, not Jenkins pipeline to achieve what you want!
Try this in your shell script
shell script command > /dev/null 2>&1 || true
so fail/pass it will execute and go to next shell script
You can always try catch the potentially failing sh execution
node {
sh "echo test"
try {
sh "/dev/null 2>&1"
} catch (error) {
echo "$error"
}
sh "echo test1"
}
Above runs successfully and produces
Started by user Blazej Checinski
[Pipeline] node
Running on agent2 in /home/build/workspace/test
[Pipeline] {
[Pipeline] sh
[test] Running shell script
+ echo test
test
[Pipeline] sh
[test] Running shell script
+ /dev/null
/home/build/workspace/test#tmp/durable-b4fc2854/script.sh: line 2: /dev/null: Permission denied
[Pipeline] echo
hudson.AbortException: script returned exit code 1
[Pipeline] sh
[test] Running shell script
+ echo test1
test1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Resources