Skip to content

Instantly share code, notes, and snippets.

@darkobits
Last active September 15, 2022 12:20
Show Gist options
  • Save darkobits/edb29cf84e64d442cbf5d50832956860 to your computer and use it in GitHub Desktop.
Save darkobits/edb29cf84e64d442cbf5d50832956860 to your computer and use it in GitHub Desktop.
Jenkins Interactive Shell
script {
while (true) {
def cmd = input(message: 'Jenkins Interactive Shell', parameters: [
string(name: 'cmd', description: 'Enter a command or leave blank to continue job.', defaultValue: '')
], ok: 'Execute')
if (cmd == '') { print 'Continuing job...'; break; }
try { sh cmd } catch (err) { }
}
}
@darkobits
Copy link
Author

darkobits commented Oct 22, 2019

This will pause a job and present the user with an input field they can use to execute shell commands from the same context as the job itself, which is not only more convenient than SSH-ing into the Jenkins node, but also ensures that the environment is exactly the same as the one used by the executor.

Using in Declarative Pipelines

This snippet can be dropped into a Delcarative Pipeline between any other instructions in a steps block to pause the job at that point.

pipeline {
  stages {
    stage('test') {
      steps {
        // Drop snippet here.
      }
    }
  }
}

Example

repl-example

@yinonavraham
Copy link

Very nice!
The Jenkins Pipeline Utilities library by ebay provides steps similar to this one, mainly:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment