目录

CICD持续集成及持续交付详解

CI/CD持续集成及持续交付详解

CICD持续集成及持续交付

1.CICD是什么

CI/CD 是指持续集成(Continuous Integration)和持续部署(Continuous Deployment)或持续交付(Continuous Delivery)

1.1 持续集成(Continuous Integration)

持续集成是一种软件开发实践,团队成员频繁地将他们的工作集成到共享的代码仓库中。其主要特点包括:

  1. 频繁提交代码:开发人员可以每天多次提交代码,确保代码库始终保持最新状态。
  2. 自动化构建:每次提交后,自动触发构建过程,包括编译、测试、静态分析等。
  3. 快速反馈:如果构建失败或测试不通过,能够快速地向开发人员提供反馈,以便及时修复问题。

1.2 持续部署(Continuous Deployment)

持续部署是在持续集成的基础上,将通过所有测试的代码自动部署到生产环境中。其特点如下:

  1. 自动化流程:从代码提交到生产环境的部署完全自动化,无需人工干预。
  2. 高频率部署:可以实现频繁的部署,使得新功能能够快速地提供给用户。
  3. 风险控制:需要有强大的测试和监控体系来确保部署的稳定性和可靠性。

1.3 持续交付(Continuous Delivery)

持续交付与持续部署类似,但不一定自动部署到生产环境,而是随时可以部署。其重点在于确保软件随时处于可发布状态。

CI/CD 的好处包括:

  1. 提高开发效率:减少手动操作和等待时间,加快开发周期。
  2. 尽早发现问题:通过频繁的集成和测试,问题能够在早期被发现和解决。
  3. 降低风险:减少了大规模部署时可能出现的问题,提高了软件的质量和稳定性。
  4. 增强团队协作:促进团队成员之间的沟通和协作,提高团队的整体效率。

常见的 CI/CD 工具包括 Jenkins、GitLab CI/CD、Travis CI 等。这些工具可以帮助团队实现自动化的构建、测试和部署流程。

2.git工具使用

https://i-blog.csdnimg.cn/img_convert/f7b43ca9748b353d536e640cdb04b18c.png

2.1 git简介

Git 是一个分布式版本控制系统,被广泛用于软件开发中,以管理代码的版本和变更。

2.1.1 主要特点
  • 分布式
    • 每个开发者都有完整的代码仓库副本,这使得开发者可以在离线状态下进行工作,并且在网络出现问题时也不会影响开发。
    • 即使中央服务器出现故障,开发者仍然可以在本地进行开发和查看项目历史。
  • 高效的分支管理
    • Git 中的分支创建和切换非常快速和简单。开发人员可以轻松地创建新的分支来进行新功能的开发或修复 bug,而不会影响主分支。
    • 合并分支也相对容易,可以使用多种合并策略来满足不同的需求。
  • 快速的版本回退
    • 如果发现某个版本存在问题,可以快速回退到之前的版本。
    • 可以查看每个版本的详细变更记录,方便了解代码的演进过程。
  • 强大的提交管理
    • 每个提交都有一个唯一的标识符,可以方便地引用和查看特定的提交。
    • 提交可以包含详细的提交信息,描述本次提交的更改内容。
  • 支持协作开发
    • 开发者可以将自己的更改推送到远程仓库,供其他开发者拉取和合并。
    • 可以处理多个开发者同时对同一文件进行修改的情况,通过合并冲突解决机制来确保代码的完整性。

Git必看秘籍:https://git-scm.com/book/zh/v2

2.2 git 工作流程

https://i-blog.csdnimg.cn/img_convert/b0589ad9b9a3eabae2cc0d993c9878ac.png

Git 有三种状态:已提交(committed)、已修改(modified) 和 已暂存(staged)。

  • 已修改表示修改了文件,但还没保存到数据库中。
  • 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
  • 已提交表示数据已经安全地保存在本地数据库中。

这会让我们的 Git 项目拥有三个阶段:工作区、暂存区以及 Git 目录。

3.部署git

3.1 安装git

# 在rhel9的系统中默认自带git
[root@CICD-node1 ~]# dnf install git  -y

# 设定命令补全功能
[root@CICD-node1 timinglee]# echo "source  /usr/share/bash-completion/completions/git" >> ~/.bashrc
[root@CICD-node1 timinglee]# source  ~/.bashrc

3.2 初始化

获取 Git 仓库通常有两种方式

  • 将尚未进行版本控制的本地目录转换为 Git 仓库。
  • 从其它服务器克隆 一个已存在的 Git 仓库。比如: git clone

初始化版本库

[root@CICD-node1 ~]# mkdir  timinglee
[root@CICD-node1 timinglee]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>

[root@CICD-node1 timinglee]# ls -a
.  ..  .git
[root@CICD-node1 timinglee]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs

# 设定用户信息
[root@CICD-node1 timinglee]# git config --global user.name "timinglee"
[root@CICD-node1 timinglee]# git config --global user.email "timinglee@timinglee.org"

# 查看当前文件状态
[root@CICD-node1 timinglee]# git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

[root@CICD-node1 timinglee]# git status -s		#简化输出

[!WARNING]

.git目录是git跟踪管理版本库的,没事别瞎溜达

4.git的使用方法

https://i-blog.csdnimg.cn/img_convert/5fe549e8ac578b8fc45501711428e0a6.jpeg

4.1 常用方法

[root@CICD-node1 timinglee]# echo timnglee > README.md
[root@CICD-node1 timinglee]# git status
位于分支 master

尚无提交

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)
        README.md

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@CICD-node1 timinglee]# git status -s
?? README.md					# ??	新建文件未添加到版本库

[root@CICD-node1 timinglee]# git add README.md
[root@CICD-node1 timinglee]# git status  -s
A  README.md					# A 已添加到暂存区

# 提交暂存区的数据
[root@CICD-node1 timinglee]# git commit -m "add README.md"
[root@CICD-node1 timinglee]# git status -s		# 无任何显示,标识已经提交到版本库

# 再次修改
[root@CICD-node1 timinglee]# vim README.md
timnglee
timnglee
[root@CICD-node1 timinglee]# git status -s
 M README.md									# 右M 表示文件在工作区被修改

# 撤销修改
[root@CICD-node1 timinglee]# git checkout -- README.md

[root@CICD-node1 timinglee]# cat README.md
timnglee

# 重新修改
[root@CICD-node1 timinglee]# echo timinglee> README.md
[root@CICD-node1 timinglee]# git add README.md
[root@CICD-node1 timinglee]# git status -s
M  README.md									# 左M表示文件已经在版本库中并被跟踪,

# 从暂存区撤销
[root@CICD-node1 timinglee]# git restore --staged README.md
[root@CICD-node1 timinglee]# git status -s
 M README.md

# 重新提交
[root@CICD-node1 timinglee]# git add README.md
[root@CICD-node1 timinglee]# git status -s
M  README.md

# 更新
[root@CICD-node1 timinglee]# git commit -m "update v1"
[master 6a14bb5] update v1
 1 file changed, 1 insertion(+), 1 deletion(-)
 
[root@CICD-node1 timinglee]# git status -s

# 更新文件
[root@CICD-node1 timinglee]# echo timinglee >> README.md
[root@CICD-node1 timinglee]# git add README.md
[root@CICD-node1 timinglee]# echo timinglee >> README.md
[root@CICD-node1 timinglee]# git status -s
MM README.md								# MM表示有一部分在暂存区,还有一部分没有提交

# 如果现在提交只能提交在暂存区中的部分
[root@CICD-node1 timinglee]# git commit -m "update v2"
[master dc9b45f] update v2
 1 file changed, 1 insertion(+)
[root@CICD-node1 timinglee]# git status -s
 M README.md								# 右M还在
 
 
# 查看已暂存和未暂存的修改变化
[root@CICD-node1 timinglee]# echo timinglee >> README.md
[root@CICD-node1 timinglee]# git diff
diff --git a/README.md b/README.md
index 62be538..f08851c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,4 @@
 timinglee
 timinglee
+timinglee
+timinglee

# 跳过使用暂存区,只能在提交过的在版本库中存在的文件使用如果文件状态是“??”不能用此方法
[root@CICD-node1 timinglee]# git commit -a -m "update v3"

# 撤销工作区中删除动作
[root@CICD-node1 timinglee]# touch lee.txt
[root@CICD-node1 timinglee]# git add lee.txt
[root@CICD-node1 timinglee]# git commit  -m "add lee.txt"

# 从版本库中删除文件
[root@CICD-node1 timinglee]# git rm lee.txt
rm 'lee.txt' 
[root@CICD-node1 timinglee]# git status -s
D  lee.txt								# 左D表示文件删除动作被提交到暂存区

[root@CICD-node1 timinglee]# git status -s
 D lee.txt								# 右D表示文件在工作区被删除
[root@CICD-node1 timinglee]# git checkout -- lee.txt
[root@CICD-node1 timinglee]# ls
dir1  lee.txt  README.md


[root@CICD-node1 timinglee]# git commit -m "delete lee.txt"
[root@CICD-node1 timinglee]# git status -s

# 恢复从版本库中被删除的文件
[root@CICD-node1 timinglee]# git log			# 查看操作日志

[root@gitlab timinglee]# git reflog				# 查看提交动作
25a4fec (HEAD -> master) HEAD@{0}: commit: delete lee.txt
be6ec2a HEAD@{1}: commit: add lee.txt			# 下面会用到到

# 版本回退到删除之前

[root@gitlab timinglee]# git reset --hard be6ec2a
HEAD is now at be6ec2a add lee.txt
[root@gitlab timinglee]# ls
lee.txt  README.md

4.2 git对于文件如何忽略

在做软件开发时对源码编译会产生一些临时文件,我们在提交时需要忽略这些临时文件

[root@CICD-node1 timinglee]# mkdir dir1/
[root@CICD-node1 timinglee]# touch dir1/.file2
[root@CICD-node1 timinglee]# git status -s
?? .file1
?? dir1/


[root@CICD-node1 timinglee]# echo .file1 > .gitignore
[root@CICD-node1 timinglee]# git status -s
?? .gitignore
?? dir1/
[root@CICD-node1 timinglee]# echo ".*" > .gitignore
[root@CICD-node1 timinglee]# git status -s

5.gitlab代码仓库

5.1 gitlab简介

https://i-blog.csdnimg.cn/img_convert/7506466b94169db0bdc0ce6d8d1023f0.png

  • GitLab 是一个用于仓库管理系统的开源项目,使用 Git 作为代码管理工具,并在此基础上搭建起来的 web 服务。
  • GitLab 具有很多功能,比如代码托管、持续集成和持续部署(CI/CD)、问题跟踪、合并请求管理等。它可以帮助开发团队更好地协作开发软件项目,提高开发效率和代码质量。

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

中文站点:

官方包地址:https://packages.gitlab.com/gitlab/gitlab-ce

5.2 gitlab 的部署实施

5.2.1 部署gitlab

部署gitlab需要内存大于4G

# 在安装包之前需配置好软件仓库来解决依赖性
[root@CICD-node1 ~]# yum install -y curl policycoreutils-python-utils  openssh-server perl

[root@CICD-node1 ~]# dnf install gitlab-ce-17.1.6-ce.0.el9.x86_64.rpm -y

https://i-blog.csdnimg.cn/img_convert/fb3f60701333425d3d299f8d1c07f370.png