Continuous Integration and Continuous Delivery---git

1. Use of git tools

1.1 Introduction to git

  • Git features:
    speed,
    simple design
    , strong support for non-linear development mode (allowing thousands of parallel development branches),
    fully distributed
    , capable of efficiently managing super-large-scale projects like the Linux kernel (speed and data volume)

  • Since its birth in 2005, Git has become more mature and perfect. While it is highly easy to use, it still retains its initial goals. It is fast, extremely suitable for managing large projects, and has an incredible non-linear branch management system.

Git must-see cheats

  • Git has three states: committed, modified, and staged.
    1. Modified means that the file has been modified, but it has not been saved in the database.
    2. Staging means to mark the current version of a modified file so that it will be included in the next snapshot.
    3. Submitted means that the data has been safely stored in the local database.

  • This will make our Git project have three stages: workspace, staging area, and Git directory.

1.2 git installation

- 安装Git:
	# yum install -y git
	
	获取 Git 仓库通常有两种方式:
		将尚未进行版本控制的本地目录转换为 Git 仓库。
		从其它服务器克隆 一个已存在的 Git 仓库。比如: git clone 
	
- 初始化版本库:
	$ mkdir demo
	$ git init
		Initialized empty Git repository in /home/git/demo/.git/ 
	$ ls .git/
		branches  config  description  HEAD  hooks  info  objects  refs
	
		.git目录是git跟踪管理版本库的,没事别瞎溜达!

- 用户信息
	$ git config --global user.name "wxh"
	$ git config --global user.email [email protected]

- 检查当前文件状态
	$ git status
	$ git status -s		//简化输出

- 状态简览
	$ git status -s
	 M README
	MM Rakefile
	A  lib/git.rb
	M  lib/simplegit.rb
	?? LICENSE.txt
- 用户信息
	$ git config --global user.name "wxh"
	$ git config --global user.email [email protected]

- 检查当前文件状态
	$ git status
	$ git status -s		//简化输出

- 状态简览
	$ git status -s
	 M README
	MM Rakefile
	A  lib/git.rb
	M  lib/simplegit.rb
	?? LICENSE.txt

- 跟踪新文件
	$ git add README

- 忽略文件
	$ cat .gitignore
		.*		//忽略所有隐藏文件
		/test		//只忽略当前目录下的test文件
		build/		//忽略任何目录下名为 build 的文件夹

- 查看已暂存和未暂存的修改
	$ git diff

- 提交更新
	$ git commit

- 跳过使用暂存区域
	$ git commit -a -m 'added new benchmarks'

- 移除文件
	$ git rm PROJECTS.md
	$ git rm --cached README
	
- 重命名文件
	$ git mv README.md README
	其实,运行 git mv 就相当于运行了下面三条命令:
		$ mv README.md README
		$ git rm README.md
		$ git add README
	
- 查看提交历史
	$ git log
	$ git log -p -2
	$ git log --stat
	$ git log --pretty=oneline

- 取消暂存的文件
	$ git reset HEAD README.md

- 撤消对文件的修改
	$ git checkout -- README.md

- 版本回退:
	$ git reflog
	$ git reset --hard efa267a

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

2. Use git with github

- 远程仓库:注册github帐号,并新建一个仓库:

- 推送本地仓库内容到github:
	$ git remote add origin https://github.com/westos007/git.git								//关联远程仓库
	$ git push -u origin master		//第一次推送需要加 -u参数

- 使用https方式推送每次需要输入用户名和密码,如果不想麻烦的话采用ssh方式:
	 $ ssh-keygen -t rsa -b 4096 -C "[email protected]"
	生成本地密钥,并上传公钥到github:

- $ git remote -v   ##关联详情
	origin	https://github.com/westos007/git.git (fetch)
	origin	https://github.com/westos007/git.git (push)

- $ git remote rm origin

- $ git remote add origin [email protected]:westos007/git.git

- $ git remote -v 
	origin	[email protected]:westos007/git.git (fetch)
	origin	[email protected]:westos007/git.git (push)
	
- $ git push origin master


- 克隆远程仓库:$ git clone [email protected]:westos007/gittest.git
[root@server1 ~]# ssh-keygen
[root@server1 ~]# cat .ssh/id_rsa.pub 

Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

3. gitlab code repository

3.1 gitlab installation

- 官网:https://about.gitlab.com/install/

- 软件下载(官方下载慢,推荐使用国内镜像站点)
	https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

- 软件安装: (官方推荐至少4G内存)
	# yum install -y curl policycoreutils-python openssh-server
	
	# yum install -y gitlab-ce-12.8.5-ce.0.el7.x86_64.rpm
	
	# vim /etc/gitlab/gitlab.rb		
		external_url 'http://172.25.13.1'		//访问gitlab的地址
	
	# gitlab-ctl  reconfigure			//重载服务
	
- 登录gitlab: 
	http://172.25.13.1 		//用户:root 第一次登录需要强制修改密码
[root@server1 ~]# ll gitlab-ce-13.2.2-ce.0.el7.x86_64.rpm 
-rw-r--r-- 1 root root 757395226 Aug  1  2020 gitlab-ce-13.2.2-ce.0.el7.x86_64.rpm
[root@server1 ~]# yum install gitlab-ce-13.2.2-ce.0.el7.x86_64.rpm -y 
[root@server1 ~]# vim /etc/gitlab/gitlab.rb	
[root@server1 ~]# gitlab-ctl  reconfigure

Insert picture description here

Insert picture description here
Insert picture description here

3.2 Common commands

- 	gitlab-ctl start    		# 启动所有 gitlab 组件
	gitlab-ctl stop        		# 停止所有 gitlab 组件		
	gitlab-ctl restart       	 	# 重启所有 gitlab 组件		
	gitlab-ctl status        		# 查看服务状态
	gitlab-ctl reconfigure     	# 重载服务
	gitlab-ctl tail        		# 查看日志

3.3 Use

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

4. jenkins continuous integration

4.1 Introduction

  • Jenkins is the leader of open source CI&CD software, providing more than 1,000 plug-ins to support construction, deployment, and automation to meet the needs of any project.

  • Jenkins is written in Java language and can be run in popular servlet containers such as Tomcat or independently.

  • CI (Continuous integration) continuous integration emphasizes that after developers submit new code, they immediately carry out construction and (unit) testing.

  • CD (Continuous Delivery) is based on continuous integration to deploy the integrated code to a closer real operating environment (production-like environment).

4.2 Installation

Software download
domestic mirror station

#安装jenkins:
[root@server2 ~]# ll
total 312456
-rw-r--r-- 1 root root 175262413 May 31  2018 jdk-8u171-linux-x64.rpm
-rw-r--r-- 1 root root  73008148 Mar 12 02:03 jenkins-2.279-1.1.noarch.rpm
-rw-r--r-- 1 root root  71679711 Mar 12 01:52 jenkins-2.283-1.1.noarch.rpm
[root@server2 ~]# rpm -ivh jdk-8u171-linux-x64.rpm
[root@server2 ~]# yum install jenkins-2.283-1.1.noarch.rpm -y 
[root@server2 ~]# systemctl start jenkins
[root@server2 ~]# systemctl status jenkins.service 




Insert picture description here
Insert picture description here

4.3 Access


[root@server2 ~]# netstat -antlp    ##查看8080端口

#更新插件源:
# vim  /var/lib/jenkins/hudson.model.UpdateCenter.xml 
<?xml version='1.1' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
  </site>


[root@server2 ~]# cd  /var/lib/jenkins/
[root@server2 jenkins]# ls
config.xml                        logs              secret.key.not-so-secret
hudson.model.UpdateCenter.xml     nodeMonitors.xml  secrets
identity.key.enc                  nodes             updates
jenkins.telemetry.Correlator.xml  plugins           userContent
jobs                              secret.key        users
[root@server2 jenkins]# vim hudson.model.UpdateCenter.xml  ##可以写成清华源,这是为了下载update中文件中的插件,这里也可以提前将文件修改好,然后放到http的默认发布目录,这里的清华源改成http://172.25.13.2/default.json
<?xml version='1.1' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
  </site>
[root@server2 jenkins]# cd /var/lib/jenkins/updates/   ##里面会有默认的default.json文件,下面是修改过程
[root@server2 updates]# sed -i.bak 's/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json
[root@server2 updates]# sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json   ##更新之后不要重启服务,不然会被覆盖
[root@server2 updates]# ls
default.json  default.json.bak
[root@server2 updates]# vim default.json  ##cp default.json /var/www/html/  一定要打开apache服务
[root@server2 ~]# systemctl restart jenkins.service   ##,如果使用的不是http,不能重启,不然文件会自动改成网络上模式。修改完不需要重启服务,改成http方式,重启也没事
[root@server2 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword  ##获取密钥


#访问: http://172.25.13.2:8080
#使用初始密码登录:cat /var/lib/jenkins/secrets/initialAdminPassword
#安装默认插件即可,使用admin用户,登录后修改密码。

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

5. Get through gitlab and jenkins

5.1 Create user

Insert picture description here
Insert picture description here

5.2 Update the contents of the warehouse

Insert picture description here
Insert picture description here
Insert picture description here

5.3 jenkins operation

5.3.1 Starting jenkins manually

[root@server2 ~]# yum install git -y  

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5.3.2 Automatically trigger jenkins through gitlab

##这里必须添加gitlab插件

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qwerty1372431588/article/details/114691261