是否可以将函数导入管道外的 jenkinsfile
is it possible to import functions to jenkinsfile outside pipeline
我有一个简单的管道。
我能够将属性带出管道块并 运行 成功。
当我尝试将属性块移动到外部 groovy 并使用“加载”导入它或什至使用共享库时,管道失败。
有没有办法在管道块外共享一段代码?
这是我对共享库的尝试,但失败了。
@Library("shared-library") _
properties()
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
这是我尝试导入 groovy 但失败了。
def shared_funcs = load "${env.WORKSPACE}/shared/@script/shared_funcs.groovy"
shared_funcs.properties()
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
这将输出“缺少必需的上下文 class hudson.FilePath
也许您忘记了用提供此功能的步骤包围代码,例如:node"
shared_funcs.groovy
def call() {
properties([ parameters([
string( name: 'AWS_ACCESS_KEY_ID', defaultValue: ''),
string( name: 'AWS_SECRET_ACCESS_KEY', defaultValue: '')
]), pipelineTriggers([]) ])
}
确保您的全球管道库具有正确的结构。 https://www.jenkins.io/doc/book/pipeline/shared-libraries/#directory-structure
确保您在该库的 vars/ 目录中有一个文件。为了您的目的,它应该被命名为 shared_funcs.groovy 并被构造为一个函数。
def call(){
properties([ parameters([
string( name: 'AWS_ACCESS_KEY_ID', defaultValue: ''),
string( name: 'AWS_SECRET_ACCESS_KEY', defaultValue: '')
]), pipelineTriggers([]) ])
}
您需要使用 call()
部分来构建它才能正常工作。
我有一个简单的管道。 我能够将属性带出管道块并 运行 成功。 当我尝试将属性块移动到外部 groovy 并使用“加载”导入它或什至使用共享库时,管道失败。 有没有办法在管道块外共享一段代码?
这是我对共享库的尝试,但失败了。
@Library("shared-library") _
properties()
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
这是我尝试导入 groovy 但失败了。
def shared_funcs = load "${env.WORKSPACE}/shared/@script/shared_funcs.groovy"
shared_funcs.properties()
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
这将输出“缺少必需的上下文 class hudson.FilePath 也许您忘记了用提供此功能的步骤包围代码,例如:node"
shared_funcs.groovy
def call() {
properties([ parameters([
string( name: 'AWS_ACCESS_KEY_ID', defaultValue: ''),
string( name: 'AWS_SECRET_ACCESS_KEY', defaultValue: '')
]), pipelineTriggers([]) ])
}
确保您的全球管道库具有正确的结构。 https://www.jenkins.io/doc/book/pipeline/shared-libraries/#directory-structure
确保您在该库的 vars/ 目录中有一个文件。为了您的目的,它应该被命名为 shared_funcs.groovy 并被构造为一个函数。
def call(){
properties([ parameters([
string( name: 'AWS_ACCESS_KEY_ID', defaultValue: ''),
string( name: 'AWS_SECRET_ACCESS_KEY', defaultValue: '')
]), pipelineTriggers([]) ])
}
您需要使用 call()
部分来构建它才能正常工作。