如何在jenkins声明式流水线中调用共享库

a14dhokn  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(128)

我试图调用主动选择和被动选择参数脚本从共享库这我的共享librey文件envparams.groovy我尝试与组合它的工作,但当我尝试共享库调用下面的代码乞讨它不工作我给Jenkins文件也得到这个错误没有这样的DSL方法'getElkEnvironments'中找到的步骤[ansiColor

def getElkEnvironments() {
        // This function returns a list of environments.
        return "['Select:selected', 'ELK-DEV', 'OSE-STG','OSE-ERIE7','OSE-QA','Test']"
    }
    def getOseEnvironments(){
        '''
            List devList  = ['Select:selected', 'c7i', 'core7kub']
            List stageList = ['Select:selected', 'core7dev','baseint3','fabdev','baseint7','basedev3']
            List erieList  = ['Select:selected', 'erie7upgrade']
            List default_item = ["None"]
    
            if (Panpticon_Environment_Name == 'ELK-DEV')        {
            return devList
            } else if (Panpticon_Environment_Name == 'OSE-STG')  {
            return stageList
            } else if (Panpticon_Environment_Name == 'OSE-ERIE7') {
            return erieList 
            } else if (Panpticon_Environment_Name == 'OSE-QA')    {
            return stageList
            } 
            else {
            return default_item
            }
        '''
    }

'''
@Library('nameof-libs') _

getElkEnvironments()
getOseEnvironments()
properties([
    parameters([
        [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: '', 
            filterLength: 1, 
            name: 'Panpticon_Environment_Name', 
            randomName: 'choice-parameter-5631314439613978', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Env\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: "${getElkEnvironments()}"
                ]
            ]
        ], 
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: ''
            filterLength: 1, 
            name: 'target_environment', 
            randomName: 'choice-parameter-5631314456178619', 
            referencedParameters: 'Panpticon_Environment_Name', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    classpath: [], 
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Environment from Env Param\']'
                ], 
                script: [
                    classpath: [], 
                    sandbox: false, 
                    script: "${getOseEnvironments()}"
                ]
            ]
        ]
    ])
])
pipeline {
    //agent any
    agent {label 'ose_agent'}
    stages {
      stage('Import ELK Certificate') {
           steps{
                sh"""
                 echo "test done"
                 echo "${getElkEnvironments()}"
                 echo "${getOseEnvironments()}"
                """
           }
     } 
    }
}
But am getting error  ** No such DSL method 'getElkEnvironments' found among steps [ansiColor** can any one help how to call this map value at begging this work fine when I don't use shared lib simply paste at begging but i need to call them in shared lib
kfgdxczn

kfgdxczn1#

默认情况下,必须在库中设置call语句:vars/MyAwesomeLib.groovy:

//example  with map config.params
def call(Map config = [:]){          
  return true
}

字符串
现在你可以在pipeline中调用它:

@Library('My_lib_in_my_git') _
pipeline{
  stages{
    stage('test')
      steps{
        MyAwesomeLib(param:'a')
      }
    }
  }
}


你也可以将我们的方法或类存储在库中:vars/myLib1.groovy:

//example  with map config.params
def MyAwesomeMethod(Map config = [:]){          
  return true
}


在管道中,您可以这样称呼它:

@Library('My_lib_in_my_git') _
pipeline{
  stages{
    stage('test')
      steps{
        myLib1.MyAwesomeMethod(param:'a')
      }
    }
  }
}

相关问题