2017-03-07 13 views
1

任何人都可以解釋爲什麼我會得到以下錯誤,什麼可能是他們的可能的解決方案?Jenkinsfile管道錯誤:「預期的符號」和「未定義的部分」

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 20: Expected a symbol @ line 20, column 4. environment {

WorkflowScript: 17: Undefined section "error" @ line 17, column 1.
pipeline {

在Jenkinsfile的代碼如下:

#!groovy 

def application, manifest, git, environment, artifactory, sonar 

fileLoader.withGit('[email protected]<reducted>', 'v1', 'ssh-key-credential-id-number') { 
    application = fileLoader.load('<reducted>'); 
    manifest = fileLoader.load('<reducted>'); 
    git = fileLoader.load('<reducted>'); 
    environment = fileLoader.load('<reducted>'); 
} 

pipeline { 
    agent { label 'cf_slave' } 

    environment { 
     def projectName = null 
     def githubOrg = null 
     def gitCommit = null 
    } 

    options { 
     skipDefaultCheckout() 
    } 

    stages { 
     stage ("Checkout SCM") { 
     steps { 
      checkout scm 

      script { 
       projectName = git.getGitRepositoryName() 
       githubOrg = git.getGitOrgName() 
       gitCommit = manifest.getGitCommit() 
      } 
     } 
     } 

     stage ("Unit tests") { 
     steps { 
      sh "./node_modules/.bin/mocha --reporter mocha-junit-reporter --reporter-options mochaFile=./testResults/results.xml" 
      junit allowEmptyResults: true, testResults: 'testResults/results.xml' 
     } 
     } 

     //stage ("SonarQube analysis") { 
     //... 
     //} 

     // stage("Simple deploy") { 
     // steps { 
     //  // Login 
     //  sh "cf api <reducted>" 
     //  sh "cf login -u <reducted> -p <....>" 
     //  
     //  // Deploy 
     //  sh "cf push" 
     // } 
     // } 
    } 

    post { 
     // always { 

     // } 
     success { 
     sh "echo 'Pipeline reached the finish line!'" 

     // Notify in Slack 
     slackSend color: 'yellow', message: "Pipeline operation completed successfully. Check <reducted>" 
     } 
     failure { 
     sh "echo 'Pipeline failed'" 
     // Notify in Slack 
     slackSend color: 'red', message: "Pipeline operation failed!" 

     //Clean the execution workspace 
     //deleteDir() 
     } 
     unstable { 
     sh "echo 'Pipeline unstable :-('" 
     } 
     // changed { 
     // sh "echo 'Pipeline was previously failing but is now successful.'" 
     // } 
    } 
} 

回答

4

您的管道大部分都沒問題 - 在Declarative pipeline塊之前添加腳本管道元素通常不成問題。

但是,在開始時,您正在定義一個名爲environment(和git)的變量,它們基本上覆蓋了各種管道插件聲明的元素。

即當您嘗試執行pipeline { environment { … } }時,environment指的是您的變量聲明,這會導致出錯。

重命名這兩個變量,您將修復第一條錯誤消息。

要解決的第二個錯誤消息,刪除從environment塊聲明變量的嘗試 - 這個塊僅供exporting environment variables在使用過程中構建步驟,如:

environment { 
    FOO = 'bar' 
    BAR = 'baz' 
} 

script塊你有沒有這些聲明就能正常工作。或者,您可以將這些變量聲明移動到腳本的頂層。

+0

謝謝@christopher奧爾。我是否也可以選擇你的大腦來處理Jenkins的這個問題? http://stackoverflow.com/questions/42664863/how-to-merge-a-successful-build-of-a-pull-request-using-a-jenkinsfile –

+0

噢不錯,不知道這一個! –

1

如果您使用聲明性管道(你是,例如,外pipeline步驟),則可能只聲明pipeline上外層,例如你不能有變量和函數定義。這是使用聲明式管道的缺點。

更多信息here

當我看到它,你可以解決這個以下幾種方式:

  1. 使用腳本式管道,而不是
  2. 之初到全球流水線庫移動代碼(可能是如果在多個地方使用一個值,則需要解決變量範圍的問題,但它應該是可行的。
  3. 將管道內的開始處的代碼移動到script步驟並將值a描述爲here
+0

換句話說,我需要將其更改爲腳本管道。目前還不清楚哪兩個人應該關注哪一個......或者推薦哪個。 –

+0

如果我切換到腳本管道,我怎樣才能在管道視圖中擁有舞臺「標籤」,特別是'checksout scm'。 –

+0

很多評論道歉。讓我再來看看劇本的另一個變體,很快。 –