2014-10-03 96 views
1

我有一個jenkins作業,我想在我的Build Flow groovy腳本中使用它的lastSuccessfulBuild編號。 我可以從詹金斯API在獲得最後成功的版本號:在Jenkins groovy作業配置中使用latestSuccessfulBuild編號

的http:// {JENKINS_DOMAIN} /工作/ {JOB_NAME}/lastSuccessfulBuild/buildNumber

我使用Groovy的RESTClient我試着生成流Groovy腳本,但是當導入groovyx.net.http.RESTClient庫時,我得到語法錯誤。 有沒有人知道避開這個錯誤或以其他方式獲取api結果?

回答

3

也許這將幫助你:

import hudson.model.Build; 
println(build.getProject().getLastSuccessfulBuild()) 

例如,我們有簡單的構建流程Groovy腳本建築只有一個項目「職吧」。如果我們想檢查並打印上一次成功的構建,我們可以編寫這樣的腳本:

import hudson.model.Build; 

def buildA = build("jobA") 
println(buildA.getProject().getLastSuccessfulBuild()) 
+0

如果您可以擴展您的答案並提供一些說明,將會有所幫助。 – 2014-10-07 11:59:00

+1

我編輯了我的答案。 – jussuper 2014-10-07 12:35:14

1

可能有點矯枉過正,但是你可以使用HttpClient,因爲你需要的只是對url的get請求。

這裏有一個我從一些代碼敲起來我已經躺在附近

測試了我們其中有超過SSL的基本身份驗證自己的詹金斯實例。

import org.apache.http.HttpResponse 
import org.apache.http.HttpVersion 
import org.apache.http.client.HttpClient 
import org.apache.http.client.methods.HttpGet 
import org.apache.http.client.params.ClientPNames 
import org.apache.http.conn.ClientConnectionManager 
import org.apache.http.conn.scheme.PlainSocketFactory 
import org.apache.http.conn.scheme.Scheme 
import org.apache.http.conn.scheme.SchemeRegistry 
import org.apache.http.conn.ssl.SSLSocketFactory 
import org.apache.http.impl.client.DefaultHttpClient 
import org.apache.http.impl.conn.PoolingClientConnectionManager 
import org.apache.http.params.BasicHttpParams 
import org.apache.http.params.HttpConnectionParams 
import org.apache.http.params.HttpParams 
import org.apache.http.params.HttpProtocolParams 


class LastSuccessfulBuild { 
    def static main(args) { 
     println new LastSuccessfulBuild().connect("your.jenkins.com", "443", "/path/to/job/YourJob/lastSuccessfulBuild/buildNumber", "your.user:your-password") 
    } 

    def connect(host, port, path, auth) { 
     def url = new URL("https", host, Integer.parseInt(port), path) 
     HttpClient client = createClient() 
     HttpGet get = new HttpGet(url.toURI()) 
     get.setHeader("Authorization", "Basic ${auth.getBytes().encodeBase64().toString()}") 

     HttpResponse response = client.execute(get) 
     def status = response.statusLine.statusCode 
     if (status != 200) { 
      throw new IOException("Failed to get page, status: $response.statusLine") 
     } 
     return response.entity.content.text 
    } 

    def createClient() { 
     HttpParams params = new BasicHttpParams() 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1) 
     HttpProtocolParams.setContentCharset(params, "UTF-8") 
     params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true) 

     SchemeRegistry registry = new SchemeRegistry() 
     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)) 
     registry.register(new Scheme("https",SSLSocketFactory.getSocketFactory(),443)) 

     ClientConnectionManager ccm = new PoolingClientConnectionManager(registry) 
     HttpConnectionParams.setConnectionTimeout(params, 8000) 
     HttpConnectionParams.setSoTimeout(params, 5400000) 
     HttpClient client = new DefaultHttpClient(ccm, params) 

     return client 
    } 

} 
相關問題