Jenkins安裝及配合GitLab代碼自動部署

**

git—倉庫搭建及使用

**

###################################################################
創建用戶
[kiosk@foundation6 ~]$ git config --global user.name "bobo"
[kiosk@foundation6 ~]$ git config --global user.email  1272425809@qq.com
查看用戶信息
[root@foundation15 ~]# cat .gitconfig 
[user]
    name = bobo
    email = 1272425809@qq.com

搭建倉庫
[root@foundation15 mnt]# mkdir  bobo
[root@foundation15 mnt]# cd  bobo
[root@foundation15 bobo]# ls
[root@foundation15 bobo]# git init
Initialized empty Git repository in /mnt/bobo/.git/
##################################################################
倉庫存放代碼
[root@foundation15 bobo]# vim   bobo.txt
[root@foundation15 bobo]# git add bobo.txt 
[root@foundation15 bobo]# git status 
# On branch master
# Initial commit
# Changesto be committed:
#   (use "git rm --cached <file>..." to unstage)
#   new file:   bobo.txt(顯示新的文件已經添加)

commit來提交
[root@foundation15 bobo]# git commit -m "demo"
[master (root-commit) 69d20ec] demo
 1 file changed, 2 insertions(+)
 create mode 100644 bobo.txt

##################################################################333
代碼修改
[root@foundation15 bobo]# vim bobo.txt 
[root@foundation15 bobo]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   bobo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

這裏顯示代碼被修改但還未被提交
######################################################################
查看改動
[root@foundation15 bobo]# git diff
diff --git a/bobo.txt b/bobo.txt
index 9a80a22..477e165 100644
--- a/bobo.txt
+++ b/bobo.txt
@@ -1,2 +1,2 @@
-hello
+wetos

對修改後的文件提交
[root@foundation15 bobo]# git add *
[root@foundation15 bobo]# git commit -m "hello"
[master 282b01d] hello
 1 file changed, 1 insertion(+), 1 deletion(-)
#####################################################

版本的回退
[root@foundation15 bobo]# git log
commit 282b01d3a25a0a5a0343019dab8034322e0e89e3
Author: bobo <1272425809@qq.com>
Date:   Thu Aug 23 14:42:06 2018 +0800

    hello

commit 69d20ecc3a43c67b5e8641f6abf46653c644055a
Author: bobo <1272425809@qq.com>
Date:   Thu Aug 23 14:35:31 2018 +0800

    demo
#######################################################################
git reset 回退已經提交的倉庫
HEAD 表示當前版本
HEAD^ 表示上一個版本
HEAD^^ 表示上上一個版本
HEAD~100 表示當前往上100個版本 
######################################################################

[root@foundation15 bobo]# git reset --hard HEAD^
HEAD is now at 69d20ec demo
查看代碼已經改變
[root@foundation15 bobo]# cat  bobo.txt 
hello

如果版本回退後需要返回回退前  指定回退版本號就可以了
[root@foundation15 bobo]# git reset --hard 282b01d3a25a0a5a0343019dab8034322e0e89e3
HEAD is now at 282b01d hello
###############################################################
對於所有的出現過的版本號 可以通過  relog查看
[root@foundation15 bobo]# git reflog
282b01d HEAD@{0}: reset: moving to 282b01d3a25a0a5a0343019dab8034322e0e89e3
69d20ec HEAD@{1}: reset: moving to HEAD^
282b01d HEAD@{2}: commit: hello
69d20ec HEAD@{3}: commit (initial): demo


工作區和贊存區

  • Git版本庫裏添加的時候,是分兩步執行的:
  • 第一步是用git add把文件添加進去,實際上就是把文件修改添加到暫存區;

  • 第二步是用git commit提交更改,實際上就是把暫存區的所有內容提交到當前分支。

  • 因爲我們創建Git版本庫時,Git自動爲我們創建了唯一一個master分支,所以,現在,git commit就是往master分支上提交更改。
    所以說明需要提交的文件修改通通放到暫存區,然後,一次性提交暫存區的所有修改

1
    對於還沒有add的代碼  修改後撤回的操作爲
[root@foundation15 bobo]# > bobo.txt 
[root@foundation15 bobo]# cat  bobo.txt 
[root@foundation15 bobo]# git  checkout  bobo.txt 
[root@foundation15 bobo]# cat  bobo.txt 
wetos

2
對於修改後還沒有commit的代碼撤回
[root@foundation15 bobo]# > bobo.txt 
[root@foundation15 bobo]# cat  bobo.txt 
[root@foundation15 bobo]# git add *
[root@foundation15 bobo]# cat bobo.txt 
[root@foundation15 bobo]# git reset HEAD  bobo.txt (將文件退回工作區)
Unstaged changes after reset:
M   bobo.txt
[root@foundation15 bobo]# git checkout  bobo.txt 
[root@foundation15 bobo]# cat bobo.txt 
wetos

3 
對於提交後的代碼 只能通過版本回退來恢復了


**

github

**

上傳本地代碼
[root@foundation15 demo]# git remote add origin [email protected]:802119323/bobo.git

[root@foundation15 demo]# git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:802119323/bobo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

這裏寫圖片描述

代碼的下載
下載github上的代碼
[root@foundation15 github]# git clone   [email protected]:802119323/bobo.git
Cloning into 'bobo'...
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
[root@foundation15 github]# ls
bobo
[root@foundation15 github]# cd  bobo
[root@foundation15 bobo]# ls
test2  test.txt

**

Gitlab的安裝及使用

**

使用rpm包方式安裝

[root@foundation15 ~]# yum install  gitlab-ce-11.0.1-ce.0.el6.x86_64.rpm 
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Examining gitlab-ce-11.0.1-ce.0.el6.x86_64.rpm: gitlab-ce-11.0.1-ce.0.el6.x86_64
Marking gitlab-ce-11.0.1-ce.0.el6.x86_64.rpm to be installed

#############################################################
初始化配置 
修改配置文件
[root@foundation15 yum.repos.d]# vim  /etc/gitlab/gitlab.rb 
external_url 'http://172.25.15.250'


開始初始化gitlab
[root@foundation15 yum.repos.d]# gitlab-ctl reconfigure
等待初始化

啓動 再瀏覽器進行ssh等配置

這裏寫圖片描述
這裏寫圖片描述

物理機ssh-keygen創建 id_rsa.pub

這裏寫圖片描述

這裏寫圖片描述

ssh配置完成

測試 創建一個項目用於本地測試拉取

創建一個bobo組 再創建一個test項目 再寫東西進去就可以了
這裏寫圖片描述

這裏寫圖片描述

測試使用ssh免密拉取項目
這裏寫圖片描述

[root@foundation15 mnt]# git clone [email protected]:bobo/test1.git
Cloning into 'test1'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

查看拉取內容
[root@foundation15 test1]# cat test1 
bobo

與網頁創建的一致
倉庫搭建完成
這裏寫圖片描述

git 代碼上傳

創建一個index.html

[root@foundation15 test1]# git add *
[root@foundation15 test1]# git commit -m "add index.html"
[master af7e6dc] add index.html
 1 file changed, 1 insertion(+)
 create mode 100644 index.html
[root@foundation15 test1]# ls
index.html  test1
#####################################################################3
git  push就可以提交代碼
[root@foundation15 test1]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 275 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.25.15.250:bobo/test1.git
   222c56a..af7e6dc  master -> master
#########################################################

瀏覽器查看是否添加成功
成功

這裏寫圖片描述

使用完成後 gitlab-ctl stop 停止服務即可

**

Jenkins的搭建與使用

**

  1. 基於JAVA的開源的自動化系統平臺

  2. 加速自動化CI,CD任務及流水線,所有類型的任務:構建,測試,部署等

  3. 豐富的插件生態系統支持功能擴展,1400+插件和SCM,測試,通知,報

告,Artfact,觸發,外部集成等,基於Web的管理和使用界面

安裝使用
本次使用docker來搭建

拉取鏡像
[root@foundation15 ~]# docker pull jenkins:latest
latest: Pulling from library/jenkins
~~~~~~~~
Digest: sha256:eeb4850eb65f2d92500e421b430ed1ec58a7ac909e91f518926e02473904f668
Status: Downloaded newer image for jenkins:latest
拉取後可以保存下來  主機更換後不用再下載

[root@foundation15 mnt]# docker save -o  jenkins.tar   jenkins

[root@foundation15 mnt]# ll jenkins.tar 
-rw------- 1 root root 714778112 Aug 23 09:16 jenkins.tar


開啓服務
需要注意  再一臺物理機運行 gitlab與jenkins時 他們的端口都是相同的  所以這裏將 8888作爲jenkins的使用端口
[root@foundation15 mnt]# docker run -it --name jenkins1 -v $HOME/jenkins:/var/  -p 8888:8080 -p 55000:50000 -p 45000:45000 jenkins:latest 
Running from: /usr/share/jenkins/jenkins.war

再瀏覽器中打開
http://172.25.15.250:8888 設置密碼登陸即可

當出現unlock時
這裏寫圖片描述

根據提示在對應目錄下找到解鎖碼輸入即可
我在docker中搭建的服務 所以進入docker查看即可
這裏寫圖片描述

解鎖後設置密碼即可


搭建項目來鏈接gitlab

這裏寫圖片描述

創建新的項目來接受gitli的信息
這裏寫圖片描述

這裏寫圖片描述

選擇gitlib
這裏寫圖片描述
添加認證 輸入gitlab的用戶密碼
這裏寫圖片描述

再添加選擇gitlab的http與版本即可即可
這裏寫圖片描述

[root@foundation15 ~]# rpm -qa|grep gitlab
gitlab-ce-11.0.1-ce.0.el6.x86_64

選擇構建的操作 這裏選擇輸出代碼文件
選擇構建的刷新時間
這裏寫圖片描述
這裏寫圖片描述

保存退出

開始構建
這裏寫圖片描述
顯示控制檯即可
這裏寫圖片描述

這裏寫圖片描述
基本的搭建完成

開始測試 本地主機上傳新代碼

[root@foundation15 test1]# git add *
[root@foundation15 test1]# git commit -m "add  bobo.txt"
[master 9df3cb8] add  bobo.txt
 1 file changed, 1 insertion(+)
 create mode 100644 bobo.txt
[root@foundation15 test1]# git  push  origin  master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 275 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.25.15.250:test/test1.git
   0c28b4d..9df3cb8  master -> master

等待代碼構建添加即可
這裏寫圖片描述這裏寫圖片描述

搭建成功

實時觸發器
下載gitlibhook插件即可

這裏寫圖片描述

在配置時打開高級選項 生成key 將網址 一併複製在gitlab上

這裏寫圖片描述

出現無法訪問本地時
在useradmin settting中 找到最下方的設置打開即可
這裏寫圖片描述

測試是否添加成功
這裏寫圖片描述
返回值200 成功
添加新代碼文件測試是否ok

[root@foundation15 test1]# vim   hello.txt
[root@foundation15 test1]# git add *
[root@foundation15 test1]# git commit  -m "add"
[master c06e5c1] add
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
[root@foundation15 test1]# git push   origin  master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.25.15.250:test/test1.git
   9df3cb8..c06e5c1  master -> master

瀏覽器看到立即構建了新文件
這裏寫圖片描述

3 與4 分別爲測試時創建的文件與自行添加的文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章