P Rajesh
P Rajesh

Reputation: 391

Jenkins deploying job over ssh

Problem:

I'm running a bash script as part of a deployment using the sshPublisher build step. Part of the script is to find a process ID using netstat (and grep/awk). When I log onto the VM and manually run the script, kills the process, but through the Jenkins deploy job, it does not

Jenkins server (VM1):

stage('deploy'){
    sshPublisher(publishers: [sshPublisherDesc(configName: 'fdpdeploy', transfers: [sshTransfer(excludes: '', execCommand: './deploy.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/fdp-0.0.1-SNAPSHOT.war')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])           
}

VM2: This is the script deploy.sh

#!/bin/bash

processId=$(sudo netstat -plten | grep 9030 | awk {'print $9}' | awk -F '/' {'print $1'})
echo $processId

kill -9 $processId

sleep 5

echo 'Starting FDP app'

nohup java -jar /data/fdp-0.0.1-SNAPSHOT.war &> /data/logs/FDPBizApp.log &

exit 0
fi

Upvotes: 0

Views: 1712

Answers (1)

BOC
BOC

Reputation: 1699

From the output you posted in the comment, this is the problem:

sudo: sorry, you must have a tty to run sudo

Option 1 - visudo solution

(This is the solution from the OP):

Use visudo to edit the sudoers file and comment out the requiretty entry

Option 2 - Jenkins plugin solution

The Publish Over SSH plugin's features list has the following item:

Enable the command/script to be executed in a pseudo TTY

You can enable this by adding usePty: true to the sshTransfer block of your publisher. Your Jenkinsfile step should look like this:

stage('deploy'){
    sshPublisher(publishers: [sshPublisherDesc(configName: 'fdpdeploy', transfers: [sshTransfer(excludes: '', execCommand: './deploy.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/fdp-0.0.1-SNAPSHOT.war', usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])           
}

The only change from your existing step is to add , usePty: true after sourceFiles:

Upvotes: 1

Related Questions