SSH to a system after jenkins pipeline is executed

84 views Asked by At

I have a jenkins pipeline

pipeline {
    agent any
     tools{
        jdk 'JDK 1.8'
    }
    stages {
        stage('Clean') {
            steps {
                script {
                    sh 'mvn clean'
                }
            }
        }

        stage('Install'){
            steps{
                script{
                    sh 'mvn install'
                }
            }
        }

        stage('Deploy'){
            steps{
                script{
                    sh 'mvn deploy'
                }
            }
        }
    }
}

when this pipeline is executed i want to ssh to a system and use a .ppk file as cred which is stored on my host machine, below is the command to ssh for your refernce

ssh -i "{path to ppk file}" [email protected]

Also when i do mvn install i get a .war file in jenkins logs whose path i want to access in order to deploy.

aws s3 --profile abc cp <war s3 path> abc.war

Please help me do that :)

1

There are 1 answers

6
user1814884 On

pipeline {
    agent any
    tools {
        jdk 'JDK 1.8'
    }
    stages {
        stage('Clean') {
            steps {
                script {
                    sh 'mvn clean'
                }
            }
        }

        stage('Install') {
            steps {
                script {
                    sh 'mvn install'
                }
                // Archive the .war file generated by Maven
                archiveArtifacts artifacts: '**/*.war', fingerprint: true
            }
        }

        stage('Deploy') {
            steps {
                script {
                    // Add your SSH key to the SSH agent
                    sshagent(credentials: ['<your-ssh-credential-id>']) {
                        // SSH to the EC2 instance and deploy the .war file
                        sh 'scp -i xyz.pem target/*.war [email protected]:/path/to/deployment/directory'
                    }
                }
            }
        }
    }
}

pipeline {
    agent any
    tools {
        jdk 'JDK 1.8'
    }
    stages {
        stage('Clean') {
            steps {
                script {
                    sh 'mvn clean'
                }
            }
        }

        stage('Install') {
            steps {
                script {
                    sh 'mvn install'
                }
                // Archive the .war file generated by Maven
                archiveArtifacts artifacts: '**/*.war', fingerprint: true
            }
        }

        stage('Deploy') {
            steps {
                script {
                    // Add your SSH key to the SSH agent
                    sshagent(credentials: ['<your-ssh-credential-id>']) {
                        // SSH to the EC2 instance and deploy the .war file
                        sh 'scp -i xyz.pem target/*.war [email protected]:/path/to/deployment/directory'
                    }
                }
            }
        }
    }
}

i normally follow this,you can use the sshagent plugin to temporarily add your SSH key (xyz.pem) to the SSH agent and then execute SSH commands to connect to your EC2 instance.