簡體   English   中英

GitLab 5.2 Post-Receive WebHook

[英]GitLab 5.2 Post-Receive WebHook

我在與生產網絡服務器相同的服務器上運行GitLab v5.2(/ var / www中的Document Root)。

我正在嘗試設置一個標准的GitLab Post-Receive Hook,但我發現很少有關於如何設置腳本來處理發布的JSON數據的信息。 我不打算做任何自定義的事情,只是開箱即用,我想在我的網站位置接收后接收數據(請記住在同一台服務器上),然后在收到時從origin-master拉出(提供后接收數據源於推送到主分支)。 這樣,在/ var / www中找到的網站總是與主人相同。

有人可以,從帖子數據中給我一個拉動腳本的例子,或者指向我正確的方向讓我創建一個?

GitLab Hook請求示例 - 對於那些沒有GitLab實例的人,這里是GitLab Post-Receive JSON數據的樣子(直接來自GitLab幫助)

{
 "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
 "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
 "ref": "refs/heads/master",
 "user_id": 4,
 "user_name": "John Smith",
 "repository": {
   "name": "Diaspora",
   "url": "git@localhost:diaspora.git",
   "description": "",
   "homepage": "http://localhost/diaspora",
 },
 "commits": [
   {
     "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
     "message": "Update Catalan translation to e38cb41.",
     "timestamp": "2011-12-12T14:27:31+02:00",
     "url": "http://localhost/diaspora/commits/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
     "author": {
       "name": "Jordi Mallach",
       "email": "jordi@softcatala.org",
     }
   },
   // ...
   {
     "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
     "message": "fixed readme",
     "timestamp": "2012-01-03T23:36:29+02:00",
     "url": "http://localhost/diaspora/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
     "author": {
       "name": "GitLab dev user",
       "email": "gitlabdev@dv6700.(none)",
     },
   },
 ],
 "total_commits_count": 4,
};

好吧,經過大量挖掘后,我找到了足夠的文檔來創建自己的腳本,這里是:

PHP

 error_reporting(E_ALL);
 ignore_user_abort(true);

 function syscall ($cmd, $cwd) {
    $descriptorspec = array(
            1 => array('pipe', 'w') // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
            $output = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
            proc_close($resource);
            return $output;
    }
 }

 if( $HTTP_RAW_POST_DATA && !empty( $HTTP_RAW_POST_DATA['ref'] ) ){
    // pull from master
    if( preg_match( '(master)', $HTTP_RAW_POST_DATA['ref'] ) )
            $result = syscall('git pull origin master', '/var/www/website/directory');
 }

因此,這完全符合其目的。 但現在我需要重新思考邏輯,甚至可能是哲學。 此方法將自動使/ var / www / website /目錄與master保持同步; 各種其他分支怎么樣? 我需要一些方法來通過我的Web服務器查看其他分支,因此開發團隊可以查看他們的工作......

這是我在想的:

而不是我的代碼只是在post字符串的ref部分中查找“master”,而是將post字符串拆分為“/”分隔符並彈出end元素:

 $branch = array_pop( split("/", $HTTP_RAW_POST_DATA['ref']) ); //this will return which branch the post data was sent from

然后我檢查這個分支是否在/ var / www / website /目錄/中有一個工作目錄,如/var/www/website/directory/master

if( is_dir( "/var/www/website/directory/$branch" ) ){ //check if branch dir exists
  $result = syscall("git pull origin $branch", "/var/www/website/directory/$branch");
} else {
  //if branch dir doesn't exist, create it with a clone
  $result = syscall("git clone ssh://git@git.mydomain.com/sadmicrowave/someproject.git $branch", "/var/www/website/directory");
  //change dir to the clone dir, and checkout the branch
  $result = syscall("git checkout $branch", "/var/www/website/directory/$branch");
} 

這個邏輯似乎相對穩固,只是在這里張貼以查看人們的意見。 使用此方法,開發人員可以創建新的遠程分支,然后推送到該分支,然后分支將被拉入/ var / www web目錄以供查看。

任何人都可以想到另一種方式,允許開發人員查看他們的開發分支或任何關於如何使這個腳本更好的建議?

謝謝

最新的文檔在應用程序本身: https//docs.gitlab.com/ce/user/project/integrations/webhooks.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM