SCP from Windows to Linux using 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. 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

Related

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"

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 do I retrieve the remote FS root from declarative Jenkinsfile?

I have a heavyweight base project that rarely changes over time and has a lot of header files. I've set it up to be built in a custom workspace with a relative path base. So on a remote node with FS root set to C:\Jenkins, the resulting path on that particular node will be C:\Jenkins\base.
The reason for this setup is that I don't want to copy or unpack the whole base project for every dependent project to save build time. Also, I don't want to use absolute paths because I like the idea of a self-contained jenkins installation.
Now I have a second project project that uses base. I need to specify the path of base to the build system of project so that it will find the base headers it needs.
Is there any way to retrieve the remote FS root through the environment? I've tried using ${env.JENKINS_HOME} but this always resolves to the home folder of the Jenkins master. My build system expects to find the path to the base project in the PATH_TO_BASE environment variable:
pipeline {
agent none
stages {
stage('Build') {
parallel {
stage('Build Linux x64') {
agent {
label "Debian9_x64"
}
steps {
withEnv(["PATH_TO_BASE=${env.JENKINS_HOME}/base"]) {
sh '''mkdir -p _build
cd _build
cmake ..
cmake --build .'''
}
}
}
stage('Build Windows10') {
agent {
label "Windows10"
}
steps {
withEnv(["PATH_TO_BASE=${env.JENKINS_HOME}/base"]) {
bat '''if not exist _build mkdir _build
cd _build
cmake ..
cmake --build .'''
}
}
}
}
}
}
}
I usually configure slave with a set of environment variables.
Go to the slave configuration
Set Environment varaible under "node properties" section.

Unable to copy file using Jenkins to an remote machine

I am trying to copy a war file from Jenkins sever to another linux machine. For that I have used SCP command. And also I have generated a private/public key.
When I copy a file (scp command) through putty it is happening without any error, but when I run my Jenkins file, where I have written shell script to copy the file to remote server using scp, it throws error saying "Permission denied (publickey,password)". Also I have configured Publish-over-SSH in my Jenkins and test-connection was successful.
stage 'War Copy'
node {
try{
sh "scp /target/abc.war user#remoteserver:/targetmachinefolder"
}catch(Exception e){
}
}

Resources