Unable to copy file using Jenkins to an remote machine - jenkins

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){
}
}

Related

Problems transferring build artifacts from Jenkins running in a docker container

I'm a little bit of a newb, with this CI/CD container stuff so please correct me anywhere I'm wrong.
I can't seem to find out how to send by npm build files created on my jenkins instance (workspace) to a remote server. I have a pipeline that successfully pulls in my github repo, does all my fun npm stuff (npm install, test, build). I see my build dir in my jenkins instance /workspace.
My environment is as follows. We have a server where docker (with Portainer) is installed. Jenkins is running in a container with a volume mounted (my react build dir goes here). No issues with the pipeline or building etc. I just can't figure out how to push my artifacts from my jenkins workspace directory to my 'remote' dev server.
I can successfully open a console in my jenkins container (portainer as the jenkins user) and scp files from the workspace directory using my remote server creds(but password is necessary).
I installed and used "Publish Over SSH" Jenkins plugin and get a successful "Test Configuration" from my setup.
I created my RSA keys on the REMOTE machine (that I'm trying to push my build files to).
I then pasted the private key (created without a password) into the plugin at the 'Use password authentication, or use a different key' section. Again, I get a successful test connection.
In my pipeline the last step is deploying and I use this command
sh 'scp -r build myusername#xx.xx.xx.xx:/var/files/react-tester'
I get a 'Permission denied (publickey,password).' error. I have no password associated with the rsa key. I tried both ways, creating the rsa key on the remote machine as my remote user, and the jenkins machine as the jenkins user. I've read examples of people creating the keys both ways, but not sure which user/machine combo to create the keys and paste to which section of the 'Publish Over SSH' plugin.
I'm out of ideas.
First, go to "Manage Jenkins" > "Credentials", add a new SSH credential of type "SSH Username with private key" and fill the "Username" and your private key (generate one if you haven't done it yet) fields (you can also upload one). Don't forget that you have to copy the generated public key to the ${SSH_USERNAME}/.ssh/authorized_keys file on the remote server.
I'm assuming you're using a scripted or DSL pipeline here. In your code, after you've builded your application, you can push it to your server adding this step to your pipeline:
pipeline {
stages {
stage("Pushing changes to remote server") {
steps {
script {
def remote_server = "1.2.3.4"
withCredentials([sshUserPrivateKey(credentialsId: 'my-key', keyFileVariable: 'SSH_KEY', passphraseVariable: '', usernameVariable: 'SSH_USERNAME')]) {
sh "scp -i \${SSH_KEY} build/ ${SSH_USERNAME}#${remote_server}:/var/files/react-tester/"
}
}
}
}
}
}
Best regards.

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

Unable to run command to remote machine via Jenkins

While trying to execute some shell command to a remote server from Jenkins, I am getting "Host key verification failed." error.
I have installed Jenkins in a docker on my MAC. Now from the Jenkins browser, I want to run some shell command to a remote server (which is accessible).
To do that I already added the server (10.206.y.z) in the configure page of jenkins (by providing hostname i.e. 10.206.y.z, username & the key generated in 10.206.y.z server in the SSH Servers section) & tested the connection. The connection test passed & then I saved the configuration.
For executing shell command on 10.206.y.z, I created a FreeStyle project & in the Execute Shell section I am passing ssh root1#10.206.y.z 'hostname'.
If I run the project by clicking 'Build Now', I am getting the below error & the build fails.
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/TestProject
[TestProject] $ /bin/sh -xe /tmp/jenkins4234161871485451783.sh
+ ssh root1#10.206.y.z hostname
Host key verification failed.
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Any help please?
I think that the problem is the key. You are using the key generated in the destination machine (10.206.y.z)
You should use the key generated on the jenkins server and pass it to the destination server. For that you could use ssh-copy-id

Jenkins build logs in slave nodes

I am trying to push our jenkins build logs to S3.
I used Groovy plugin and the following script in Build phase
// This script should be run in a system groovy script build step.
// The FilePath class understands what node a path is on, not just the path.
import hudson.FilePath
// Get path to console log file on master.
logFile = build.getLogFile()
// Turn this into a FilePath object.
logFilePathOnMaster = new FilePath(logFile)
logFileName = build.envVars["JOB_BASE_NAME"] + build.envVars["RT_RELEASE_STAGING_VERSION"] + '.txt'
// Create remote file path obj to build agent.
remoteLogFile = new FilePath(build.workspace, logFileName)
// Copy contents of master's console log to file on build agent.
remoteLogFile.copyFrom(logFilePathOnMaster)
And then I am using S3 plugin to push .txt files to S3.
But this script fetches the build log file from the master node.
How are the build logs transferred from slave to master node ?
Can I access the build log file on my slave node without master's involvement at all ?
The slave node must be preserving the build logs while building somewhere ? I cant seem to find it.
I am not much familiar with Groovy but here is the solution which worked for me using shell script.
I am using Jenkins 'Node and Label parameter plugin' to run our java process on a slave node. Job is triggered using 'Build >> Execute Shell' option. The log is collected into a file as below :
sudo java -jar xxx.jar | sudo tee -a ${JOB_NAME}/${BUILD_NUMBER}.log 2>&1
This log file is then pushed to S3 :
sudo aws --region ap-south-1 s3 cp ${JOB_NAME}/${BUILD_NUMBER}.log s3://bucket/JenkinsLogs/${JOB_NAME}/${BUILD_NUMBER}.log
Its working perfectly for us. Hope it helps you too.

How to download file over SSH from remote server in jenkins?

I am using Publish Over SSH plugin in Jenkins to transfer the files over to remote server from local and execute some commands on the remote server.
But, it seems like there is no option available in this plugin to download files from remote server to local.
Can some one assist how can i achieve this?
From a pipeline perspective I have this workaround
First download in you Jenkins server instance
stage("Download") {
steps {
fileOperations([fileDownloadOperation(password: "", targetFileName: "${params.APP_KEY}.zip", targetLocation: "${params.HOME_PATH}", url: "${params.ARTIFACT_URL}", userName: "")])
}
}
and then copy with a scp instrucction
stage("Download last version") {
sshagent(['xxxx-xxxx-xxxx-xxxx-xxxx']) {
sh "scp ${params.APP_KEY_PATH}/${params.APP_KEY}.ZIP ${params.REMOTE_SERVER_USER}#${params.REMOTE_SERVER_URL}:${params.REMOTE_APP_KEY_PATH}"
}
}
For brevity I am avoiding to put another steps that I change a little bit what I do
But the idea is to do the following steps
Download the artifact (locally)
Unzip (locally)
Create a file with the script I want to execute in the remote server (locally)
Copy the script to the remote server
Copy the unziped artifact to the remote server
Execute the script

Resources