Jenkins cannot find sh script in git repo - jenkins

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?

Related

Jenkins build failed: gradlew: not found

I want to configure Jenkins 2.375.2 to build gradle project. But when I configure the pipe using Blue Ocean plugin and I run the pipeline I get error:
+ ./gradlew build
/var/lib/jenkins/workspace/jenkins_master#tmp/durable-dcccf1cd/script.sh: 1: ./gradlew: not found
Jenkins file:
pipeline {
agent any
stages {
stage('Build Image') {
steps {
sh "echo 'building..'"
// configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5 ', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'
// execute Java -jar ... and build docker image
sh './gradlew build'
sh 'docker build -t springio/gs-spring-boot-docker .'
}
}
}
I tried to add Gradle config
But still I get the same error. Do you know how I can fix this issue?

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.

How to gather all Gradle dependencies in Jenkins workspace

There is a gradle project in git and there is a Jenkins job. What should be done:
The job checkouts project code from git.
The job starts gradle build: sh "cd ${workspace}/${projectName}; ${workspace}/${projectName}/gradlew
After that I want all project dependecies to be available right there in Jenkins workspace ${workspace}/${projectName} in separate directory. Let's say ${workspace}/${projectName}/dependecies
Of course there is no way to edit the project gradle.build files.
Could it be resolved by the gradlew command flags? Or should I add some init script: ${workspace}/${projectName}/gradlew --init-script /opt/myScript. If so what it should contain?
The point is to set Jenkins workspace as gradle.home. It solves by using of --gradle-user-home flag.
The final solution:
sh "rm -rf ${workspace}/${projectName}/caches"
sh "cd ${workspace}/${projectName}"
sh "chmod +x ${workspace}/${projectName}/gradlew"
sh "${workspace}/${projectName}/gradlew build --gradle-user-home ${workspace}/${projectName}"
sh "zip -r -j ${workspace}/dependencies.zip ${workspace}/${projectName}/caches/modules-2/files-2.1"
All dependencies are stored in caches/modules-2/files-2.1 directory now. For my needs I zip it to separate dependencies.zip file.

Jenkins - groovy script returned exit code 126

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...

How can I add another git pull from one jenkins pipeline stage

I would like to ask on how can I add another step for pulling another repo on the jenkins pipeline I created. You see from the jenkins pipeline settings I already specified a repo for pulling the ui to build. Then after the build is made I need to pull another repo for api and build it as one docker image. I already tried this doc however I'm getting issue on getting the the ui files to combine on the api, here is my pipeline script used.
pipeline {
agent { label 'slave-jenkins'}
stages {blah blah
}
stage('Workspace Cleanup') {
steps {
step([$class: 'WsCleanup'])
checkout scm
}
}
stage('Download and Build UI Files') {
steps {
sh '''#!/bin/bash
echo "###########################"
echo "PERFORMING NPM INSTALL"
echo "###########################"
npm install
echo "###########################"
echo "PERFORMING NPM RUN BUILD"
echo "###########################"
npm run build
echo "###########################"
echo "Downloading API Repo"
echo "###########################"
**git branch: 'master',**
**credentialsId: 'XXXXXXXXXXXX',**
**url: 'ssh://git#XXXXXXe:7999/~lXXXXXX.git'**
echo ""
'''
}
}
}
You shouldn't include Jenkins Pipeline DSL into shell script - it must be a separate step, for example
steps {
// first step to run shell script
sh '''#!/bin/bash
echo "###########################"
echo "PERFORMING NPM INSTALL"
echo "###########################"
npm install
echo "###########################"
echo "PERFORMING NPM RUN BUILD"
echo "###########################"
npm run build
echo "###########################"
echo "Downloading API Repo"
echo "###########################"
'''
// second step to checkout git repo
git branch: 'master',
credentialsId: 'XXXXXXXXXXXX',
url: 'ssh://git#XXXXXXe:7999/~lXXXXXX.git'
}
But mixing two repos in one workspace (and two responsibilities in one job) probably isn't a good idea. It would be better if you split your continuous delivery process into multiple jobs:
job to build UI;
job to build API;
job to create Docker image.
and then chain this jobs, so that they are executed one by one and pass build artifacts to each other. So each part of your CD process will satisfy the principle of
single responsibility.

Resources