差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

Both sides previous revision 前次修改
下次修改
前次修改
jenkins:restapi:get_artifacts [2016/12/23 23:25]
tony [Sample code]
jenkins:restapi:get_artifacts [2023/06/25 09:48] (目前版本)
行 2: 行 2:
 ====== Get Artifacts with RestAPI ====== ====== Get Artifacts with RestAPI ======
 ===== Problem ===== ===== Problem =====
-原先為了自動安裝測試開發的軟體,我們有隻腳本,會去jenkins抓某個固定位置的最新安裝程式並安裝。然而,開發過程會因為新功能或修bug等原因產生branch;原本的腳本並無法根據branch去下載安裝程式,也因此花了些時間去研究並解決這個問題。主要目的還是為了節省反安裝、下載與反安裝的時間+為了節省軟體的反安裝、下載與安裝時間,我們有隻script,會去jenkins抓某個固定位置的最新安裝程式並安裝。然而,開發過程會因為新功能或修bug等原因產生branch;原本的腳本並無法根據branch去下載安裝程式,也因此花了些時間去研究並解決這個問題。
 ===== How to? ===== ===== How to? =====
 jenkins既然有RestAPI,應該就有辦法讓人可以存取到它專案相關資訊吧?​ 為了達到我們目的,其中會包含幾個步驟:​ jenkins既然有RestAPI,應該就有辦法讓人可以存取到它專案相關資訊吧?​ 為了達到我們目的,其中會包含幾個步驟:​
行 85: 行 85:
 http://​tonylin.idv/​job/​Example/​api/​json?​tree=builds[description,​result,​displayName,​url] http://​tonylin.idv/​job/​Example/​api/​json?​tree=builds[description,​result,​displayName,​url]
 </​code>​ </​code>​
-三個build的輸出結果如下,從結果內容不難得知個別資訊意義,需注意的是master的description會是null:​+三個build的輸出結果如下,從結果內容不難得知個別資訊意義,需注意的是master的description會是null或master(我們使用dscription當branch名稱):
 <​code>​ <​code>​
 { {
行 156: 行 156:
     found_build = None     found_build = None
     for build in json_object["​builds"​]:​     for build in json_object["​builds"​]:​
-        if build["​description"​] == branch+        if not branch and build["​description"​] == "​master"​
-            ​found_build = build +            ​return ​build 
-            ​break +        elif build["​description"​] == branch
-    if found_build+            return ​build
-        return ​found_build+
     return None     return None
  
行 180: 行 179:
             return build_url + "​artifact/"​ + relative_path             return build_url + "​artifact/"​ + relative_path
     return None     return None
 +</​code>​
 +簡單的測試案例:​
 +<code python>
 +from unittest import TestCase
 +from jenkins.utils import find_latest_build
 +from jenkins.utils import find_installer
 +
 +
 +class TestJenkinsUtils(TestCase):​
 +    base_url = "​http://​tonylin.idv/​job/"​
 +    project = "​Example"​
 +    installer_prefix = "​Example"​
 +
 +    def test_find_latest_master_build(self):​
 +        latest_build = find_latest_build(TestJenkinsUtils.base_url,​ TestJenkinsUtils.project)
 +        self.assertTrue(latest_build["​url"​].startswith(TestJenkinsUtils.base_url))
 +        self.assertIsNone(latest_build["​description"​])
 +        self.assertIsNotNone(latest_build["​displayName"​])
 +        self.assertEqual("​SUCCESS",​ latest_build["​result"​])
 +
 +    def test_find_latest_branch_build(self):​
 +        branch_name = "​origin/​integration_testing"​
 +
 +        latest_build = find_latest_build(TestJenkinsUtils.base_url,​ TestJenkinsUtils.project,​ branch_name)
 +        self.assertTrue(latest_build["​url"​].startswith(TestJenkinsUtils.base_url))
 +        self.assertEqual(branch_name,​ latest_build["​description"​])
 +        self.assertIsNotNone(latest_build["​displayName"​])
 +        self.assertEqual("​SUCCESS",​ latest_build["​result"​])
 +
 +    def test_find_none_build(self):​
 +        branch_name = "​origin/​none"​
 +
 +        latest_build = find_latest_build(TestJenkinsUtils.base_url,​ TestJenkinsUtils.project,​ branch_name)
 +        self.assertIsNone(latest_build)
 +
 +    def test_find_build_with_invalid_url(self):​
 +        branch_name = "​origin/​none"​
 +        try:
 +            latest_build = find_latest_build(TestJenkinsUtils.base_url,​ "​InvalidProject",​ branch_name)
 +        except RuntimeError as e:
 +            self.assertEqual("​request failed, status code=404",​ e.message)
 +
 +    def test_find_installer(self):​
 +        latest_build = find_latest_build(TestJenkinsUtils.base_url,​ TestJenkinsUtils.project)
 +        installer_url = find_installer(latest_build["​url"​],​ TestJenkinsUtils.installer_prefix)
 +        self.assertTrue(installer_url.startswith(TestJenkinsUtils.base_url) and installer_url.__contains__(TestJenkinsUtils.installer_prefix))
 +
 </​code>​ </​code>​
 \\ \\