jenkins 如何将构建参数传递给共享库,并在那里设置env变量

hiz5n14c  于 11个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(205)

我有一个声明式的管道,它利用共享库来压缩 checkout 的仓库,并设置一些环境变量,如env.VERSION。管道使用构建参数来运行。
我尝试在Build阶段调用这个共享库。但是当我将构建参数传递给共享库(params.git_urlparams.runtype)时,它给了我错误:

Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.jenkinsci.plugins.workflow.cps.CpsClosure2 build org.codehaus.groovy.runtime.GStringImpl). Administrators can decide whether to approve or reject this signature.

字符串
我的Jenkinsfile看起来:

#!groovy
@Library('my-jenkins-shared-lib')_
pipeline {
    agent any
    options {
        disableConcurrentBuilds(abortPrevious: true)
    }
    
    parameters {
        string(description: 'Git Repository (HTTPS URL)', name: 'git_url', defaultValue: 'https://git.myrepo.com:8443/scm/my_workspace/', trim: true)
        choice(choices: 'Build\nPromote', description: 'Select the run type', name: 'runtype')
        string(description: 'Enter the git branch to be built', name: 'branch', defaultValue: 'master', trim: true)
        credentials(credentialType: 'com.cloudbees.plugins.credentials.common.StandardCredentials', defaultValue: '', description: 'Select Credentials for Git checkout', name: 'git_credentials', required: true)
    }
    stages {
        stage('Clean') {
            steps {
                cleanWs()
            }
        }
        stage('Checkout') {
            steps {
                script {
                    if ( params.branch == '') {
                        echo "Building commit with Tag: master"
                        checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: 'master']], extensions: [], userRemoteConfigs: [[credentialsId: params.git_credentials, url: params.git_url]]]
                        echo "Built at path: ${WORKSPACE}"
                        sh 'ls -l ${WORKSPACE}'
                    }
                    else {
                        echo "Building commit with Tag: ${params.branch}"
                        checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: params.branch]], extensions: [], userRemoteConfigs: [[credentialsId: params.git_credentials, url: params.git_url]]]
                        echo "Built at path: ${WORKSPACE}"
                    }
                }
            }
        }
        stage('Build') {
            steps {
                    build "${params.git_url}" "${params.runtype}"
                }
            }
        }
    }
}


共享库的vars/build.groovy看起来:

def call(String git_url, String runtype) {
    env.VERSION = sh script: 'grep -m 1 version_number ${WORKSPACE}/ComponentInfo.toml | tr -s \' \' | tr -d \'"\' | tr -d "\'" | cut -d\' \' -f3', returnStdout: true
    echo "Version: ${env.VERSION}"
    env.PROJECT = sh script: 'echo '+git_url+'|rev|cut -d\'/\' -f1|cut -d\'.\' -f2|rev', returnStdout: true
    echo "Project: ${env.PROJECT}"
    env.BASEPATH = sh script: 'echo ${WORKSPACE%/*}' , returnStdout: true
    if ( runtype == 'Build') {
        env.ZIP_FILEPATH = "${env.BASEPATH.trim()}/${env.PROJECT.trim()}-${env.VERSION.trim()}-SNAPSHOT.zip"
    }
    else if ( runtype == 'Promote') {
        env.ZIP_FILEPATH = "${env.BASEPATH.trim()}/${env.PROJECT.trim()}-${env.VERSION.trim()}.zip"
    }
    echo "Zip File Path: ${env.ZIP_FILEPATH}"
    zip defaultExcludes: false, exclude: '*.git, *.log, *.idea, dist', glob: '', overwrite: true, zipFile: env.ZIP_FILEPATH
    sh "md5sum "+env.ZIP_FILEPATH.trim()+" | awk '{printf \$1}' > "+env.ZIP_FILEPATH+".md5"
    sh "sha1sum "+env.ZIP_FILEPATH.trim()+" | awk '{printf \$1}' > "+env.ZIP_FILEPATH+".sha1"
    sh "sha256sum "+env.ZIP_FILEPATH.trim()+" | awk '{printf \$1}' > "+env.ZIP_FILEPATH+".sha256"
    sh "sha512sum "+env.ZIP_FILEPATH.trim()+" | awk '{printf \$1}' > "+env.ZIP_FILEPATH+".sha512"
    sh "ls -lrt "+env.BASEPATH
}

oug3syen

oug3syen1#

build是Jenkins pipeline的保留函数名,用于调用另一个Jenkins pipeline/job,它只接受job url作为参数。
https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#build-build-a-job
尝试将vars/build.groovy重命名为vars/packageRepo.groovy,并在Jenkinsfile中更改为:

packageRepo( "${params.git_url}", "${params.runtype}")

字符串

相关问题