问题
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}
""")
}
}
}
回答1:
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
.
From the documentation of sh:
returnStdout (optional)
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.)
So instead of echo sh (returnStdout:true, ...)
try just sh (...)
来源:https://stackoverflow.com/questions/57275712/when-running-an-ansible-playbook-via-a-shell-script-in-jenkins-pipeline-the-ech