配置github的pull request觸發jenkins自動構建


 

參照:

https://www.cnblogs.com/zanjiahaoge666/p/6402738.html

 

  之前的配置,都是向master分支push操作觸發jenkins進行構建,但是在一般的正常工作中,不會允許程序員直接向主分支推送代碼;正常都是fork一個本地的分支,在本地分支調試完后,向主干分支提交pull request,待相關的管理人員進行review后,才merge到master分支;

  基於此,我們之前的配置就有點不合適了,接下來我們就一塊研究下如何在別人提交pull request時,就自動觸發構建,當然這個構建要執行的任務,應該是將新提交的代碼獲取到服務器,並部署到環境當中,這應該是一些基本的操作,不懂就問下別人你們的環境如何部署;這里我們主要看如何配置jenkins能使pull request觸發構建;

 

第一步:安裝pull request builder plugin插件 

  jenkins插件管理中,可選插件中搜索pull request builder plugin,選中后點擊直接下載,插件安裝完一般都需要重啟jenkins以生效;

  這個插件應該是最近版本新出的,因為網上的資料確實少,甚至有些文章說不需要特別的插件,只要勾選了Build when a change is pushed to GitHub,不管是push還是pull request都能觸發jenkins構建,這應該是不靠譜的;

  pull request builder plugin插件官方文檔:https://github.com/jenkinsci/ghprb-plugin/blob/master/README.md

 

第二步:配置pull request builder plugin插件

  安裝好插件以后,jenkins》》系統管理》》系統設置》》GitHub Pull Request Builder節點:

  

  GitHub Server API URL:  如果不是使用的企業版jenkins,就保持默認的https://api.github.com;

  Jenkins URL overrde:  如果考慮到防火牆、跨域等問題,可以寫一個替換連接jenkins主頁的url;

  Shared secret:  如果填寫一個密碼,則每一個提交的pull request都需要驗證這個密碼才能連接jenkins;

  Credentials:  選擇我們之前用github生成的token創建的認證身份;一般這個身份在github中具有較高的權限;

  完后點擊下方的測試鏈接,來測試jenkins與github的基本連接以及身份用戶的權限驗證;

  

  其中Repository ownere/name,顧名思義就是填寫github的用戶名跟遠程庫名,中間有反斜杠/;

  在Description寫一個這個Credentials的描述,以區分將來不同Credentials;

  

  下面Admin list中填寫github用戶白名單,在白名單中的用戶提交pull request,可以直接觸發構建,沒有在白名單中的,需要通過admin的確認;感受下官網的描述:

  When a new pull request is opened in the project and the author of the pull request isn't whitelisted, builder will ask Can one of the admins verify this patch?. One of the admins can comment ok to test to accept this pull request for testing, test this please for one time test run and add to whitelist to add the author to the whitelist.

  If an author of a pull request is whitelisted, adding a new pull request or new commit to an existing pull request will start a new build.

  ok,配置完后點擊保存;

 

第三步:配置jenkins任務

  基本的配置跟之前的類似,我這里只提不同的地方:

  1、源碼管理

  

  基本配置搞定后,點擊高級:

  Name填寫origin

  Refspec填寫  +refs/pull/*:refs/remotes/origin/pr/*

  Branch Specifier填寫${sha1},如果想要用到提交的pr,則這個地方填寫${ghprbActualCommit}

  官網描述:

  • Under Advanced, set Name to origin and:
    • If you just want to build PRs, set refspec to +refs/pull/*:refs/remotes/origin/pr/*
    • If you want to build PRs and branches, set refspec to +refs/heads/*:refs/remotes/origin/* +refs/pull/*:refs/remotes/origin/pr/* (see note below about parameterized builds)
  • In Branch Specifier, enter ${sha1} instead of the default */master.
  • If you want to use the actual commit in the pull request, use ${ghprbActualCommit} instead of ${sha1}

  2、構建觸發器

  

  如果插件安裝正常,構建觸發器這邊會有GitHub Pull Request Builder,不要選擇之前的push觸發的那個咯;下面的credentials選擇下拉框給出的就好,應該就是之前在系統設置中測試通過的那個;

  Admin list填寫對於這個job要加入白名單的github用戶;下面的Use github hooks for build triggering勾選;

  3、構建步驟

  

  如上截圖,在構建操作中,我設置了兩個Execute shell用以區分構建操作的兩個步驟;第一個是在提交pr,並將pr拉取下來merge到jenkins工作目錄中配置的代碼分支后,執行環境部署的操作,這里請根據你們項目中實際的操作進行配置;第二個Execute shell是在部署好了新環境的基礎上,執行postman的api腳本;只有在集成了新pr的環境中執行,才能對該pr進行測試,看是否會對整個環境的api穩定性造成什么影響;

 

 

  另外如果配置的沒有問題,在github對應的遠程庫,Settings>>Webhooks中,會生成一個新的webhook:

  

  要保證這個hook的觸發方式是pull_request,如果不是,需要點擊Edit進行修改:

  

  與Pull request相關的有三個,從名稱上就可以看出,下面兩個一個是對pr進行review時觸發,另一個是對pr的review進行評論;根據需要進行配置,一般情況只要Pull request就可以,包括新建pr、關閉pr(目測不會觸發)、編輯pr等等都會觸發github想jenkins發送post請求,jenkins執行構建操作;

  其他的跟之前的配置相同,這里就不啰嗦了,不懂得看我其他幾篇文章吧;

  

  ok,到這里配置就完成了,保存一下提交一個pull request試試看吧;

  github也是工作中比較常用的工具,這里沒有出相關的學習資料,因為在下也是在工作中零零碎碎學習的,比較不系統╮(╯▽╰)╭;需要的話,大家可以在網上搜一下教程;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM