Dhara
Dhara

Reputation: 1972

Copy file to remote Windows server in pipeline using jenkins

I have tried multiple solutions in jenkins to copy a file on remote which is EC2 window server on AWS.

  1. Publish over ssh: provided key, hostname, username and password but connection is failed every time

  2. pipeline script:

    pipeline { agent any

         stages {
             stage('SCP') {
                 steps {
                     bat '"C:\\Program Files\\Git\\usr\\bin\\scp.exe" -i "C:\\Live" C:\\Windows\\System32\\config\\systemprofile\\AppData\\Local\\Jenkins\\.jenkins\\workspace\\MSDeploy\\abc.txt abc.txt'
                     bat '"c:\\Program Files\\Git\\usr\\bin\\ssh.exe" -i "C:\\Live" [email protected] ls -ltr'
                 }
             }
         }
     }
    

    where C:\Live is remote server directory and C:\\Windows\\System32\\config\\systemprofile\\AppData\\Local\\Jenkins\\.jenkins\\workspace\\MSDeploy\\abc.txt is the local directory, but it throws an error: shows no such file or directory found

  1. pipeline {
     agent any
     stage ('Deploy') {
         steps {
             withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'var', credentialsId: 'credid', secretKeyVariable: 'seckey']]) {
                 writeFile file: 'groovy1.txt', text: 'ls'
                 bat 'ls -l groovy1.txt'
                 bat 'cat groovy1.txt'
             }
         } 
     }
    }
    

It does create file with text but doesn't work. None of the solutions worked for me.

What I have missed?

Upvotes: 0

Views: 9079

Answers (1)

Aditya Malviya
Aditya Malviya

Reputation: 2140

You're using windows server, you need to use some tools to achieve this, copy over ssh probably won't work here. The best straight solution which I normally use it.

Step 1:

  1. Create a network drive and attached it with shared folder.
  2. You can use bat command to attach
net use N: \\ServerName\Folder /user:Administrator Password

N is your drive

  1. Now from jenkins use the copy command.

copy "D:\Jenkins\file N:"

You can use these commands in your jenkins file refer this link for the same https://thenucleargeeks.com/2020/11/24/how-to-run-batch-scripts-commands-in-jenkinsfile/ This will work for your case, let me know if you face any issue.

Upvotes: 0

Related Questions