2017-07-27 96 views
1

我通過自定義工具插件在Jenkins中定義了一個自定義工具。如果我創建自由式項目,Install custom tools選件在執行期間正確找到並使用該工具(Salesforce DX)。Jenkins管道 - 如何使用「工具」選項指定自定義工具?

但是,我找不到通過管道文件做同樣的方法。我已經使用管道語法片斷髮生器獲得:

tool name: 'sfdx', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'

我已經將它放入我的舞臺定義:

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

有沒有其他的方法我應該使用這個片段?

全部Jenkinsfile的信息:

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) 
    } 

}

UPDATE 我已經使用this example Jenkinsfile 我現在的階段取得了一些進展看起來是這樣的: 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" }

不幸的是,雖然它看起來像詹金斯現在發現和運行sfdx工具,我得到一個新的錯誤:

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

回答

1

我遇到了同樣的問題。我得到了以下解決方法:

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詹金斯服務器上安裝習慣)。執行bat命令時添加${groovy_home}修復了我的問題。 這種調用工具的方式基本上來自腳本化的管道語法。 我用我所有的自定義工具(不僅groovy)。

工具部分:

tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation' 

由片段生成像你這樣產生的。

根據Jenkins users mailing list,工作仍在繼續爲最終的解決方案,所以我的解決方案真的是一個工作。