简体   繁体   English

如何在 Jenkins 文件(管道)中使用共享库中的 groovy 常量?

[英]How to use groovy constant from Shared Libray in Jenkins file (pipeline)?

I want use grrovy constant from Shared Libray in my Jenkins pipeline.我想在我的 Jenkins 管道中使用来自 Shared Libray 的 grrovy 常量。 I try this but I have this error:我试试这个,但我有这个错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 27: Not a valid stage section definition: "def paramChecker = ParameterChecker.new(this)". Some extra configuration is required. @ line 27, column 9. stage('Checkout') {

def libIdentifier = "project-jenkins-pipeline"
def libGitBranch = params.LIB_GIT_BRANCH
if(libGitBranch) {
    libIdentifier += "@${libGitBranch}"
}

def com = library(identifier: libIdentifier, changelog: false).com

def Constants = com.project.Constants
def ParameterChecker = com.project.ParameterChecker

pipeline {
    agent {
        docker {
            label "linux"
            image "my-host:8082/project-build-java:jdk1.6-jdk1.8-mvn3.2.5"
            registryUrl 'http://my-host:8082'
            registryCredentialsId 'ReadNexusAccountService'
        }
    }
    stages {
        stage('Clean workspace') {
            steps {
                deleteDir()
            }
        }
        stage('Checkout') {
            def paramChecker = ParameterChecker.new(this)
            paramChecker.checkProjectAndBranchNames()
            checkoutGitSCM(
                url: "${Constants.BITBUCKET_URL}/${paramChecker.projectName}.git",
                tag: Constants.GIT_PROJECT_DEFAULT_BRANCH
            )
        }       
        stage('Compilation Maven') {
            steps {
                timestamps {
                    sh 'mvn -version'   
                }
            }
        }
    }
}

I had script in steps in stage('Checkout')我在阶段的steps中有script stage('Checkout')

def libIdentifier = "project-jenkins-pipeline"
def libGitBranch = params.LIB_GIT_BRANCH
if(libGitBranch) {
    libIdentifier += "@${libGitBranch}"
}

def com = library(identifier: libIdentifier, changelog: false).com

pipeline {
    agent {
        docker {
            label "linux"
            image "my-host:8082/project-build-java:jdk1.6-jdk1.8-mvn3.2.5"
            registryUrl 'http://my-host:8082'
            registryCredentialsId 'ReadNexusAccountService'
        }
    }
    stages {
        stage('Clean workspace') {
            steps {
                deleteDir()
            }
        }
        stage('Checkout') {
            steps {
                script {
                    def Constants = com.project.Constants
                    def ParameterChecker = com.project.ParameterChecker
                    def paramChecker = ParameterChecker.new(this)
                    paramChecker.checkProjectAndBranchNames()
                    checkoutGitSCM(
                        url: "${Constants.BITBUCKET_URL}/${paramChecker.projectName}.git",
                        tag: Constants.GIT_PROJECT_DEFAULT_BRANCH
                    )
                }
            }
        }       
        stage('Compilation Maven') {
            steps {
                timestamps {
                    sh 'mvn -version'   
                }
            }
        }
    }
}

Thanks at @Matt Schuchard and Jenkins: Cannot define variable in pipeline stage感谢@Matt SchuchardJenkins:无法在管道阶段定义变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jenkins 管道 groovy 脚本中的 RTC 插件? - How to use RTC plugin from jenkins pipeline groovy script? 用于 jenkins 的 groovy 管道的参数化 - Parameterisation of a groovy Pipeline for use with jenkins 使用Jenkins Groovy管道脚本中的ci-game - Use ci-game from Jenkins groovy pipeline script 使用从另一个 Groovy 文件加载的枚举(Jenkins 管道问题) - Using an enum loaded from another Groovy file (Jenkins Pipeline issue) 如何在Groovy Jenkins Pipeline中使用全局外部Java库中的方法? - How to use methods from a global external java library in a Groovy Jenkins Pipeline? 如何在Jenkins声明性管道中导入的groovy脚本中使用@Library? - How to use @Library in an imported groovy script in Jenkins declarative pipeline? 如何在 Jenkins 管道中使用 groovy.xml.StreamingMarkupBuilder - How To Use groovy.xml.StreamingMarkupBuilder in Jenkins Pipeline 如何在 Jenkins 管道中解决 Groovy 中的关键字“use”? - How to make a workaround for the keyword 'use' in Groovy in a Jenkins Pipeline? 在Jenkins管道脚本中将另一个groovy文件中的类用作类型 - Using class from another groovy file as a type in a Jenkins pipeline script 从 groovy 管道文件在 linux Jenkins 服务器上执行 powershell 脚本? - Execute powershell script on a linux Jenkins server from groovy pipeline file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM