Jenkins安装及配合GitLab代码自动部署

**html

git—仓库搭建及使用

**ios

###################################################################
建立用户
[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版本库里添加的时候,是分两步执行的:
  • 第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;github

  • 第二步是用git commit提交更改,实际上就是把暂存区的全部内容提交到当前分支。web

  • 由于咱们建立Git版本库时,Git自动为咱们建立了惟一一个master分支,因此,如今,git commit就是往master分支上提交更改。
    因此说明须要提交的文件修改统统放到暂存区,而后,一次性提交暂存区的全部修改docker

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 
对于提交后的代码 只能经过版本回退来恢复了

**vim

github

**浏览器

上传本地代码
[root@foundation15 demo]# git remote add origin git@github.com: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 git@github.com:802119323/bobo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

这里写图片描述

代码的下载
下载github上的代码
[root@foundation15 github]# git clone git@github.com: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

**ruby

Gitlab的安装及使用

**ssh

使用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 git@172.25.15.250: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 分别为测试时建立的文件与自行添加的文件