admin管理员组

文章数量:1123781

Usually, in a shell script one can redirect stdout and stderr to a logfile using:

<command_to_run> > <log_file> 2>& 1
Example: ps -efww > /tmp/processes.log 2>&1 

However, I want to do the same in a Jenkins pipeline via groovy script.

node('tool'){
  def failedMachines = []
  def machinesToCheck = [
    "machine1",
    "machine2"
  ]

  String processCheckCmd = "git clone <repository>"

  stage("Check processes") {
    parallelRunnable = [:]
    for(int i = 0; i < machinesToCheck.size(); i++){
      def tmpMachine = machinesToCheck[i]
      parallelRunnable[tmpMachine] = {
        node(nodetmpMachinename){
            try {
                sh """
                    echo "Running: ${processCheckCmd}" 
                    ${processCheckCmd} > "${nodetmpMachinename}.log" 2>&1
                """
                archiveArtifacts artifacts: "*.log", allowEmptyArchive: true
                } catch(e){
                    echo "Failed to clone on ${nodetmpMachinename}"
                    failedMachines.add(nodetmpMachinename)
                }
            }
        }
    }
    parallel parallelRunnable
  }
  stage('Status'){
    if(failedMachines){
        error("Failed to clone on some machines: ${failedMachines}")
    }
    echo "Clone complete"
  }
}

However, if the command fails to run then it does not log to the file. I do get the logs for successful run. Any pointers as to what the issue is here?

本文标签: