如何在jenkins流水线上并行解析日志文件?

u91tlkcl  于 2023-06-28  发布在  Jenkins
关注(0)|答案(1)|浏览(176)

我试图解析文件,而执行脚本这是我的脚本的例子
script.sh

#!/bin/bash

# Define ANSI color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
RESET="\033[0m"

# Function to print colored text
print_colored_text() {
    local color="$1"
    local text="$2"
    echo -e "${color}${text}${RESET}"
}

# Print different colored text
print_colored_text "$RED" "This is red text."
print_colored_text "$GREEN" "This is green text."
print_colored_text "$YELLOW" "This is yellow text."
print_colored_text "$BLUE" "This is blue text."

# Print colored strings
echo -e "${RED}Error:${RESET} Something went wrong."
echo -e "${GREEN}Success:${RESET} Operation completed successfully."
echo -e "${YELLOW}Warning:${RESET} This action is irreversible."
echo -e "${BLUE}Info:${RESET} Please read the documentation."

# Reset color settings
echo -e "${RESET}"

我有一个管道脚本,它看起来像这样

pipeline {
    agent any

    stages {
        stage('Execute Bash Script') {
            steps {
                script {
                    // Start the long-running script and redirect the output to a file
                    sh(script: 'chmod +x script.sh && ./script.sh > script_output.log &', returnStatus: true)

                 
                    def process = sh(script: 'tail -f script_output.log & echo $!', returnStatus: true, returnStdout: true).trim()
                    echo "ON Next One"
                    def pid = process.stdout.trim()

                    echo "Next one again"
                    while (true) {
                        def status = sh(script: "ps -p $process -o etime=", returnStatus: true, returnStdout: true).stdout.trim()

                        if (status.empty() || status.startsWith('+')) {
                            // Script has completed or log file has stopped changing
                            break
                        }

                        sleep 30 // Adjust the sleep duration as needed
                    }

                    // Terminate the tail process
                    sh(script: "kill $pid", returnStatus: true)

                    // Check the output of the script
                    def scriptOutput = readFile('script_output.log')
                    if (scriptOutput.contains("This is red text.")) {
                        echo "Parsing completed successfully."
                    } else {
                        echo "Parsing failed."
                    }
                }
            }
        }
    }
}

我这里trim()函数不工作抛出错误

Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: bd5e3fed-a88b-4ea2-ac3e-7a8e799a30f1
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.trim() is applicable for argument types: () values: []
Possible solutions: wait(), grep(), wait(long), grep(java.lang.Object), times(groovy.lang.Closure), is(java.lang.Object)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:159)

是否有任何替代方法来实现这一点或如何解决这个错误,请帮助。我试图实现的是并行解析日志文件,同时执行script.sh我的script.sh需要更多的时间来完成执行大约一个小时,我也尝试了timeout,但在这种情况下,它不会帮助我。

kfgdxczn

kfgdxczn1#

我发现了我的答案张贴这一点,所以我可以帮助开发人员时,来到这个问题,所以我现在切换到并行阶段,并通过发送中断信号退出这里是我修改后的脚本

pipeline {
    agent any
    
    stages {
        stage('Parallel Stages') {
            parallel {
                stage('Stage 1') {
                    steps {
                        script {
                            // Execute stage 1 commands and store the result
                            
                            sh "bash +x script.sh > script.log" 
                            currentBuild.getRawBuild().getExecutor().interrupt(Result.SUCCESS)
                            sleep(1)
                           
                        }
                    }
                }
                stage('Stage 2') {
                 
                    steps {

                        script {
                            // Continue with stage 2 steps
                           
                     def parseOutput = sh(script: 'tail -f script.log | while IFS= read -r line; do if [[ $line == *"This is red text."* ]]; then echo "I have output: This is red text."; fi; done', returnStatus: true, returnStdout: true)

                    // Check the result of the background process
                    if (parseOutput == 0) {
                        echo "Parsing completed successfully."
                    } else {
                        echo "Parsing failed."
                    }

                    // Terminate the tail process
                    sh(script: 'pkill -f "tail -f script.log"', returnStatus: true)
                        }
                    }
                }
            }
        }
    }
}

相关问题