Jenkins has weird view of filesystem - jenkins

I have a Jenkins build server running Linux. I am observing the following behavior:
SSHed in as Jenkins:
> ls /usr/local/bin
cordova ionic sentry-cli
Jenkins script console:
> println "ls /usr/local/bin".execute().text
cordova
ionic
sentry-cli
From a pipeline build script:
> sh 'ls /usr/local/bin'
cordova
ionic
Why would this be different?

Related

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?

CICD Pipeline Deployment stage unable to come out after successful deployment

Background : Spring boot application deployment using cicd pipeline declarative script
Issue :
The spring boot application jar file is able to launch successfully. After some time we can access application health info also from browser but the build job is unable to exit from deployment stage. It is spinning at this stage continuously.
Action Taken: even we have added timeout=120000 in launch command but no change in behaviour.
Help : please help us how can we make clean exit after deployment stage from jenkin cicd declarative pipeline.
We are ssh'ing and executing our launch command. The code is like:
sshagent([sshAgent]) { sh "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -v *.jar sudouser#${server}:/opt/project/tmp/application-demo.jar" sh "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null sudouser#${server} nohup '/opt/java/hotspot/8/64_bit/jdk1.8.0_141/bin/java -jar -Dspring.profiles.active=$profile -Dhttpport=8890 - /opt/project/tmp/application-demo.jar ' timeout=120000" }
I need to come out (clean exit) from jenkins build after deployment stage is successful.
You need to add the '&' to the start process in backgroud
Example:
nohup '/opt/java/hotspot/8/64_bit/jdk1.8.0_141/bin/java -jar -Dspring.profiles.active = $ profile -Dhttpport = 8890 - /opt/project/tmp/application-demo.jar &
You can also put an 'if' condition for when the log appears 'started in' to exit execution.
Example:
status(){
timeout=$1
process=$2
log=$3
string=$4
if (timeout ${timeout} tail -f ${log} &) | grep "${string}" ; then
echo "${process} started with success."
else
echo "${process} startup failed." 1>&2
exit 1
fi
}
start_app(){
java -jar -dspring.profiles.active=$profile -dhttpport=8890 - /opt/project/tmp/application-demo.jar >> /tmp/log.log 2>&1 &
status "60" "application-demo" "/tmp/log.log" "started"
}

Build and Run Docker Container in Jenkins

I need to run docker container in Jenkins so that installed libraries like pycodestyle can be runnable in the following steps.
I successfully built Docker Container (in Dockerfile)
How do I access to the container so that I can use it in the next step? (Please look for >> << code in Build step below)
Thanks
stage('Build') {
// Install python libraries from requirements.txt (Check Dockerfile for more detail)
sh "docker login -u '${DOCKER_USR}' -p '${DOCKER_PSW}' ${DOCKER_REGISTRY}"
sh "docker build \
--tag '${DOCKER_REGISTRY}/${DOCKER_TAG}:latest' \
--build-arg HTTPS_PROXY=${PIP_PROXY} ."
>> sh "docker run -ti ${DOCKER_REGISTRY}/${DOCKER_TAG}:latest sh" <<<
}
}
stage('Linting') {
sh '''
awd=$(pwd)
echo '===== Linting START ====='
for file in $(find . -name '*.py'); do
filename=$(basename $file)
if [[ ${file:(-3)} == ".py" ]] && [[ $filename = *"test"* ]] ; then
echo "perform PEP8 lint (python pylint blah) for $filename"
cd $awd && cd $(dirname "${file}") && pycodestyle "${filename}"
fi
done
echo '===== Linting END ====='
'''
}
You need to mount the workspace of your Jenkins job (containing your python project) as volume (see "docker run -v" option) to your container and then run the "next step" build step inside this container. You can do this by providing a shell script as part of your project's source code, which does the "next step" or write this script in a previous build stage.
It would be something like this:
sh "chmod +x build.sh"
sh "docker run -v $WORKSPACE:/workspace ${DOCKER_REGISTRY}/${DOCKER_TAG}:latest /workspace/build.sh"
build.sh is an executable script, which is part of your project's workspace and performans the "next step".
$WORKSPACE is the folder that is used by your jenkins job (normally /var/jenkins_home/jobs//workspace - it is provided by Jenkins as a build variable.
Please note: This solution requires that the Docker daemon is running on the same host as Jenkins! Otherwise the workspace will not be available to your container.
Another solution would be to run Jenkins as Docker container, so you can share the Jenkins home/workspaces easily with the containers you run within your build jobs, like described here:
Running Jenkins tests in Docker containers build from dockerfile in codebase

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

Ionic e2e CI tests in jenkins

I want to achieve similar functionality to How to run ionic in the background to run e2e tests of my Ionic2 application in Jenkins. The e2e tests are created with protractor.
Option 1) running in the background did not work. As suggested I tried screen / tmux like:
stage 'e2e testing'
sh 'tmux new-session -d -s ionicServe'
sh 'tmux new-session -d -s e2e'
sh 'tmux send-keys -t ionicServe "ionic serve --nobrowser --nolivereload localhost" C-m'
sh 'tmux send-keys -t e2e "npm run e2ej" C-m'
sh 'tmux attach -t e2e'
which works great locally / in a docker container but does not work when Jenkins is executing the test cases.
Do you have any suggestions how to either get it to work in Jenkins with tmux or to have ionic serve the app without the CLI (plain gulp tasks)
Hint: a gulp serve:before does not seem to start the development server.
This (https://github.com/lathonez/clicker/blob/master/.travis.yml) is a fix. But does not seem to be as neatly integrated into Ionic CLI
cd www && python -m SimpleHTTPServer 8100 >> python_serve.log 2>&1 &

Resources