本文介绍了在 Jenkins 管道中通过 shell 脚本运行 ansible playbook 时,回显输出被缓冲并且不会实时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以下面我附上了我的 Jenkins 管道代码.runansible.sh 脚本接受给定的参数,然后根据 paramMode 值继续运行 ansible playbook.在终端中启动 shell 脚本时,我会得到 ansible playbook 运行的实时输出,但是当使用 Jenkins 管道时,我只会在 echo 阶段获取加载齿轮.在整个剧本完成之前,我没有得到任何回声输出,然后它一次全部吐出来.我需要能够像在终端中一样从剧本中获得实时输出,以便在构建过程中可以监控进度.我需要做什么才能获得实时输出?

So below I have attached my pipeline code for Jenkins. The runansible.sh script takes in the parameters given and then proceeds to run an ansible playbook based on the paramMode value. When kicking off the shell script in a terminal I get realtime output of the ansible playbook running, but when using the Jenkins pipeline I just get the loading cogwheel during the echo stage. I do not get any echo output until the whole playbook has completed and then it spits it out all at once. I need to be able to have live output from the playbook like I do in a terminal so the progress can be monitored as the build is building. What do I need to do to get realtime output?

def paramMode = "${params.mode}"
def paramClientCode = "${params.client_code}"
def paramPrevCodeDropID = "${params.prev_code_drop_id}"
def paramCodeDropID = "${params.code_drop_id}"
def paramReleaseID = "${params.release_id}"
def paramConfigZipVer = "${params.config_zip_ver}"
def paramEmailIDs = "${params.email_ids}"

node {
    stage('one-click') {
        node ('nodeName') {
            git url: "gitURL", branch: "runParallel", credentialsId: "stash_id"
            echo sh (returnStdout: true, script: """
            ./runansible.sh ${paramMode} -h ${paramClientCode} -c ${paramCodeDropID} -r ${paramReleaseID} -v ${paramConfigZipVer} -e ${paramEmailIDs} -p ${paramPrevCodeDropID}
            """)

        }

    }

}

推荐答案

我认为你想要的已经是 sh 的默认行为,但是你使用 returnStdout: true 禁用了它.这会导致捕获并返回输出,而不是打印输出.然后用 echo 打印它.

I think what you want is already the default behavior of sh, but you have disabled it by using returnStdout: true. This causes the output to be captured and returned, instead of printed. Then you print it with echo.

来自 sh:

returnStdout(可选)

如果选中,则任务的标准输出为作为步骤值作为字符串返回,而不是被打印到构建日志.(标准错误,如果有的话,仍然会打印到日志.)

If checked, standard output from the task is returned as the step value as a String, rather than being printed to the build log. (Standard error, if any, will still be printed to the log.)

所以不要使用 echo sh (returnStdout:true, ...) 试试 sh (...)

So instead of echo sh (returnStdout:true, ...) try just sh (...)

这篇关于在 Jenkins 管道中通过 shell 脚本运行 ansible playbook 时,回显输出被缓冲并且不会实时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:48