Executing Shell Scripts in Jenkins Pipeline and Displaying Output with the Input Step for Approval
This article explains how to run a Shell script within a Jenkins pipeline, capture its output, and present it through the Input step as a multi‑line text field for approval, illustrating the process with complete pipeline examples.
With the widespread adoption of CI/CD practices, Jenkins has become a leading automation server, and its pipeline feature offers a powerful way to define and run automated processes. In many scenarios, you need to execute a Shell script inside a pipeline and use its output for an approval step.
In a Jenkins pipeline you can run a Shell script using the sh step. The basic syntax is shown below:
pipeline {
agent any
stages {
stage('执行 Shell 脚本') {
steps {
script {
// Execute Shell script and capture output
def output = sh(script: 'your_shell_script.sh', returnStdout: true).trim()
echo "脚本输出: ${output}"
}
}
}
}
}Assume we have a simple Shell script hello.sh with the following content:
#!/bin/bash
echo "Hello, Jenkins!"You can invoke this script from the pipeline and capture its output:
pipeline {
agent any
stages {
stage('执行 Shell 脚本') {
steps {
script {
def output = sh(script: './hello.sh', returnStdout: true).trim()
echo "脚本输出: ${output}"
}
}
}
}
}In this example the returnStdout: true argument ensures that the script’s output is captured and assigned to the variable output .
The input step in Jenkins allows you to pause the pipeline and wait for user input. By passing the captured script output as a multi‑line text parameter to the input step, you can create an approval workflow.
Below is a complete pipeline example that shows how to display the Shell script output as a multi‑line text field for approval:
pipeline {
agent any
stages {
stage('执行 Shell 脚本') {
steps {
script {
def output = sh(script: './hello.sh', returnStdout: true).trim()
echo "脚本输出: ${output}"
// Use input step for approval, defining a multi‑line text parameter
def userInput = input(
id: 'Approval',
message: "脚本输出如下:",
parameters: [text(name: '脚本输出', defaultValue: output, description: '请确认以下输出')]
)
echo "用户输入: ${userInput}"
}
}
}
stage('后续操作') {
steps {
echo "继续后续操作..."
}
}
}
}In this example the input step displays the script output and lets the user confirm whether to continue. The user's response is stored in the userInput variable for later use.
By executing a Shell script in a Jenkins pipeline and incorporating its output into an input step, you achieve a flexible review process that improves automation transparency and team collaboration. As CI/CD practices evolve, this flexibility becomes increasingly valuable, and the article aims to provide useful guidance for implementing such approval flows.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.