Jenkins build failing in CI machine - jenkins

I have installed jenkins in my CI server machine and i am creating a pipeline to build my project. Jenkins build fails saying it couldn't find the path. However i have mentioned my workspace path in my pipeline code. Also i am using SVN as my sub version. Kindly assist.
The error i am getting is
+ cd var/lib/jenkins/workspace/ProjectDemo/target
/var/lib/jenkins/workspace/ProjectDemo#tmp/durable-a40648b0/script.sh: line 1: cd: var/lib/jenkins/workspace/ProjectDemo/target: No such file or directory
pipeline {
agent any
stages {enter code here
stage('Code Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'cd var/lib/jenkins/workspace/ProjectDemo/target; mvn clean package'
}
}
}
}

The better solution is use the Jenkins' build in Environment Variable WORKSPACE which represents the job's workspace where source code resides after cloned from SVN or Git.
stage('Build') {
steps {
sh '''
pwd
ls -l
cd ${WORPKSPACE}
echo $PATH
mvn clean package
'''
}
}
Above pwd is to print the path of job's workspace folder, ls -l is to print out files & folders under job's workspace folder. You can remove them if the job's workspace folder is your expected work dir.

Related

Where Jenkins clone GIT repository?

I am trying to find out where Jenkins clone the GIT repo but I can't find it.
In my script in Jenkins pipeline I have:
stage('Do something'){
steps {
sh '''
#!/bin/bash
YAML_FILE="${JENKINS_HOME}/jobs/<JENKINS_JOB_NAME>/workspace/GIT_REPO_NAME/path/to/file.yaml"
...
However when I am trying to buid it, it can't find it in this path:
Error: open /efsmnt/jobs/<JENKINS_JOB_NAME>/workspace/GIT_REPO_NAME/path/to/file.yaml: no such file or directory
I also found that it could be in:
20:13:55 Error: open /home/jenkins/agent/workspace/<JENKINS_JOB_NAME>/GIT_REPO_NAME/path/to/file.yaml: no such file or directory
but with no luck.
The global environment WORKSPACE represents the job workspace where your repo will be clone into.
And Jenkins won't create GIT_REPO_NAME folder in job workspace when run git clone.
You can try below script
sh '''
pwd
ls -l
YAML_FILE="${WORKSPACE}/path/to/file.yaml"
'''
Use double quote in case job workspace path includes space when Jenkins job or Jenkins folder name includes space

current working directory in Jenkins pipeline script

I want to do SCP from Windows Jenkins node to Linux server. In this set up, Windows machine is a Jenkins slave and the target server where i want to copy is Linux.
Below is my Jenkins pipeline script
stage('SCP JAR file') {
steps {
bat 'scp c:\\Jenkins\\workspace\\migration\\test-project\\build\\libs\\ssupservice-0.0.1-SNAPSHOT.jar rxp096p#server:/home/rxp096p/testing'
}
}
}
Above script works but we need to use ${env.WORKSPACE} as the current directory might change.So i tried below
bat 'scp ${env.WORKSPACE}\\build\\libs\\ssupservice-0.0.1-SNAPSHOT.jar
rxp096p#server:/home/rxp096p/testing'
But it gives me error ${env.WORKSPACE}/build/libs/ssupservice-0.0.1-SNAPSHOT.jar no such file or directory.
It seems single quote used after bat command is not interpolating the Jenkins environment variable (env.WORKSPACE).
Please change
bat 'scp ${env.WORKSPACE}\\build\\libs\\ssupservice-0.0.1-SNAPSHOT.jar rxp096p#server:/home/rxp096p/testing'
to
bat "scp ${env.WORKSPACE}\\build\\libs\\ssupservice-0.0.1-SNAPSHOT.jar rxp096p#server:/home/rxp096p/testing"

How to get the project root directory in a Jenkins pipeline?

I use Jenkins v2.222.1 and created a simple pipeline for a project. For the deployment I want to set the release/tag name manually. So the plan, how I want it working is:
I change the release name/number in a defined file, e.g. /root/of/my/project/release.txt.
I start deployment by clicking Build Now
Jenkins reads the release name/number from the file.
Jenkins checks out the appropriate tag (and creates and pushes the according branch).
pipeline {
agent {
label 'dev'
}
stages {
stage('build') {
environment {
APP_VERSION = sh (
script: 'eval "cat $REMOTE_ROOT_DIRECTORY/release.txt"', returnStdout: true
)
}
steps {
sh 'git fetch --all --tags'
sh 'git checkout ${APP_VERSION} -b v${APP_VERSION}'
...
}
}
...
}
}
Since I haven't found out, how to get the remote root directory path, I use a workaround: The $WORKSPACE is placed in the folder /workspace/$project_name (where the $project_name is the project name / title defined in Jenkins). So I just use this knowledge and define the path for cat as ../../release.txt. It works, but is a bit dirty because of the hard-coded path in the Jenkinsfile.
How to get / retrieve the project root directory dynamically in a Jenkins pipeline?

Jenkins build via SSH, artifacts on host but not in workspace?

I've started with a new company and a very convoluted build methodology. The best way I've found to build projects is to command Jenkins to SSH into the build server and execute a string of commands there.
Building by executing shell commands using SSH works well, but the build artifacts don't show up in the Jenkins workspace. Therefore, I can't seem to archive the artifacts directly to Jenkins.
Is there a way to correct this or work around it? Can Jenkins be set up to archive files from outside the workspace?
Assuming that you've added your build node as a node (slave), here's a stash / unstash example
Stash on the build node
stage ('Stash file on node 1') {
node ('some_build_node') {
dir ('/tmp') {
stash name: 'TestTransfer', includes: 'foo.jar'
}
}
}
Unstash on the master
stage ('Unstash file on node 2') {
node ('master') {
// If you don't use a 'dir' block, it'll unstash in the workspace
// which is handy if you then want to archive the artifacts
dir ('/tmp') {
unstash 'TestTransfer'
}
}
}

How to set specific workspace folder for jenkins multibranch pipeline projects

I have an external tool that should be called as build-step in one of my jenkins jobs. Unfortunately, this tool has some issues with quoting commands to avoid problems with whitespaces in the path that is called from.
Jenkins is installed in C:\Program Files (x86)\Jenkins. Hence I'm having trouble with jenkins calling the external tool.
What I tried is to set "Workspace Root Directory" in Jenkins->configuration to C:\jenkins_workspace in order to avoid any whitespaces. This works for Freestyle Projects but my Multibranch Pipeline Project is still checked out and built under C:\Program Files (x86)\Jenkins\workspace.
One solution would be to move the whole jenkins installation to e.g. C:\jenkins. This I would like to avoid. Is there a proper way to just tell Jenkins Pipeline jobs to use the "Workspace Root Directory" as well?
Thanks for any help
the ws instruction sets the workspace for the commands inside it. for declarative pipelines, it's like this:
ws("C:\jenkins") {
echo "awesome commands here instead of echo"
}
You can also call a script to build the customWorkspace to use:
# if the current branch is master, this helpfully sets your workspace to /tmp/ma
partOfBranch = sh(returnStdout: true, script: 'echo $BRANCH_NAME | sed -e "s/ster//g"')
path = "/tmp/${partOfBranch}"
sh "mkdir ${path}"
ws(path) {
sh "pwd"
}
you can also set it globally by using the agent block (generally at the top of the pipeline block), by applying it to a node at that level:
pipeline {
agent {
node {
label 'my-defined-label'
customWorkspace '/some/other/path'
}
}
stages {
stage('Example Build') {
steps {
sh 'mvn -B clean verify'
}
}
}
}
Another node instruction later on might override it. Search for customWorkspace at https://jenkins.io/doc/book/pipeline/syntax/. You can also it use it with the docker and dockerfile instructions.
Try this syntax instead:
pipeline {
agent {
label {
label 'EB_TEST_SEL'
customWorkspace "/home/jenkins/workspace/ReleaseBuild/${BUILD_NUMBER}/"
}
}
}

Resources