interactive shell script on jenkins

12,866

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      
 }
}
Share:
12,866
Pooja R
Author by

Pooja R

Updated on July 08, 2022

Comments

  • Pooja R
    Pooja R almost 2 years

    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

  • ByteHamster
    ByteHamster about 9 years
    Welcome to Stack Overflow! While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. See How do I write a good answer.
  • Pooja R
    Pooja R about 9 years
    Is there no way where I can let the user into the details manually on console during the process?