Q: Groovy find specific text inside of an Array I am newbie working with Groovy inside of JenkinsFile. We have a pipeline that runs Terraform to create secrets in AWS Secrets Manager. Often times the pipeline fails because a secret is created outside of Terraform and Terraform apply fails. We came up with a solution and that is to parse out Terraform plan and pick the list of secrets it is going to create and then run AWS CLI commands to make sure those secrets don't exist. If they exist the pipeline fails and Jenkins comments in the PR. If the secrets don't exist, the pipeline continues with applying Terraform. Once we parse terraform, we store the list of secrets in an array. We then loop through the array, run our AWS commands, and then store the results of that into another array. Finally we loop through the second array, if we detect (ResourceNotFoundException) in the array we will continue with the pipeline. If we do detect a secret, we will fail the pipeline and comment on the PR. code is below:
def SecretValidation = { String environment, region ->
        secrets = sh(script: "cat plan.log | awk -F '+' '\$4 != \$2 {print \$0}' | grep -w name | awk -F '= ' '{gsub(/\"/, \"\", \$2); print \$2}'", returnStdout: true).trim().split('\n')

        for (secret in secrets) {

            returnText = sh(script: "aws secretsmanager describe-secret --secret-id ${secret}", returnStdout: true).trim().split('\n')

        }
        if (!returnText.contains("ResourceNotFoundException")){ 


            commentPR "## ERROR \\n Secrets already exist! \\n\\n" + "## " + environment + " / " + region + "\\n```" + "\\n```\\n"
            throw new Exception("Secrets already exist.")

        }
        else {

            commentPR "## INFO \\n Secrets do not exist. Proceeding with Apply. \\n\\n" + "## " + environment + " / " + region + "\\n```" + "\\n```\\n"
        }
}

The code does not work and throws hudson.AbortException: script returned exit code 255 error. Any help is appreciated.

A: i'm not sure what you are doing - just trying to fix minor issues in your code
def SecretValidation = { String environment, region ->
    def secrets = sh(script: "cat plan.log | awk -F '+' '\$4 != \$2 {print \$0}' | grep -w name | awk -F '= ' '{gsub(/\"/, \"\", \$2); print \$2}'", returnStdout: true).trim().split('\n')

    for (secret in secrets) {
        def returnText = sh(script: """
                ## ignore error
                set +e
                ## rediredt stderr to stdout
                aws secretsmanager describe-secret --secret-id ${secret} 2>&1
            """, returnStdout: true).trim()
        //check returnText for every secret
        if (!returnText.contains("ResourceNotFoundException")){ 
            commentPR "## ERROR \\n Secrets already exist! \\n\\n" + "## " + environment + " / " + region + "\\n```" + "\\n```\\n"
            throw new Exception("Secrets already exist.")

        } else {
            commentPR "## INFO \\n Secrets do not exist. Proceeding with Apply. \\n\\n" + "## " + environment + " / " + region + "\\n```" + "\\n```\\n"
        }
    }
}

