akp
akp

Reputation: 91

Can't push file to remote host from jenkins(docker image)

I have Jenkins running on docker on an ubuntu machine. I'm using the below script to first copy a script from the jenkins(docker image) to the remote host and then execute it to modify some configuration file.

node {
    properties([
        parameters([
            string(name: 'version', defaultValue: '', description: 'Enter the version in x.y.z format')
        ])
    ]) 
    def remote = [:]
    remote.name = 'testPlugin'
    remote.host = 'x.x.x.x'
    remote.allowAnyHosts = true

    withCredentials([
        sshUserPrivateKey(credentialsId: 'ssh-credentials', usernameVariable: 'ssh-user', passphraseVariable: 'ssh-pass')
    ]) {
        remote.user = ssh-user
        remote.password = ssh-pass

        stage('testPlugin') {
            sshPut remote: remote, from: 'myscript.sh', into: '.'
            sshScript remote: remote, script: "myscript.sh ${version}"               
        }
    }
}

I could see the myscript.sh file by logging into the docker image where jenkins is running. However, when I execute the above code i get the below error:

java.lang.IllegalArgumentException: /var/jenkins_home/workspace/Cloud-gating/myscript.sh does not exist.

Any idea, why the Jenkins is not able to see the file inside its workspace. The file(myscript.sh) ownership is jenkins. It's the same user with the Jenkins job is executing.

Upvotes: 1

Views: 711

Answers (1)

spikalev
spikalev

Reputation: 61

The problem is really with accessibility of this script.

Please check whether the file really exists:

def exists = fileExists 'myscript.sh'
if (exists) {
    echo 'Yes'
} else {
    echo 'No'
}

if it doesnt, please check whether you are not confused with mapped to the container directory, check its the real path and content:

//inside your pipeline
sh 'pwd' 
sh 'ls'

Check which host paths are mapped to which paths inside container:

docker inspect -f '{{ .Mounts }}' YOUR_CONTAINER_NAME

And the last but not least: you can skip using sshPut in your script, sshScript itself copies a file and executes it remotely:

stage('testPlugin') {
    sshScript remote: remote, script: "myscript.sh ${version}"               
}

Cheers.

Upvotes: 1

Related Questions