Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a file from Jenkins Agent node to a remote server using Jenkinsfile

I have a Jenkinsfile which implements a pipeline and in one of the stages and under a node, if I say unstash 'myfile', where this 'myfile' will be available on the node? My requirement is I need to access this file and copy to a known remote server (this remote server is not part of Jenkins pool) as part of the Jenkinsfile script.

like image 388
Surender Panuganti Avatar asked Jan 17 '26 09:01

Surender Panuganti


1 Answers

You can use SSH Pipeline Steps to copy file to a remote server. Here is example how to send file from job workspace to remote host:

remote = [:]

remote.name = "name"
remote.host = "remote_ip"
remote.allowAnyHosts = true
remote.failOnError = true
withCredentials([usernamePassword(credentialsId: 'credentials_name', passwordVariable: 'password', usernameVariable: 'username')]) {
    remote.user = username
    remote.password = password
}

sshPut remote: remote, from: 'myfile', into: 'folder_on_remote_host'
like image 151
Sers Avatar answered Jan 20 '26 09:01

Sers