Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interactive shell script on jenkins

Tags:

shell

ssh

jenkins

I'm new to jenkins. and I'm trying to invoke remote shell script in jenkins by using 'Execute shell script on remote host using ssh' option.But I'm using read command in the shell script and it is not working on jenkins.Can anybody help me with how to get user input for shell script on jenkins.

script:
echo "hello"
 read -p  "Choose your option : " ch
echo "Hi"

output on jenkins:

Started by user anonymous
[EnvInject] - Loading node environment variables.
Building in workspace C:\Users\pooja_ravi\.jenkins\workspace\freeStyle
[SSH] executing pre build script:    

[SSH] executing post build script:

./testing.sh
./testing.sh[2]: read: no query process
hello
Hi
[SSH] exit-status: 0
Finished: SUCCESS

Thanks in advance,

Pooja

like image 654
Pooja R Avatar asked Oct 19 '22 13:10

Pooja R


1 Answers

You can use the input step in a Jenkins pipeline and then pass that to an expect script that can enter the information for you.

See https://jenkins.io/doc/pipeline/steps/pipeline-input-step/#code-input-code-wait-for-interactive-input

An example of using this is below:

choice = input message: 'Choose your option', parameters: [string(defaultValue: 'Option 1', description: 'What am I choosing', name: 'Comment')]

Now the the result will be stored in the 'choice' variable. This can be passed to an expect script to automate entering the answer to the query.

Example expect script

make_choice.exp
#!/usr/bin/expect
set choice [lindex $argv 0]
spawn read -p  "Choose your option : " ch
expect "option :" { send "$choice\r" }
expect eof
wait

Note that some examples use 'interact' at the end. I found that the last two lines 'expect eof ... wait' were needed for it work in Jenkins.

You would call the expect script from the pipeline passing in the choice obtained from the input step.

E.g:

make_choice.exp $choice

Note: It is best practice to not put the expect script inside a jenkins node. See (https://www.cloudbees.com/blog/top-10-best-practices-jenkins-pipeline-plugin point 7 for explanation).

Rather you would put it outside. So to tie that together in pipeline you'd have something like

timeout(time:5, unit:'DAYS') {
 choice = input message: 'Choose your option', parameters: [string(defaultValue: 'Option 1', description: 'What am I choosing', name: 'Comment')]
 node {
   ./make_choice.exp $choice      
 }
}
like image 104
Samuel Garratt Avatar answered Oct 27 '22 17:10

Samuel Garratt