Skip to content

Jenkins Pipeline中获取shell命令的标准输出或者状态

获取shell命令的获取标准输出

  • 第一种方式
groovy
result = sh returnStdout: true ,script: "<shell command>"
result = result.trim()
  • 👍 第二种方式
groovy
result = sh(script: "<shell command>", returnStdout: true).trim()
  • 第三种方式
groovy
sh "<shell command> > commandResult"
result = readFile('commandResult').trim()

获取shell命令的执行状态

  • 第一种方式
groovy
result = sh returnStatus: true ,script: "<shell command>"
result = result.trim()
  • 👍 第二种方式
groovy
result = sh(script: "<shell command>", returnStatus: true).trim()
  • 第三种方式
groovy
sh '<shell command>; echo $? > status'
def r = readFile('status').trim()

​示例

​示例 1:获取 Shell 命令的标准输出​​

groovy
pipeline {
    agent any  // 使用任意可用的代理节点执行
    
    stage("版本号写入") {
        steps {
            script {
                try {
                    // 创建存放版本文件的目录(注意目录名需与Jenkins文件夹一致)
                    sh ''' mkdir -p "/home/application/jd/be/${project}" '''
                    
                    // 写入新版本号(直接追加,文件不存在时会自动创建)
                    sh "echo '${_VERSION}' >> /home/application/jd/be/${project}/version"
                    def versionFile = "/home/application/jd/be/${project}/version"

                    def lineCount = sh(script: "wc -l < '${versionFile}'", returnStdout: true).trim()
                    def lineCountInt = lineCount.toInteger() // 显式转为整数

                    echo "当前版本文件中的版本号数量为:${lineCountInt}"
                    
                    // 如果行数超过3行,删除最旧的版本
                    if (lineCountInt > 3) {
                        sh "tail -n 3 ${versionFile} > ${versionFile}.tmp && mv -f ${versionFile}.tmp ${versionFile}"
                        echo "版本号超过3个,已删除最旧版本,当前保留最新3个版本"
                    } else {
                        echo "版本号未超过3个(当前${lineCountInt}个),无需清理"
                    }
                }catch(err) {
                    echo "🚨🚨🚨版本号写入出错🚨🚨🚨"
                }
            }
        }
    }
}

示例 2:获取 Shell 命令的执行状态

groovy
pipeline {
    agent any  // 使用任意可用的代理节点执行
    
    stages {
        stage('获取 Shell 执行状态') {
            steps {
                script {
                    // 场景 1:执行一个会失败的命令(删除不存在的文件)
                    def failCommand = 'rm /tmp/non_existent_file_12345.txt'  // 目标文件不存在,命令会失败
                    def exitStatusFail = sh(
                        script: failCommand,
                        returnStatus: true  // 启用返回退出状态码
                    ).trim() as Integer  // 转换为整数类型
                    
                    echo "=== 失败命令的状态码 ==="
                    echo "退出状态码:${exitStatusFail}"  // 输出非 0(如 256,具体取决于系统)
                    if (exitStatusFail != 0) {
                        echo "结论:命令执行失败(状态码非零)"
                    }

                    // 场景 2:执行一个成功的命令(创建临时文件)
                    def successCommand = 'touch /tmp/success_demo.txt && echo "文件创建成功" > /tmp/success_demo.txt'
                    def exitStatusSuccess = sh(
                        script: successCommand,
                        returnStatus: true  // 启用返回退出状态码
                    ).trim() as Integer
                    
                    echo "=== 成功命令的状态码 ==="
                    echo "退出状态码:${exitStatusSuccess}"  // 输出 0
                    if (exitStatusSuccess == 0) {
                        echo "结论:命令执行成功(状态码为 0)"
                    }
                }
            }
        }
    }
}
最近更新

采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 运维小弟