Transfer to remote host using ssh (jenkins) - jenkins

I have a compressed file in my jenkins workspace folder, I am trying to transfer that compressed file over ssh, but I got SSH: Transferred 0 file(s)
My Configurations are below-
Transfer set source file: my-files.zip
Remove Prefix:
Remote directory: /home/my-files
My Console Output:
Started by user Mizanur Rahman
SSH: Transferred 0 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
Finished: SUCCESS

The first test would be to try and replicate that tranfer manually, from the server executing the job, with the user running the job.
On that server, try at least a:
ssh -Tv xxx#remote.com
That will confirm if you (as the user running the Jenkins job) actually establish an secure shell session. Replace xxx and remote.com by the remote user and remote server target for that session.

Related

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 user gets "Permission denied" at deploying

Deploying a PDF documentation Jenkins executes a simple batch file:
#echo off
REM
REM Adapt the filepath as needed
REM
set PDF_OUTPUT_DIR="F:\svnWorkingCopy\kostra\trunk\DITA\__out_pdf"
set PDF_DEPLOY_DIR="\\FILE-SRV01\Austausch\Setup_Masterbuild\KOSTRA_AKVS\Deutsch\KostraAKVS"
del %PDF_DEPLOY_DIR%\KostraUserManual.pdf /F /Q
copy %PDF_OUTPUT_DIR%\kostra.pdf %PDF_DEPLOY_DIR%\KostraUserManual.pdf /Y
It quits with permission denied. End of console output (in German) is:
[kostra] $ cmd /c call C:\Windows\TEMP\jenkins1093559218605334188.bat
F:\svnWorkingCopy\kostra>F:\svnWorkingCopy\kostra\trunk\DITA\kostra-pdf-deploy.bat
Zugriff verweigert
0 Datei(en) kopiert.
Build step 'Windows Batch-Datei ausführen' marked build as failure
Archiviere Artefakte
Discard old builds...
#111 is removed because old than numToKeep
Sending e-mails to: fjk#akgsoftware.de
Finished: FAILURE
The Jenkins user is the user logged on that machine (Win Server 2012 r2) and has all Jenkins rights.
If I open CMD and execute the batch file manually, everything goes OK. Any idea why Jenkins refuses to copy the file? Is there a way to get a more detailed error msg. from Jenkins?
I see you are running Jenkins on windows. First check that if the user that started Jenkins is allowed to write on your directory. If that is not the case restart Jenkins with a user allowed to write in that directory.
Found the solution for my environment (Jenkins as Windows Service):
The default system user assigned to the Jenkins service must be changed to the logged on windows user.
Even after doing so Jenkins as a service doesn't know about mapped network drives, so you must change these to UNC paths if you have to deploy to a local network share.

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.

Publish over ssh plugin - can't copy build archive

I have been struggling for hours with this thing and I have spent time reading some of the suggested similar questions but couldn't find a hint or solution.
I have a build job that creates a build archive. Then I have a separate deploy job that should take that archive and copy it over ssh to a remote environment. I am using the publish over ssh Jenkins plugin. Everything seems to go well except for the copy part. No files are copied to the remote location.
I get the following:
Started by user admin
Building in workspace /usr/lib/jenkins/workspace/preprod-deploy
SSH: Connecting from host [xxxxxx]
SSH: Connecting with configuration [xxxxxx] ...
SSH: Creating session: username [xxxxxx], hostname [xxxxxx], port [22]
SSH: Connecting session ...
SSH: Connected
SSH: Opening SFTP channel ...
SSH: SFTP channel open
SSH: Connecting SFTP channel ...
SSH: Connected
SSH: cd [/var/www/test_automation]
SSH: OK
SSH: Disconnecting configuration [xxxxxx] ...
SSH: Transferred 0 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
Finished: SUCCESS
My guess is that there is something about the way I'm trying to configure the Source Files path (The base directory for this fileset is the workspace as per the tooltip).
My build archive is located in /usr/lib/jenkins/job/build/${BUILD_TO_DEPLOY}/artifact/*zip*/archive.zip where BUILD_TO_DEPLOY in a natural number corresponding to the build job.
According to the console output, the workspace of the deploy job is /usr/lib/jenkins/workspace/preprod-deploy
This is my configuration:
Ignore the exec command - that is just for testing purposes - but it works - it is successfully creating the txt file to the remote directory
Any ideas how can I overcome this?

Download multiple files from SFTP server as a Jenkins job

I am trying to create a Jenkins job which will pull multiple files from a SFTP server to local machine.
By using ssh2easy plugin in Jenkins i am able to connect to SFTP server and pull single file as shown below.
You can use SSH2Easy plugin with following configuration in job:
remoteFile: /remoteFolderPath/*
localFolder: jobs/jobName/workspace/
fileName: temp/
localFolder is path to job workspace - this depends on jenkins configuration where workspace is located
fileName must end with "/" and the folder must exist in workspace.
You can use a shell build step with curl.
curl command to use:
curl --insecure sftp://username:urlencodedPassword#somedomain.com
Be aware that username/password have to be at the beginning of the domain name, separated by an #.

Resources