Jenkins批准作为输入及其在其他阶段的使用

46scxncf  于 2023-03-07  发布在  Jenkins
关注(0)|答案(1)|浏览(156)

我尝试在我的管道的最开始创建一个批准阶段,根据该输入,其余阶段将被执行或跳过。
在我的情况下,我不能在其他阶段使用它们。可能的原因是什么。我需要检查值是否已批准。请提供建议

pipeline {
    agent any
    parameters {
        string(name: 'Localities', defaultValue: '', description: 'Enter Country Name')
        string(name: 'name', defaultValue: '', description: 'Enter  Name')
        string(name: 'HOST', defaultValue: '', description: 'Enter Host Name')
    }
    stages {
        stage('Approval') {
            steps {
                script {
                    emailext body: "Localities : ${Localities} \n name : ${name} \n Click this link to approve and run the executor job: ${env.BUILD_URL}console",
                             subject: "Job Approval: Supplier Delete with build #${currentBuild.number}",
                             to: 'user1'
                   timeout(time: 1, unit: 'HOURS') {
                env.approvalStatus= input message: 'You want to approve this build? ', ok: 'Submit', parameters: [choice(choices: ['Approved', 'Rejected'], name: 'ApprovalStatus')], submitter: 'user1,user2', submitterParameter: 'approverID'
                   }
                }
                echo "Approval status: ${env.approvalStatus}"
                //##prints Approval status: {approverID=user1, ApprovalStatus=Approved}
            }
        }
        stage('Build') {
            when {
                expression { env.approvalStatus == 'Approved' }
            }
            steps {
                sh 'echo "Building..."'
            }
        }
        stage('Deploy') {
            when {
                expression { env.approvalStatus == 'Approved' }
            }
            steps {
                sh 'echo "Deploying..."'
            }
        }
    }
}

即使我选择并提交Approved.我的所有阶段都将被跳过。

l7wslrjt

l7wslrjt1#

检查更新的管道。看起来您的输入返回了一个HashMap,因此通过删除env前缀来保存返回的Map,然后从HashMap中提取Input参数,如approvalStatus["ApprovalStatus"]

pipeline {
    agent any
    stages {
        stage('Approval') {
            steps {
                script {
                   timeout(time: 1, unit: 'HOURS') {
                        approvalStatus = input message: 'You want to approve this build? ', ok: 'Submit', parameters: [choice(choices: ['Approved', 'Rejected'], name: 'ApprovalStatus')], submitter: 'user1,user2', submitterParameter: 'approverID'
                   }
                }
                echo "Approval status: ${approvalStatus}"
                //##prints Approval status: {approverID=user1, ApprovalStatus=Approved}
            }
        }
        stage('Build') {
            when {
                expression { approvalStatus["ApprovalStatus"] == 'Approved' }
            }
            steps {
                sh 'echo "Building..."'
            }
        }
        stage('Deploy') {
            when {
                expression { approvalStatus["ApprovalStatus"] == 'Approved' }
            }
            steps {
                sh 'echo "Deploying..."'
            }
        }
    }
}

相关问题