詹金斯管道失败,没有一些解释性异常

jenkins pipeline fails without some explanatory exception

我有一个管道,其中包括机器名称、客户端等其他参数,例如应该从该分支的 aws 获取最新的 ami,然后将其放入 clien.json,terraform 将使用它来创建机器但我还想让用户能够为参数提供一个值,并且当该参数没有值时,可以从开发中的最新 ami 中选择值,例如:

#!/usr/bin/env groovy

管道{ 代理 { 标签 'new' }

parameters {

    string(name: 'AMI_ID', defaultValue: '', description: '[Mandatory]')

}


stages {


    stage('Retrieve latest AMI.') {
        when {
            expression { ${AMI_ID} == '' }
        }
        steps {
            script {
               AMI_ID = sh(script: "aws ec2 describe-images --region region1 --owners 123456 --filters \"Name=tag:type,Values=develop\" --query 'sort_by(Images, &CreationDate)[-1].ImageId'  | jq -r '.'", returnStdout: true).trim()
                echo "AMI retrieved: " + $ { AMI_ID }

            }
        }
    }
    stage("Updating client data") {
        environment {
            TERRAHELP_KEY = credentials('some-key')
        }
        steps {

            dir("data/clients/") {


                clientJson = readJSON file: "${CLIENT}.json"

                clientJson.put("client_ec2_eda_ami_id", ${ AMI_ID })

                writeJSON file: "${CLIENT}.json", json: clientJson, pretty: 4

                echo "Following  data will be applied:"
                sh "cat ${CLIENT}.json"
            }
        }
    }
}

}

发现根本原因是我将 shell 脚本变量传递给 groovy,我说:

clientJson.put("client_ec2_eda_ami_id", ${ AMI_ID })

相反,我应该将 AMI_ID 传递给上面的 groovy 变量,然后说:

clientJson.put("client_ec2_eda_ami_id", currentAmi)

代码的和平与 aws 查询变暗:

    stage('Retrieve latest AMI.') {
        }
        steps {
            script {
                currentAmi = params.AMI_ID
                if (currentAmi.isEmpty())
                    currentAmi = sh(script: "aws ec2 query blah blah")
                        echo  "Ami retrieved is: ${currentAmi}"
            }
        }
    }

    stage("Updating client data") {
        environment {
            TERRAHELP_KEY = credentials('some-key')
        }
        steps {

            dir("data/clients/") {
                clientJson = readJSON file: "${CLIENT}.json"

                clientJson.put("client_ec2_eda_ami_id", currentAmi)

                writeJSON file: "${CLIENT}.json", json: clientJson, pretty: 4

                echo "Following  data will be applied:"
                sh "cat ${CLIENT}.json"
            }
        }
    }
}
}