1. 程式人生 > >CentOS7——gitlab本地git倉庫搭建 以及web hook配置

CentOS7——gitlab本地git倉庫搭建 以及web hook配置

ron pda tran 訪問 visit epo 目前 down style

整個搭建用的都是各種默認設置,所以沒有用到高深的的東西,比較簡單,比較傻瓜式,這篇也僅僅是一個入門。

另外本文具有時效性,瀏覽本文請註意發表時間,為防止過時產生誤導,本文盡量把 “如何得知應該這樣做” 之類的內容放進去。

1、安裝GitLab

從官網獲得安裝步驟,並執行(不去官網也可以,本文已經把所有用到的命令放進來了,往下看吧)。

官網:https://about.gitlab.com/installation/

技術分享

選擇CentOS7

技術分享

進入了社區版的安裝步驟說明,gitlab分為社區版(免費)和企業版(收費),社區版目前已經滿足我們的需求,直接照著裏面的命令敲一遍即可,中間有個命令要下載三百多兆的文件,這裏把命令貼出來,省的再去官網了。

命令如下,加粗標下劃線的:

 1. Install and configure the necessary dependencies

If you install Postfix to send email please select Internet Site during setup. Instead of using Postfix you can also use Sendmail or configure a custom SMTP server and configure it as an SMTP server.

On CentOS, the commands below will also open HTTP and SSH access 
in the system firewall. sudo yum install curl policycoreutils openssh-server openssh-clients sudo systemctl enable sshd sudo systemctl start sshd sudo yum install postfix sudo systemctl enable postfix sudo systemctl start postfix sudo firewall-cmd --permanent --add-service=http sudo systemctl reload firewalld
2. Add the GitLab package server and install the package curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash sudo yum install gitlab-ce If you are not comfortable installing the repository through a piped script, you can find the entire script here and select and download the package manually and install using curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download rpm -i gitlab-ce-XXX.rpm 3. Configure and start GitLab sudo gitlab-ctl reconfigure 4. Browse to the hostname and login On your first visit, youll be redirected to a password reset screen to provide the password for the initial administrator account. Enter your desired password and youll be redirected back to the login screen. The default accounts username is root. Provide the password you created earlier and login. After login you can change the username if you wish.

第四條是說你安裝完成後的首次登陸會被重定向到一個密碼帳號設置頁,默認gitlab是跑在80端口的,以上步驟執行完後直接從本地訪問localhost,或者用其他機器訪問,然後設置密碼即可。

2、登陸gitlab並創建一個倉庫

默認用戶是root,密碼是你剛才設置的密碼,不建議使用該用戶登陸,建議註冊一個

技術分享

用新用戶登陸,點擊New project,創建一個項目

技術分享

填寫項目名,選擇private可設為私有項目,這樣你不添加協作者,他們是沒法搞你的項目的,這裏選擇private

技術分享

創建完成後

技術分享

中間的地址就是訪問地址,至此倉庫創建完畢。如何在客戶端使用不是本文 討論範疇,不再贅述,下面我們來看看web hook怎麽添加。

3、創建web hook

web hook 功能:

gitlab服務器可以在 用戶推送代碼到服務器、推送註釋到服務器等用戶事件發生的時候,將該事件的細節上報給你指定的web url。

你只需要告訴gitlab服務器,你想讓它訪問的url和在什麽事件產生時訪問該url即可。

新版本的web hook設置的位置稍有改變,在上圖所示的頁面上,點擊右上角的Settings,再選擇Integrations,就可以進入web hook創建界面,界面截圖如下:

技術分享

其中URL就是各種事件的推送地址,Secret Token則可以用來作為驗證字符串,這裏不填,下邊的Trigger則羅列了各種可以選擇的事件。填完保存

技術分享

保存完後,這條web hook就會出現在這個按鈕的下方

技術分享

這裏可以點Test來測試一下該地址。

這裏配置的地址是http://192.168.31.221:8080/test,選擇的事件是Push Events,

那麽一旦有人提交代碼到這個倉庫,服務器就會訪問http://192.168.31.221:8080/test,並把細節作為報文內容發送過去,一般會發送以下內容:

{
  "object_kind": "push",
  "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
  "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "ref": "refs/heads/master",
  "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "user_id": 4,
  "user_name": "John Smith",
  "user_username": "jsmith",
  "user_email": "[email protected]",
  "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
  "project_id": 15,
  "project":{
    "name":"Diaspora",
    "description":"",
    "web_url":"http://example.com/mike/diaspora",
    "avatar_url":null,
    "git_ssh_url":"[email protected]:mike/diaspora.git",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "namespace":"Mike",
    "visibility_level":0,
    "path_with_namespace":"mike/diaspora",
    "default_branch":"master",
    "homepage":"http://example.com/mike/diaspora",
    "url":"[email protected]:mike/diaspora.git",
    "ssh_url":"[email protected]:mike/diaspora.git",
    "http_url":"http://example.com/mike/diaspora.git"
  },
  "repository":{
    "name": "Diaspora",
    "url": "[email protected]:mike/diaspora.git",
    "description": "",
    "homepage": "http://example.com/mike/diaspora",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "git_ssh_url":"[email protected]:mike/diaspora.git",
    "visibility_level":0
  },
  "commits": [
    {
      "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "message": "Update Catalan translation to e38cb41.",
      "timestamp": "2011-12-12T14:27:31+02:00",
      "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "author": {
        "name": "Jordi Mallach",
        "email": "[email protected]"
      },
      "added": ["CHANGELOG"],
      "modified": ["app/controller/application.rb"],
      "removed": []
    },
    {
      "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "message": "fixed readme",
      "timestamp": "2012-01-03T23:36:29+02:00",
      "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "author": {
        "name": "GitLab dev user",
        "email": "[email protected](none)"
      },
      "added": ["CHANGELOG"],
      "modified": ["app/controller/application.rb"],
      "removed": []
    }
  ],
  "total_commits_count": 4
}

各種事件發送的報文內容略有不同,具體可以訪問你github服務器上的:

http://localhost/help/user/project/integrations/webhooks

地址來查看。

有了這樣一種設置,我們可以自己實現一些功能,比如如果推送了一個符合自己約定規則的標簽,那麽就自動獲取最新代碼並發布到某個服務器上,當然,我描述的這個功能Jenkins已經幫我們實現了,這個也不在本文中討論了。

以上,希望能對別人有所幫助,完畢。

CentOS7——gitlab本地git倉庫搭建 以及web hook配置