user123
user123

Reputation: 243

Jenkins pipeline to SSH into an instance and call a function

How to create a function def test() which does some steps after sshing into an instance

I have something like this:

#!/usr/bin/env groovy

def test() {
    cd $testPath
    mv test*.txt archiveFiles
    sh "someScript.sh"
}

pipeline { 
agent java
    parameters {
       string(
            name: 'testPath',
            defaultValue: '/home/ubuntu/testFiles',
            description: 'file directory'
        )
    }
        stages {
        stage(test) {
            steps{
                script{
                    sh "ssh ubuntu@IP 'test()'"
                }
            }

        }
    }
}

I am trying to ssh into an instance and do the steps in the function test() by calling it I am getting an error like this: bash: -c: line 1: syntax error: unexpected end of file ERROR: script returned exit code 1

Upvotes: 0

Views: 2820

Answers (1)

David
David

Reputation: 8206

We use the SSH plugin as follows:

steps {
    timeout(time: 2, unit: 'MINUTES') {
        sshagent(credentials: ['local-dev-ssh']) {
                    sh "ssh -p 8022 -l app ${ENVIRONMENT_HOST_NAME} './run-apps.sh ${SERVICE_NAME} ${DOCKER_IMAGE_TAG_PREFIX}-${env.BUILD_NUMBER}'"
        }
    }
}

Upvotes: 2

Related Questions