顯示具有 gitlab 標籤的文章。 顯示所有文章
顯示具有 gitlab 標籤的文章。 顯示所有文章

2019年2月18日 星期一

Gitlab+Jenkins實現自動部署

1. 安裝(使用yum安裝,略)

jenkins 2.150.1
gitlab ( GitLab Community Edition 11.5.0 )

2. 配置gitlab

打開project、配置SSH密鑰、上傳code(略)

3. 配置Jenkins

a. 新增插件

Manage Jenkins => Manage Plugins => 安裝以下插件
Gitlab Hook
Build Authorization Token Root
Publish Over SSH
Gitlab Authentication
Gitlab

添加完插件重啟jenkins,開始添加要部署代碼的主機,注意一定是要能夠ssh登陸的用户

b. 添加主機

Manage Jenkins => Configure System => Publish over SSH

c. 新建任務

New Item => (填寫任務名字)Freestyle project => Source Code Management
Build Triggers
Build
Exec command:
# 重啟nginx
/usr/local/nginx/sbin/nginx -s reload
# 同步.git 目錄(git log)
rsync -r /var/lib/jenkins/workspace/test/.git /path/test/jenkins_cd
# 切到develop分支
git --git-dir=/path/test/jenkins_cd/.git remote update
git --git-dir=/path/test/jenkins_cd/.git branch -D develop
git --git-dir=/path/test/jenkins_cd/.git checkout -b develop remotes/origin/develop

4. 配置gitlab,當有代碼提交時,觸發jenkins的部屬操作


出現Hook executed successfully: HTTP 200 => 說明配置成功

5. 加入git-flow

test-release (預發佈分支的jenkins item)

Source Code Management
Branches to build - Branch Specifier (blank for 'any'): */master => 從master生release分支
Build Triggers
Allowed branches - Filter branches by regex - Target Branch Regex: .*master
Build
SSH Publishers - Transfers - Remote directory: /path/test/jenkins_release
SSH Publishers - Transfers - Exec command:
/usr/local/nginx/sbin/nginx -s reload
# 同步 release worksapce的git log 到release項目路徑上
rsync -r /var/lib/jenkins/workspace/test-release/.git /path/test/jenkins_release
# 切換到release 分支
git --git-dir=/path/test/jenkins_release/.git remote update
git --git-dir=/path/test/jenkins_release/.git branch -D release
git --git-dir=/path/test/jenkins_release/.git checkout -b release remotes/origin/master
# 配置.git目录权限
chmod 777 -R /path/test/jenkins_release/.git/


test-master(線上分支的jenkins item)

Source Code Management
Branches to build - Branch Specifier (blank for 'any'): */master
Build Triggers
Allowed branches - Filter branches by regex - Target Branch Regex: .*master
Build
SSH Publishers - Transfers - Remote directory: /path/test/jenkins_master
SSH Publishers - Transfers - Exec command:
/usr/local/nginx/sbin/nginx -s reload
# 同步 master worksapce的git log 到master項目路徑上
rsync -r /var/lib/jenkins/workspace/test-master/.git /path/test/jenkins_master
# 可以使用一些jenkins中的變量
echo "$PWD bear $JOB_NAME-$BUILD_NUMBER $GIT_COMMIT $GIT_PREVIOUS_COMMIT $GIT_PREVIOUS_SUCCESSFUL_COMMIT start" >> /tmp/bear2
# 可以使用bash的條件判斷
if [ "$GIT_COMMIT" = "$GIT_PREVIOUS_COMMIT" ]
then
    echo "var is 123"  >> /tmp/bear2
else
    echo "var is 456"  >> /tmp/bear2
fi
# 切換到master分支
git --git-dir=/path/test/jenkins_master/.git remote update
git --git-dir=/path/test/jenkins_master/.git branch -D master
git --git-dir=/path/test/jenkins_master/.git checkout -b master remotes/origin/master
# 合併release分支內容
git --git-dir=/path/test/jenkins_master/.git --work-tree=/path/test/jenkins_master/ merge remotes/origin/release
# 更新到倉庫的master分支
git --git-dir=/path/test/jenkins_master/.git push origin master:master
# 删除release远程分支
git --git-dir=/path/test/jenkins_master/.git push origin --delete release
# 删除release环境
rm -rf /path/test/jenkins_release/*
rm -rf /path/test/jenkins_release/.git//

ps. test-release、test-master皆需配gitlab webhook

6. 流程

開發在本地環境開發完+自測後更新到develop分支,jenkins會自動部屬測試環境給測試測,測試測完build test-release環境(從master分支分出來的),release環境只有一個,兩人同時要上時必須等待,開發上release環境將develop分支中的commit cherry-pick到release分支中,測試測完release後更新到release分支,線上發佈時build test-master,部屬完更新到master分支、刪除倉庫release分支、release環境

7. Jenkins 建置狀態回寫gitlab

a. 安裝gitlab、jenkins安裝gitlab plugin

略

b. Gitlab 使用者設定

GitLab 主畫面開啟帳號圖像下拉選單,依序點選「Settings」 → 「Access Tokens」
產生一個Personal Access Tokens
然後你就可以用這個token存取gitlab api
ex. http://gitlab.server.com/api/v4/projects?private_token=xxx

c. Jenkins 設定

Manage Jenkins => Configure System => Gitlab
Gitlab區塊輸入「Connection name」與「Gitlab host URL」
於「Credentials」旁按下【Add】 → 【Jenkins】,開啟「新增 Credentials」頁面
然後填入以下欄位(API token為你剛剛在gitlab生成的access tokens)

d. 工作組態設定

test item => Configure
General
GitLab Connection: 選擇 Gitlab
Post-build Actions
按下【Build Now】,此時此項工作會進行建置並使用 GitLab connection 將建置狀態發送至 GitLab 上。

8. 可能的報錯

a. Gitlab webhook 報錯: 500 Internal Server Error - URI::InvalidURIError

使用root登入gitlab => Admin area => Settings => Network => Outbound requests => 勾選「Allow requests to the local network from hooks and services」
ps. 可能會因為gitlab 版本不同,而菜單位置有差異

b. 永遠使用英文介面

因為jenkins 預設文字為用戶瀏覽器語言,但是網路上查到的資料都是英文的,如何使介面為英文?
i. Jenkins安裝 Locale Plugin
ii. Jenkins configuration => Locale => Default Language: en

參考資料:

https://hk.saowen.com/a/286fcee36ef6c80c697b392adeb8249047a9c282b50fe82b42526af3f673562a  Gitlab+Jenkins實現自動部署(主要)
https://dotblogs.com.tw/echo/2018/04/29/jenkins_plugin_gitlab_publishbuildstatus  【Jenkins】外掛套件:GitLab(Jenkins 建置狀態回寫)(次要)
https://linuxize.com/post/how-to-install-jenkins-on-centos-7/  How To Install Jenkins on CentOS 7
https://web.devdon.com/archives/10  GitLab搭配Jenkins實現可持續集成開發
https://blog.csdn.net/u010947098/article/details/61922969  jenkins使用publishover ssh插件连接应用机器时,报Message [Auth fail]的问题(jenkins.plugins.publish_over.BapPublisherException: Failed to connect and initialize SSH connection. Message: [Failed to connect session for config [测试机]. Message [Auth fail]])
https://stackoverflow.com/questions/49626860/gitlab-webhook-get-error-500-internal-server-error-uriinvalidurierror  Gitlab webhook get error: 500 Internal Server Error - URI::InvalidURIError
https://stackoverflow.com/questions/1386291/git-git-dir-not-working-as-expected  git --git-dir not working as expected
https://stackoverflow.com/questions/12350707/jenkins-in-windows-shows-ui-in-russian-language  Jenkins in Windows shows UI in russian language
https://alexbilbie.com/2015/04/setting-up-jenkins/  Setup Jenkins and test a PHP project


2016年5月17日 星期二

gitlab 自動custom hook佈署到server上

gitlab和web server現在放在同一台機器上,想要本地測試完git push到機器上後自動佈署到web服務上
因為懶得搞另一套 jenkins,所以打算用hook做

方案有
git本身的hook
gitlab的 custom hook   http://docs.gitlab.com/ee/hooks/custom_hooks.html
gitlab的 web hook   http://docs.gitlab.com/ee/web_hooks/web_hooks.html

這裡選用gitlab的 custom hook

步驟:
0. 切換到git使用者,因為gitlab文件的使用者權限都是git
# sudo su git
1. 假設root使用者的test專案需要hook
2. 找server上test.git庫的位置,我的放在 /var/opt/gitlab/git-data/repositories/root/test.git ,但也有可能放在 /home/git/repositories/<group>/<project>.git ,依你安裝的方式不同而不同
3. 進入該資料夾後新建 custom_hooks 資料夾
4. 進入 custom_hooks 資料夾,在這邊新增原git hook類型的檔案,ex. pre-receive。這邊新增 post-receive這個鉤子文件,當push完成後這個文件就會被調用
post-receive
#!/bin/sh
git --work-tree=/var/www/test.localhost/html --git-dir=/var/opt/gitlab/git-data/repositories/root/test.git checkout -f
--git-dir git庫所在位置
--work-tree 實際文件被存放的位置
5. 別忘了賦予 post-receive 可執行權限
$ chmod +x post-receive
6. /var/www/test.localhost/html 路徑也必須是git權限
# chown git:git -R /var/www/test.localhost/html

最後在本地端測試push後,機器上web服務也馬上更新了,
如果 /var/www/test.localhost/html 上面原本有內容的檔案不會清空,只會被覆蓋

參考資料:
http://sumyblog.me/2015/11/02/use-git-hooks-for-hexo-automatic-deployment/  使用git hooks进行hexo博客自动化部署


2015年1月20日 星期二

gitlab 心得

使用Omnibus package embed安装的gitlab
https://about.gitlab.com/downloads/
路徑放在:
/var/opt/gitlab/
而非
/home/git/gitlab/ 下
重啟gitlab:
gitlab-ctl restart  #重啟動後刷頁要等一段時間,而非像apache馬上生效
而非
/etc/init.d/gitlab restart

設定檔在:
/var/opt/gitlab/gitlab-shell/config.yml

log檔在:
tail -f /var/log/gitlab/nginx/gitlab_error.log
tail -f /var/log/gitlab/nginx/gitlab_access.log

查gitlab版本
http://yourdomain/help

git push錯誤:(這是突發事件,之前用正常)
$ git push -u origin master
Agent admitted failure to sign using the key.
Access denied.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
修改config.yml的
gitlab_url: "http://domain" # "http://domain:8080(/)", "http://wlan_ip(/)", "http://wlan_ip:8080(/)", "http://localhost:8080(/)", "http://localhost(/)"
self_signed_cert: false => true
都無解

暴力解法:
去 /var/opt/gitlab/git-data/repositories/username 下
因為這邊在gitlab GUI介面沒開project-new.git資料夾出來,所以推上去找不到地方存
/var/opt/gitlab/git-data/repositories/bear# ls
project-old.git    project-old.wiki.git    project-new.wiki.git   ( 沒開project-new.git )

隨便cp一個舊的project,然後把使用者改git:git,權限改777(不改777可以拉下來但推上去gitlab會沒紀錄)
# cp -r project-old.git project-new.git
# chown git:git -R project-new.git
# chmod 777 -R project-new.git
這樣在http://domain/username/project-new/commits/master 下就可以看到log了,只是會帶舊的log在裡面,我試過開新的project-new然後git init --bare,ln -s hooks /opt/gitlab/embedded/service/gitlab-shell/hooks ,但無效,gitlab看不到專案

....過了幾個小時候,又可以正常用了,看來是偶發事件....不確定跟我中間reboot重啟機契友沒有關係

更改gitlab後台repo的網址
將gitlab後台網址
git@li8xx-xxx.members.linode.com:root/test.git
改成
git@your.domain:root/test.git
修改 /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
host: li8xx-xxx.members.linode.com => your.domain
重啟gitlab
# gitlab-ctl restart



2015年1月19日 星期一

git合併兩個現有repos的紀錄到同一個repos

假設要把mp3合併到mp2(主要)
下載兩個repos並進入其中主要的branch
$ git clone git@your.domain:bear/mp2.git
$ git clone git@your.domain:bear/mp3.git
$ cd mp2
把mp3加入mp2的remote
$ git remote add mp3 ../mp3
$ git fetch mp3
$ git branch mp3 mp3/master
$ git merge mp3
合併後的git log圖如下:
*   0f54c78 2015-01-19 | fix conflict merge [bear]
|\
| * dc09a26 2015-01-19 | add mp3.txt (mp3/master, mp3) [bear]
| * d6b0993 2015-01-19 | mp3 test commit 2 [bear]
| * 9d89949 2015-01-19 | mp3 first commit [bear]
*   4fea708 2014-11-23 | Merge branch 'master' of your.domain:bear/mp2 (origin/master, origin/HEAD) [b
|\
| * fdc6ced 2014-11-23 | gitlab [bear]
* 719d8ef 2014-11-23 | gitlab [bear]
* a66d115 2014-11-23 | Initial commit [kalecgos0616]
( ~/.bashrc:設定 alias glog='git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short')
要重新回到mp2 被merge前的 commit( 4fea708 ) 並保留log,因為 0f54c78 中混合了4fea708 和 dc09a26 的code,有衝突的檔案要手動解決conflict,要回到最原本的4fea708
方法1. ( 會多3個revert的commit )
$ git revert dc09a26 d6b0993 9d89949
方法2. ( 只會多一個commit,並回到 4fea708 的狀態)
$ git revert -m 1 0f54c78

下面這方法試不出來,可能是我的0f54c78 是兩個repos merge的node
$ git revert HEAD~2..HEAD

ps. 把本地端某個branch推到gitlab上
$ git checkout -b branch_name remote_name/branch_name
$ git push -u origin branch_name

參考資料:
http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit
http://blog.caplin.com/2013/09/18/merging-two-git-repositories/
http://stackoverflow.com/questions/2765421/push-a-new-local-branch-to-a-remote-git-repo-and-track-it-too

2015年1月16日 星期五

git 搬移log到svn下

需求:
git下的所有log資料搬到svn 某新資料夾下(svn其他資料夾已有log)

過程照參考資料做基本上就可以了...
1. Create Subversion-aware Git clone ( 首先你要安裝git-sv )
 $ git svn clone http://svn.localhost/svn/trunk/company
 $ cd trunk
 $ git fetch git@your.domain:username/project.git # 我是抓我gitlab上的git

 $ git branch tmp $(cut -b-40 .git/FETCH_HEAD)
$(cut -b-40 .git/FETCH_HEAD) 是原本gitlab最後一個commit id
 $ git tag -a -m "Last fetch" last tmp

2. Apply initial commit
 $ INIT_COMMIT=$(git log tmp --pretty=format:%H | tail -1)
$(git log tmp --pretty=format:%H | tail -1)是tmp branch log的最後一個 commit id
 $ git checkout $INIT_COMMIT .  #注意最後要有點(.)
 $ git commit -C $INIT_COMMIT

3. Rebase and submit
 $ git rebase master tmp
 $ git branch -M tmp master

 $ git svn dcommit

4. Update Google Code
 ...
 $ mv .git/refs/tags/newlast .git/refs/tags/last #這步我沒做



參考資料:
http://code.google.com/p/support/wiki/ImportingFromGit

2014年11月23日 星期日

git同時push到兩個repo

情境:
以github和gitlab為例,假設他們各自有不同的內容要整合在一起

模擬環境
分別在github和gitlab建立新專案(ex 專案名稱mp),gitlab為master的pull
master 更新檔案 pull從gitlab
master 上傳檔案push到 gitlab和github(github純備份用,因為gitlab上是和其他人共同開發)

(分別在github和gitlab的mp.git新增不同的內容,我們之後要整合在一起)

拉下gitlab檔案
$ git clone git@xxx.tw:bear/mp.git gitlab_mp #用ssh要在gitlab設定keys,用https不用,但要輸入gitlab的帳號密碼
新增remote
$ git remote add github git@github.com:bear/mp.git
更新remote(remote branch會多remotes/github/master,用git branch -a 查詢)
$ git remote update

更新特定的remote

$ git remote update github

(這邊假設github和gitlab裡面各自有不同內容要整合,如果備份的repo為新開的repo可以略過a.b.c這三步)
a. 開github的branch
$ git checkout -b github remotes/github/master
b. 切回master
$ git checkout master
c. 將github整合進gitlab
$ git rebase github


git pull(做這步之後才能push,中間有conflict就手動改)
$ git pull #這步會多一個git merge的 commit
設定origin remote的url
$ git remote set-url origin --push --add git@xxx.tw:bear/mp2.git
$ git remote set-url origin --push --add git@github.com:bear/mp.git
檢查remote 會push到哪裡
$ git remote -v
github  git@github.com:bear/mp.git (fetch)
github  git@github.com:bear/mp.git (push)
origin  git@xxx.tw:bear/mp2.git (fetch)  # 因為git push預設是git push origin master,所以master會抓自架gitlab的repo
origin  git@xxx.tw:bear/mp2.git (push) # master會push到 gitlab
origin  git@github.com:bear/mp.git (push) # master也會push到 github
最後git push就能同時推到兩個不同的repo去了
$ git push

刷新gitlab和github頁面檢查

要更改預設master pull的repo
修改 .git/config 的
[remote "origin"]
    url = git@xxx.tw:bear/mp2.git #改成你要pull的repo

參考資料:
http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations 採用Malvineous的答案

關於刪除branch
移除gitlab上多餘的branch
$ git push origin --delete alt(多餘的branch名字)
本地的branch要分開刪,沒辦法一起刪
$ git branch -D alt
把本地remote的branch( git branch -a )也刪
# git branch --delete --remotes origin/alt

push新的branch到remote repository
$ git push origin exhibition:exhibition3
這樣遠端倉庫就會新建exhibition3分支,內容是本地端exhibition的內容,另外origin是你remote的名字

將遠端master branch拉到本地端exhibition branch
$ git pull origin master:exhibition
語法:
git pull <remote_name> <remote_branch>:<local_branch>

將本地端exhibition branch推到遠端master branch
$ git push origin exhibition:master
語法:
git push <remote_name> <local_branch>:<remote_branch>