current working directory in Jenkins pipeline script - jenkins

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"

Related

SCP from Windows to Linux using 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. Before the below script runs, i am cloning the repository and then building the project which finally creates a .jar file. I want to copy this file to Linux server.
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'
}
}
}
My working directory is /c/Jenkins/workspace/migration/test-project/. Inside the given directory, build/libs folder gets created where the required .jar file is present.
Running above script gives the following error:
/c/Jenkins/workspace/migration/test-project/build/libs/ssupservice-0.0.1-SNAPSHOT.jar: No such file or directory
Give this a shot:
pipeline {
agent any
stages {
stage('SCP JAR file') {
steps {
bat '"c:\\Program Files\\git\\usr\\bin\\scp.exe" -i "c:\\Users\\tom\\.ssh\\azure\\id_rsa" C:\\Users\\tom\\.jenkins\\workspace\\scp-to-linux\\abc.jar tom#xy.xyz.xy.xz:abc.jar'
bat '"c:\\Program Files\\git\\usr\\bin\\ssh.exe" -i "c:\\Users\\tom\\.ssh\\azure\\id_rsa" tom#xy.xyz.xy.xz ls -ltr'
}
}
}
}
Note: While doing scp, if you do not specify the destination file name, it will create file on remote server with the complete source path name. For example, in my case, it would have created file with the name C:\Users\tom\.jenkins\workspace\scp-to-linux\abc.jar on remote server had i not specified this syntax: tom#xy.xyz.xy.xz:abc.jar

Converting Jenkins Project to Pipeline and unable to run ant build get 'cmd' is not recognized

Project builds just fine trying to convert this to run as pipeline. Get error - Running batch script
'cmd' is not recognized as an internal or external command,
operable program or batch file.
Used Convert This Job to Pipeline which didn't quite succeed and had other issues (separate issue). Created Jenkins File and copied in and modified the Pipeline script.
I have tried both WithEnv and WithAnt and same results. Additionally found a note out there and put C:\Windows\System32 in the PATH on the Agent and still get the error. So I'm not sure what I'm missing since I have no issues with my original project and should be calling the same tools I set in Global Tools Config
stage ('PowerSuite - Build') {
agent {
node {
label 'jenkinsclient2'
}
}
steps {
withEnv(["JAVA_HOME=${tool 'JDK_1.7.0_09'}", "PATH=${env.JAVA_HOME}/bin"]){
withAnt(installation: 'Ant 1.7.0') {
echo 'Jenkins client 2 running PowerSuite Build'
bat "ant -Xmx1024m -Xms1024m -buildfile C:\\Builds\\Dev\\Powersuite\\build\\project.xml -Dbuild_type=DEV -DdeployDir=\\\\cifs\\Builds\\PowerSuite\\DEVTest -lib .\buildtoolkit clean ear"
}
}
}
}
Have Ant run the build same as the Jenkins Project

Why does Jenkins shell file copy not work as expected (does not overwrite existing files)

I have a step in a Jenkins pipeline to copy some source files to the workspace.
stage('Copy Files') {
script {
echo 'Staging files'
sh "cp -ar /home/dev/src/ ${env.WORKSPACE}"
}
}
Yet, when I rerun the build it uses the old code. The only solution is to delete the workspace prior to the copy. In a normal Linux file system a copy will overwrite the destination. Why does Jenkins behave differently--i.e., old files are not overwritten? From the syntax it seems like it is just running a shell command, so why does this not have the expected behavior?
It is because, Jenkins run on master node and workspace will be on the worker node.
when checkout scm and sh "" code blocks are in different stages, files will not be save from first stage to other. You should use stash & unstash. when you stash a directory path, files in that dir will be available to the unstashed step in later stages.
Jenkins doc - here

How to execute shell commands in jenkins pipeline script on windows machine

node{
def app
stage ("Build Image"){
bat 'cd C:/Users/trivedi2/Desktop/DEV_pipeline/DEV_Workspace'
app = docker.build("CDashboard")
}
}
This is my pipeline code for creating docker images
error while running jenkins job:nohup: failed to run command 'sh': No such file or directory
Can any one help me with this issue. I am using windows machine
First set the env PATH variable in the machine which points out to a sh.exe in Git->bin
Second Try to do a sysmlink to nohup.exe as the error susggest
mklink "C:\Program Files\Git\bin\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
After this setup you can use node{sh "git --version" in your jenkinsfile and it works fine.
https://stackoverflow.com/a/45151156/3648023

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