简体   繁体   中英

Shell script cd command in Jenkins pipeline is giving error "no such file or directory"

I have a Jenkins pipeline script for file transfer job in which I am trying to read a CSV file (FileTransferCSV.csv) from server and perform the actions accordingly. Under the script block after reading the CSV file, "scp" command to copy a file from Jenkins Server to Actual testing transfer is working fine but "cd" to the Target Server is giving error

no such file or directory

Below is the part of the Jenkins pipeline script:

stage('Parsing the CSV and Transferring Files')
{
     steps {
        script {
            readFile("${WORKSPACE}/archives/TEST/FileTransferCSV.csv").split('\n').each { line  ->
                if (!line.contains("Action,Source Path,File Name,Destination Path"))
                {
                    def fields = line.split(',')
                    for(String item: fields) {
                        println item
                    }

                    if (fields[0].equals("UPDATE") || fields[0].equals("INSERT"))
                    {
                        if (fields[1] == null || fields[1].isEmpty())
                        {
                            sh (script :  "scp -r ${WORKSPACE}/archives/TEST/${fields[2]} asapuser@${ASAP_IP}:${SERVER_PATH}/${fields[3]} " , returnStdout: true).trim();
                            echo "------------Files copied to Target server--------------"
                        }
                        else
                        {
                            sh (script : "scp -r ${WORKSPACE}/archives/TEST/${fields[1]}/${fields[2]} asapuser@${ASAP_IP}:${SERVER_PATH}/${fields[3]} " , returnStdout: true).trim();
                            echo "------------Files copied to Target server--------------"
                        }
                    }

                    if (fields[0] == "DELETE")
                    {
                        sh (script : "#!/bin/bash",returnStdout: true).trim();
                        sh (script : "cd asapuser@${ASAP_IP}:${SERVER_PATH}/ZDP/${fields[3]}",returnStdout: true).trim();
                        sh (script : "rm -r asapuser@${ASAP_IP}:${fields[2]}",returnStdout: true).trim();
                        echo "------------Files deleted from Target server--------------"
                    }

                }
            }
        }
    }
}
                              

For "DELETE" Action, the destination path and file name are populating correctly but error is coming "no such file or directory". I have checked the file location from terminal with the same command and it's working.

Below is the output of the pipeline:

[Pipeline] { (Parsing the CSV and Transferring Files)
[Pipeline] script
[Pipeline] {
[Pipeline] readFile
[Pipeline] echo
23:16:46  UPDATE
[Pipeline] echo
23:16:46  ONENDS
[Pipeline] echo
23:16:46  MODIFY_HLR_ADD_PRIMARY.xml
[Pipeline] echo
23:16:47  property_files/ONENDS
[Pipeline] sh
23:16:47  + scp -r /home/tomcat/.jenkins/workspace/ASAP_PROPERTY_FILES_DEPLOY_PIPELINE/archives/TEST/ONENDS/MODIFY_HLR_ADD_PRIMARY.xml asapuser@10.162.53.18:/opt/asap/asap/property_files/ONENDS
[Pipeline] echo
23:16:59  ------------Files copied to Target server--------------
[Pipeline] echo
23:17:11  INSERT
[Pipeline] echo
23:17:11  
[Pipeline] echo
23:17:12  NEW
[Pipeline] echo
23:17:12  property_files
[Pipeline] sh
23:17:12  + scp -r /home/tomcat/.jenkins/workspace/ASAP_PROPERTY_FILES_DEPLOY_PIPELINE/archives/TEST/NEW asapuser@10.162.53.18:/opt/asap/asap/property_files
[Pipeline] echo
23:17:24  ------------Files copied to Target server--------------
[Pipeline] echo
23:17:49  DELETE
[Pipeline] echo
23:17:49  
[Pipeline] echo
23:17:49  DUMMY
[Pipeline] echo
23:17:49  NEW_DUMMY
[Pipeline] sh
23:08:43  + cd asapuser@10.162.53.18:/opt/asap/asap/ZDP/NEW_DUMMY
23:08:43  /home/tomcat/.jenkins/workspace/ASAP_PROPERTY_FILES_DEPLOY_PIPELINE@tmp/durable-568d025c/script.sh: line 1: cd: asapuser@10.162.53.18:/opt/asap/asap/ZDP/NEW_DUMMY: No such file or directory
[Pipeline] }
[Pipeline] // script

Below is the CSV file:

Action,Source Path,File Name,Destination Path
UPDATE,ONENDS,MODIFY_HLR_ADD_PRIMARY.xml,property_files/ONENDS
INSERT,,NEW,property_files
DELETE,,DUMMY,NEW_DUMMY

You cannot cd into a server. That is just not how the command works. I don't know why it would work in you local shell, maybe you have installed some plugin (which would be feasible in zsh or similar shells).

You could use the node block (given that the server is a Jenkins node) combined with the dir block to move to the correct directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM