jenkinsjenkins-pipelinejenkins-groovy

How to pass build parameters to shared library, and set env variables there


I have a declarative pipeline, that is making use of shared library to zip the checked-out repo, and set some environment variable like env.VERSION. The pipeline uses build-parameters to run.
I am tryin to call this shared library in the Build stage. But when I am passing the build parameters to the shared library (params.git_url, and params.runtype), its giving me error:

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.

My Jenkinsfile looks:

#!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}"
                }
            }
        }
    }
}

And shared library's vars/build.groovy looks:

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
}

Solution

  • The build is a reserve function name by Jenkins pipeline, it's used to invoke another Jenkins pipeline/job, it only accepts the job url as parameter.

    https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#build-build-a-job

    Try to rename vars/build.groovy to vars/packageRepo.groovy, and make change in Jenkinsfile to:

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