Jenkins 管道布尔参数默认值不能通过 jenkins 共享库动态设置

问题描述 投票:0回答:0

这是 Jenkinsfile

@Library('shared-library@variablestatus')

def IMG_NAME = 'shortener'
def REPO_URL = ''

(BRANCH, CLUSTER, NAMESPACE, ENVPROFILE, REGION, ECR_REGISTRY, BUILDFLAG, DEPLOYFLAG) = getEnvParams("system")

properties([
    parameters([
        string(name: 'GIT_REVISION', description: 'Git branch or commit to fetch.', defaultValue: "$BRANCH", trim: true),
        booleanParam(name: 'BUILD', description: 'Whether to build and publish the image. If disabled, deployment will use latest available image.', defaultValue: "${BUILDFLAG}"),
        booleanParam(name: 'DEPLOY', description: 'Whether to deploy the image to EKS.', defaultValue: "${DEPLOYFLAG}")
    ])
])

node('ec2') {
    stage('checkout') {
        gitCommit = gitCheckout(this, "$REPO_URL", params.GIT_REVISION)
    }

    stage("build") {
        withFolderProperties{
            withCredentials([sshUserPrivateKey(credentialsId: 'eksgit', keyFileVariable: 'EKSGIT')]) {
                if (fileExists('eksgit')) {
                    sh 'rm -rf eksgit'
                }
                sh "cp \$EKSGIT eksgit"
                sh "envsubst < env.tmpl > .env"
            }
        }
        
        buildTag = buildECRImage(this, params.BUILD, "$ENVPROFILE", "$BUILD_NUMBER", "$REGION", "$ECR_REGISTRY", "$IMG_NAME", gitCommit)
    }

    stage("deploy") {
        if (params.DEPLOY) {
            deployToEKS("$REGION", "$CLUSTER", "$ENVPROFILE" , "$NAMESPACE", "$ECR_REGISTRY", "$IMG_NAME", buildTag)
            publishReleaseTag(this, buildTag)
        }
    }

    stage("cleanup") {
        cleanupBuild(this)
    }
}

共享库getEnvParameter函数代码如下:

def call(String jobtype) {

    // Get current environment name based on the job path.
    def jobenv = "$env.JOB_NAME".split('/').first()

    if (jobtype == "conversion") {

        // Environment params used in conversion server pipelines.
        def env = ''
        def branch = 'dev'
        def serverFilter = 'all'
        def dockerHubUser = ''

        if (jobenv == 'production') {
            branch = 'master'
            env = 'production'
            serverFilter = ''
        } else if (jobenv == 'Development') {
            env = 'dev'
        } else if (jobenv == 'msofficeprod') {
            env = 'qa2'
        } else {
            env = jobenv
        }

        return [env, branch, serverFilter, dockerHubUser]
    }
    else if (jobtype == "system") {

        // Environment params used in EKS pipeliness (a.k.a system services).
        def branch = 'dev'
        def kubeCluster = 'devstage'
        def kubeNamespace = ''
        def envProfile = ''
        def region = 'us-east-2'
        def ecrRegistry = ''
        def buildflag = true
        def deployflag = true

        if (jobenv == 'production') {
            branch = 'master'
            kubeCluster = 'convert'
            kubeNamespace = 'prod'
            envProfile = 'prod'
            buildflag = false
            deployflag = false
        } else if (jobenv == 'Development') {
            kubeNamespace = 'development'
            envProfile = 'stage'
        } 
        else {
            kubeNamespace = jobenv
            envProfile = jobenv
        }

        return [branch, kubeCluster, kubeNamespace, envProfile, region, ecrRegistry]
    }
    else {
        error "Invalid jobtype specified for env param retrieval: $jobtype"
        return 1
    }
    
}

问题是我无法使用 defaultValue: "${BUILDFLAG}" 这种语法形成共享库来设置布尔参数的默认值。它不是压倒一切的。我需要在詹金斯管道中使用共享库来验证默认值。

jenkins continuous-integration jenkins-pipeline continuous-deployment continuous-delivery
© www.soinside.com 2019 - 2024. All rights reserved.