Jenkins Pipeline-如何使用“工具"选项指定自定义工具? [英] Jenkins Pipeline - How do I use the 'tool' option to specify a custom tool?
问题描述
我有一个通过自定义工具插件在Jenkins中定义的自定义工具.如果我创建一个自由式项目,则Install custom tools
选项在执行过程中会正确找到并使用该工具(Salesforce DX).
I have a custom tool defined within Jenkins via the Custom Tools plugin. If I create a freestyle project the Install custom tools
option correctly finds and uses the tool (Salesforce DX) during execution.
但是,我找不到通过管道文件执行相同操作的方法.我已经使用管道语法代码段生成器来获取:
However, I cannot find a way to do the same via a pipeline file. I have used the pipeline syntax snippet generator to get:
tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
我已将其放入我的阶段定义中:
I have put that into my stage definition:
stage('FetchMetadata') {
print 'Collect Prod metadata via SFDX'
tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
sh('sfdx force:mdapi:retrieve -r metadata/ -u DevHub -k ./metadata/package.xml')
}
但是我收到一条错误消息,指出line 2: sfdx: command not found
but I get an error message stating line 2: sfdx: command not found
我应该使用其他片段吗?
Is there some other way I should be using this snippet?
完整的Jenkins文件以获取信息:
Full Jenkinsfile for info:
node {
currentBuild.result = 'SUCCESS'`
try {
stage('CheckoutRepo') {
print 'Get the latest code from the MASTER branch'
checkout scm
}
stage('FetchMetadata') {
print 'Collect Prod metadata via SFDX'
tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
sh('sfdx force:mdapi:retrieve -r metadata/ -u DevHub -k ./metadata/package.xml')
}
stage('ConvertMetadata') {
print 'Unzip retrieved metadata file'
sh('unzip unpackaged.zip .')
print 'Convert metadata to SFDX format'
sh('/usr/local/bin/sfdx force:mdapi:convert -r metadata/unpackaged/ -d force-app/')
}
stage('CommitChanges') {
sh('git add --all')
print 'Check if any changes need committing'
sh('if ! git diff-index --quiet HEAD --; then echo "changes found - pushing to repo"; git commit -m "Autocommit from Prod @ $(date +%H:%M:%S\' \'%d/%m/%Y)"; else echo "no changes found"; fi')
sshagent(['xxx-xxx-xxx-xxx']) {
sh('git push -u origin master')
}
}
}
catch (err) {
currentBuild.result = 'FAILURE'
print 'Build failed'
error(err)
}
}
更新 我已经使用此示例Jenkinsfile 取得了一些进展. 我的舞台现在看起来像这样:
UPDATE I have made some progress using this example Jenkinsfile My stage now looks like this:
stage('FetchMetadata') {
print 'Collect Prod metadata via SFDX'
def sfdxLoc = tool 'sfdx'
sh script: "cd topLevel; ${sfdxLoc}/sfdx force:mdapi:retrieve -r metadata/ -u DevHub -k ./metadata/package.xml"
}
不幸的是,尽管看起来Jenkins现在正在寻找并运行sfdx工具,但出现了一个新错误:
Unfortunately, although it looks like Jenkins is now finding and running the sfdx tool, I get a new error:
TypeError: Cannot read property 'run' of undefined
at Object.<anonymous> (/var/lib/jenkins/.cache/sfdx/tmp/heroku-script-509584048:20:4)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
推荐答案
我遇到了同样的问题.我有解决方法:
I ran into the same problem. I got to this workaround:
environment {
GROOVY_HOME = tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation'
}
stages {
stage('Run Groovy') {
steps {
bat "${groovy_home}/bin/groovy <script.name>"
}
}
}
默认情况下,默认情况下不将工具路径添加到PATH
(这是我的1.6 Jenkins服务器安装中的习惯).在执行bat命令时添加${groovy_home}
可以解决此问题.
这种调用工具的方法基本上是从脚本化管道语法中借用的.
我正在将其用于所有自定义工具(不仅是Groovy).
Somehow the tool path is not added to PATH
by default (as was customary on my 1.6 Jenkins server install). Adding the ${groovy_home}
when executing the bat command fixes that for me.
This way of calling a tool is basically lent from the scripted pipeline syntax.
I am using this for all my custom tools (not only groovy).
工具部分:
tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation'
是由代码片段生成器生成的,就像您一样.
was generated by the snippet generator like you did.
根据 Jenkins用户的邮件列表,最终解决方案仍在进行中,因此我的解决方案确实可以解决.
According to the Jenkins users mailing list, work is still ongoing for a definitive solution, so my solution really is a work around.
这篇关于Jenkins Pipeline-如何使用“工具"选项指定自定义工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!