我不希望詹金斯保留代理人的批准步骤。我怎样才能做到这一点?

I don't want the jenkins to hold onto the agent for approval step. How can I do that?

我正在尝试编写一个 jenkins 管道,其中我的作业不应该在等待输入时保留从属执行器。这是一种不合适的等待输入的方式,因为它使这些节点对其他用户不可用,而与此同时,他们却闲置等待。因此,我正在尝试以他们在块外等待的方式编写管道作业。下面是我的 groovy 脚本,它被 Jenkinsfile 调用。在下面的代码中,我不希望代理保留等待输入的批准步骤。我该怎么做?

void call(Map configuration = [:], env) {
    pipeline{
        agent { label 'docker-kitchensink-slave' }

        environment {
            K8S_DEV_NS_TOKEN= "dev-ns-cicd"
            K8S_TEST_NS_TOKEN= "test-ns-cicd"
        }

        stages{
            stage('Checkout') {
                steps{
                    checkout scm
                }
            }

            // Maven Build and Unit Tests Dev
            stage('Maven Build and Unit Tests') {
                steps{
                    build(configuration)
                }
            }

            // SonarQube Analysis
            stage('SonarQube analysis') {
                steps{
                    sonarQubeGating(configuration)
                }
            }

            // Build Docker Image and Push to Artifactory
            stage('Build Docker Image and Push to Artifactory') {
                steps{
                    artifactoryImagePush(configuration)
                }
            }

            // Approve DEV Deployment
            stage('Approve K8s Dev Deployment') {
                steps{
                    approveDeployment()
                }
            }

            // Create and Deploy to Dev Environment
            stage('Create and Deploy to k8s Dev Environment') {
                steps {
                    withCredentials([string(credentialsId: "$env.K8S_DEV_NS_TOKEN", variable: 'DEV_TOKEN')]) {
                        kubernetesDeploy(Env: 'dev', Token: "${DEV_TOKEN}")
                    }
                }
            }

            // Approve TEST Deployment
            stage('Approve K8s Test Deployment') {
                steps{
                    approveDeployment()
                }
            }

            // Create and Deploy to Test Environment
            stage ('Create and Deploy to k8s Test Environment') {
                options {
                    skipDefaultCheckout()
                }
                steps {
                    withCredentials([string(credentialsId: "$env.K8S_TEST_NS_TOKEN" , variable: 'TEST_TOKEN')]) {
                        kubernetesDeploy(Env: 'test', Token: "${TEST_TOKEN}")
                    }
                }
            }
        }
    }
}

我可以通过这样的方式直接让它工作。关键是必须为每个阶段指定代理,这样您就可以等待而不会占用代理。此外,您还必须手动结帐,因为这也需要代理。这些里程碑并不是严格需要的,但可以通过取消任何比 运行.

作业更早的作业来阻止您部署较旧的等待作业
pipeline {
    agent none

    options { skipDefaultCheckout() }
    
    stages {
        stage ('Checkout') {
            agent { label 'build' }
            steps {
                checkout scm
            }
        }
        
        stage ('Build') {
            agent { label 'build' }
            steps {
                milestone 1
                echo "Build steps"
            }
        }
        
        stage ('Deploy dev') {
            agent { label 'build' }
            when {
                expression {
                    input message: 'Deploy dev?'
                    return true
                }
                beforeAgent true
            }
            steps {
                milestone 2
            
                echo "deploy dev"
            }
        }
            
        stage ('Deploy test') {
            agent { label 'build' }
            when {
                expression {
                    input message: 'Deploy test?'
                    return true
                }
                beforeAgent true
            }
            steps {
                milestone 3
                
                echo "deploy test"
            }
        }
    }
}