跳到主要內容

發表文章

目前顯示的是有「jenkins」標籤的文章

Jenkins - Authenticating scripted clients

Problem 在啟用Jenkins Security設定後,不管是web操作還是透過script client形式都必須要認證才更夠使用。本篇文章主要分享我有使用到的script client的認證方式。 How to? Get API Token 使用scripted client存取Jenkins時,為了安全考量,Jenkins官方建議用API token取代真實密碼。取得API Token方式很簡單,假設你的scripted client所使用的user名稱為build,請先使用build登入Jenkins;接著到使用者設定畫面中的API Token點擊Show API Token,就能看到以下畫面: 假如要變更可以點擊Change API Token。 Scripted Client 我分享我有使用到的部分,如果有增加會更新: Java - HttpClient 4.1.3 我使用到的是HttpPost去通知Jenkins build,做法就是把使用者與密碼設定為Basic Authorization的Header: private void setupAuthorizationHeader ( HttpPost httpPost, String id, String passwd ) { String token = id + ":" + passwd ; String header = Base64. encodeBase64String ( token. getBytes ( StandardCharsets. UTF_8 ) ) . trim ( ) ; httpPost. addHeader ( "Authorization" , "Basic " + header ) ; } 這裡我只列出重要部分的程式碼,client code就不放了。 CURL curl -X GET --user "build:d1d26b55cec0378450abd1a2df8d3e99" http: // 192.168.0.1: 8080 // job / Test / lastSuccessfulBuild / art...

Jenkins - Get Artifacts with REST API

Problem 為了節省軟體的反安裝、下載與安裝時間,我們有隻script,會去jenkins抓某個固定位置的最新安裝程式並安裝。然而,開發過程會因為新功能或修bug等原因產生branch;原本的腳本並無法根據branch去下載安裝程式,也因此花了些時間去研究並解決這個問題。 How to? jenkins既然有RestAPI,應該就有辦法讓人可以存取到它專案相關資訊吧? 為了達到我們目的,其中會包含幾個步驟: 取得專案build列表。 取得branch吻合的build URL。 取得此build的artifact列表。 過濾想要的artifacts。 以專案名稱Example為例,其專案的URL為: http://tonylin.idv/job/Example 取得專案build列表 首先可以透過此URL去搜尋所有build資訊: http://tonylin.idv/job/Example/api/json?tree=builds[*] 以一個build的結果如下: { "builds": [ { "actions": [ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ], "artifacts": [ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ], "building": false, "description": "origin/integration_testing", "displayName": "#6611", "duration": 375922, "estimatedDuration": 366451, "executor": null, "fullDisplayName": "Example #6611", "id": ...

Jenkins - DirectoryBrowserSupport.SCP Problem

Problem 某天更新Jenkins後,RobotFramework的報表無法完整呈現,像CSS不見、javascript無法執行還有圖片無法顯示等。這問題是由於 Content Security Policy 所造成的,必須設定Jenkins變數去解決。 How to? 首先到Script Console: %JENKINS_URL%/script中,執行以下命令確認你目前的policy: System . getProperty ( "hudson.model.DirectoryBrowserSupport.CSP" ) 確認需要打開的項目後,執行以下指令去設定新的policy(以下是我們的範例): System . setProperty ( "hudson.model.DirectoryBrowserSupport.CSP" , "default-src 'unsafe-inline' 'unsafe-eval' img-src * data:;" ) 在有問題的網頁要清除網頁暫存後重新整理,應該就可以看到你要的內容。而啟動設定要記得改/etc/sysconfig/jenkins: JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Xloggc:/opt/jenkins/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Dhudson.model.DirectoryBrowserSupport.CSP=\"default-src 'unsafe-inline' 'unsafe-eval' img-src \"*\" data:;\"" (註: \“*\”的寫法還沒試驗過,已知直接寫*會有問題。) Resource Jenkins - 'allow-scripts' permission is not set Jenkins Content Security Policy Jenkins - Configuring Content Security Policy c...

Jenkins上無法看到Robotframework測試報表圖片

要設定Other files to copy去蒐集執行結果內容:

Jenkins - Git Branch變數可以用在哪?

Introduction 我們團隊中,有一個build大神,我從他身上學到許許多的Jenkins應用。本篇文章主要紀錄Git Branch變數可以應用在Jenkins上的哪些地方。 根據Git Branch執行建置工作 建置專案的參數非常好用,可以用來客製化建置需求。在這裡我宣告了一個名為BRANCH的參數: 接著在Source Code Management選完Git後,可以將這個變數設定給Branches to build,這樣執行建置時,就會根據你設定的BRANCH名稱去checkout GIT repository: 假如你使用Ant去執行建置且會根據BRANCH有不同操作,也可以把這個變數直接傳遞給你的Ant script。 將Git Branch名稱顯示於Build History 在Build History可以直接看到branch名稱,可以省去你還要一個一個點去找到你想要的結果,這個在多個功能同時開發很好用。 方法很簡單,就是啟用Post-build Actions的Set build description的功能: Regular expression設定為: ^Checking out Revision .* [(]((?!master).)*[)]$ 這代表著master branch就不會設定description,其它的branch則會。(這行是要去build console log的Checking out Revision這行抓出branch名稱)