目录
一、Git工具
二、git安装
三、git使用
三、gitlab代码仓库
四、jenkins持续集成
五、Jenkins自动构建docker镜像,并上传至harbor仓库
六、Jenkins连接docker构建主机
七、jenkins结合ansible
git简介
1).Git特点:
• 速度
• 简单的设计
• 对非线性开发模式的强力支持(允许成千上万个并行开发的分支)
• 完全分布式
• 有能力高效管理类似 Linux 内核一样的超大规模项目(速度和数据量)
• 自诞生于 2005 年以来,Git 日臻成熟完善,在高度易用的同时,仍然保留着初期设定的目标。 它的速度飞快,极其适合管理大项目,有着令人难以置信的非线性分支管理系统。
2).Git必看秘籍:Git - Book
3).Git 有三种状态:已提交(committed)、已修改(modified) 和 已暂存(staged)。
• 已修改表示修改了文件,但还没保存到数据库中。
• 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
• 已提交表示数据已经安全地保存在本地数据库中。
• 这会让我们的 Git 项目拥有三个阶段:工作区、暂存区以及 Git 目录。
1、安装git
[root@node11 ~]# yum install -y git
2、获取git仓库
[root@node11 ~]# mkdir demo
[root@node11 ~]# cd demo/
3、初始化版本库
[root@node11 demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@node11 demo]# l.
. .. .git
1、本地下的使用
[root@node11 demo]# touch readme.txt
[root@node11 demo]# ls
readme.txt
[root@node11 demo]# git status 查看状态的详细信息
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@node11 demo]# git status -s 查看状态的简略信息
?? readme.txt ??表示新建立未添加到版本库
[root@node11 demo]# git add readme.txt 添加这个文件到版本库
[root@node11 demo]# git status -s 查看状态
A readme.txt A表示该文件已经添加到版本库的暂存区
[root@node11 demo]# git add . 添加这个目录下所有文件到版本库
[root@node11 demo]# git commit -m "add readme.txt" 提交
[master (root-commit) b362cbb] add readme.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
[root@node11 demo]# git status -s
2、设置用户信息
[root@node11 demo]# git config --global user.email "[email protected]"
[root@node11 demo]# git config --global user.name "zcx"
3、各种状态
[root@node11 demo]# touch test.txt
[root@node11 demo]# git status -s
?? test.txt
[root@node11 demo]# git add test.txt 添加到暂存区
[root@node11 demo]# git status -s
A test.txt
[root@node11 demo]# echo 123 > test.txt 输入124
[root@node11 demo]# echo 456 > readme.txt
[root@node11 demo]# git status -s
M readme.txt
AM test.txt
[root@node11 demo]# git add readme.txt 添加到暂存区
[root@node11 demo]# git status -s
M readme.txt M在左边表示修改的文件已经提交在暂存区
AM test.txt M在右边表示修改的时候在本地目录下修改的
[root@node11 demo]# echo wstos >> test.txt
[root@node11 demo]# git status -s
M test.txt
[root@node11 demo]# git add test.txt
[root@node11 demo]# git status -s
M test.txt
[root@node11 demo]# echo wstos1 >> test.txt
[root@node11 demo]# git status -s
MM test.txt 右边M 表示在当前工作目录中修改,未add到暂存区
左边M 表示已经提交到暂存区
4、忽略文件
[root@node11 demo]# touch .a
[root@node11 demo]# touch .b
[root@node11 demo]# git status -s
?? .a
?? .b
[root@node11 demo]# mkdir .dir
[root@node11 demo]# cd .dir/
[root@node11 .dir]# touch file1
[root@node11 .dir]# cd ..
[root@node11 demo]# git status -s
?? .a
?? .b
?? .dir/
[root@node11 demo]# vim .gitignore
[root@node11 demo]# cat .gitignore 忽略所有以.开头的文件
.*
5、查看已暂存和未暂存的修改
[root@node11 demo]# git diff
6、跳过使用暂存区
[root@node11 demo]# echo 123 >> readme.txt
[root@node11 demo]# git commit -a -m "v1"
[master d71fb72] v1
1 file changed, 1 insertion(+)必须时已经add过的,两个??的不行
7、删除文件
[root@node11 demo]# rm -rf test.txt 删除本地工作目录里的
[root@node11 demo]# git status -s
D test.txt
[root@node11 demo]# git checkout -- test.txt 撤销删除
[root@node11 demo]# ls
readme.txt test.txt
[root@node11 demo]# git rm test.txt
rm 'test.txt'
[root@node11 demo]# git status -s 已经在暂存分区了
D test.txt
[root@node11 demo]# git commit -m "delet test.txt" 提交到版本
[master 5a4415c] delet test.txt
1 file changed, 3 deletions(-)
delete mode 100644 test.txt
[root@node11 demo]# git status -s
8、查看提交历史
[root@node11 demo]# git reflog
5a4415c HEAD@{0}: commit: delet test.txt
d71fb72 HEAD@{1}: commit: v1
9395aa6 HEAD@{2}: commit: add files
ccd4227 HEAD@{3}: commit: add files
6fd3944 HEAD@{4}: commit: add files
26392d2 HEAD@{5}: commit: add files
b362cbb HEAD@{6}: commit (initial): add readme.txt
9、版本回退
[root@node11 demo]# git reset --hard d71fb72 回退到v1版本
HEAD is now at d71fb72 v1
[root@node11 demo]# ls
readme.txt test.txt
10、取消暂存文件
[root@node11 demo]# git rm readme.txt
rm 'readme.txt'
[root@node11 demo]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# deleted: readme.txt
#
[root@node11 demo]# git reset HEAD readme.txt
Unstaged changes after reset:
D readme.txt
[root@node11 demo]# git checkout -- readme.txt 撤销对文件的修改
[root@node11 demo]# ls
readme.txt
11、创建远程仓库
注册github帐号,并新建一个仓库:
[root@node11 demo]# git remote -v 查看仓库信息
[root@node11 demo]# git remote remove origin 删除仓库
[root@node11 demo]# git remote add origin [email protected]:zcx0216/westos.git 创建
[root@node11 demo]# git remote -v
origin [email protected]:zcx0216/westos.git (fetch)
origin [email protected]:zcx0216/westos.git (push)
使用ssh免密登陆
[root@node11 ~]# ssh-keygen 生成密钥
[root@node11 ~]# cd .ssh/
[root@node11 .ssh]# cat id_rsa.pub 查看密钥
[root@node11 demo]# git push -u origin "master"免密登陆
[root@node11 ~]# rm -rf demo/
[root@node11 ~]# git clone [email protected]:zcx0216/westos.git删除后可以克隆回来
Cloning into 'westos'...
Warning: Permanently added the ECDSA host key for IP address '212.64.63.190' to the list of known hosts.
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 18 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (18/18), done.
[root@node11 ~]# ls
anaconda-ks.cfg westos
1、安装gitlab
官网:https://about.gitlab.com/install/
• 软件安装: (官方推荐至少4G内存)
[root@node11 ~]# yum install -y gitlab-ce-15.3.3-ce.0.el7.x86_64.rpm
[root@node11 ~]# yum install -y curl policycoreutils-python openssh-server[root@node11 ~]# vim /etc/gitlab/gitlab.rb 访问gitlab的地址
2、登录gitlab:
• http://192.168.0.11 //用户:root 第一次登录需要强制修改密码
3、常用命令:
gitlab-ctl start 启动所有 gitlab 组件
gitlab-ctl stop 停止所有 gitlab 组件
gitlab-ctl restart 重启所有 gitlab 组件
[root@node11 gitlab]# gitlab-ctl status 查看服务状态
run: alertmanager: (pid 21314) 65s; run: log: (pid 21080) 107s
run: gitaly: (pid 21300) 67s; run: log: (pid 20562) 224s
run: gitlab-exporter: (pid 21273) 70s; run: log: (pid 21026) 125s
run: gitlab-kas: (pid 21255) 72s; run: log: (pid 20821) 207s
run: gitlab-workhorse: (pid 21262) 71s; run: log: (pid 20936) 148s
run: logrotate: (pid 20495) 240s; run: log: (pid 20522) 237s
run: nginx: (pid 20958) 145s; run: log: (pid 20967) 142s
run: node-exporter: (pid 21269) 71s; run: log: (pid 21004) 131s
run: postgres-exporter: (pid 21321) 64s; run: log: (pid 21098) 101s
run: postgresql: (pid 20607) 217s; run: log: (pid 20695) 213s
run: prometheus: (pid 21280) 69s; run: log: (pid 21055) 115s
run: puma: (pid 20880) 163s; run: log: (pid 20887) 162s
run: redis: (pid 20525) 234s; run: log: (pid 20543) 231s
run: redis-exporter: (pid 21275) 69s; run: log: (pid 21039) 121s
run: sidekiq: (pid 20897) 157s; run: log: (pid 20905) 156s
gitlab-ctl reconfigure 重载服务
gitlab-ctl tail 查看日志
4、新建项目
[root@node11 ~]# git clone [email protected]:root/demo.git 克隆并登陆
Cloning into 'demo'...
The authenticity of host '192.168.0.11 (192.168.0.11)' can't be established.
ECDSA key fingerprint is SHA256:+PU4HVx+48YgaGzC57QVNbsQ6tDzDCuh8QtMZIJ+9jg.
ECDSA key fingerprint is MD5:2a:6d:a1:5b:f7:1b:96:26:3d:02:f6:59:4f:e2:bc:2e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.11' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@node11 ~]# cd demo/
[root@node11 demo]# ls
README.md
[root@node11 demo]# git remote -v
origin [email protected]:root/demo.git (fetch)
origin [email protected]:root/demo.git (push)
[root@node11 demo]# echo www.westos.org > index.html
[root@node11 demo]# git add index.html 添加到暂存区
[root@node11 demo]# git commit -m "add index.html" 提交到版本库
[main 0cc139b] add index.html
1 file changed, 1 insertion(+)
create mode 100644 index.html
[root@node11 demo]# git push -uf origin main 上传
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:root/demo.git
0c5ff26..0cc139b main -> main
Branch main set up to track remote branch main from origin.
1、jenkins简介
• Jenkins是开源CI&CD软件领导者, 提供超过1000个插件来支持构建、部署、自
动化, 满足任何项目的需要。
• Jenkins用Java语言编写,可在Tomcat等流行的servlet容器中运行,也可独立运行。
• CI(Continuous integration持续集成)持续集成强调开发人员提交了新代码之后,
立刻进行构建、(单元)测试。
• CD(Continuous Delivery持续交付) 是在持续集成的基础上,将集成后的代码部署
到更贴近真实运行环境(类生产环境)中。
2、安装
• 国内镜像站:https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/
• 安装jenkins:
[root@node22 ~]# yum install -y jdk-11.0.15_linux-x64_bin.rpm 装java
[root@node22 ~]# yum install -y fontconfig 解决依耐性
[root@node22 ~]# yum install -y jenkins-2.367-1.1.noarch.rpm
[root@node22 ~]# systemctl enable --now jenkinsCreated symlink from /etc/systemd/system/multi-user.target.wants/jenkins.service to /usr/lib/systemd/system/jenkins.service.
[root@node22 ~]# cd /var/lib/jenkins/updates/
[root@node22 updates]# ls
default.json
• 访问: http://192.168.0.22:8080• 使用初始密码登录:cat /var/lib/jenkins/secrets/initialAdminPassword
[root@node22 updates]# cat /var/lib/jenkins/secrets/initialAdminPassword
7ce22b25316b453dbf2ed2169c4f5f2a
• 安装默认插件即可,使用admin用户,登录后修改密码。
3.加速方法
[root@node22 updates]# curl -sSL https://cdn.jsdelivr.net/gh/lework/jenkins-update-center/speed-test.sh | bash 哪个快选哪个
https://cdn.jsdelivr.net/gh/lework/jenkins-update-center/updates/huawei/update-center.json
[root@node22 jenkins]# cat hudson.model.UpdateCenter.xml 这就是上边修改的地方
default
https://cdn.jsdelivr.net/gh/lework/jenkins-update-center/updates/huawei/update-center.json
[root@node22 jenkins]# mkdir /var/lib/jenkins/update-center-rootCAs
[root@node22 jenkins]# wget https://cdn.jsdelivr.net/gh/lework/jenkins-update-
center/rootCA/update-center.crt -O /var/lib/jenkins/update-center-rootCAs/update-center.crt
4、jenkins使用
定义轮询间隔,有更新时触发jinkens构建
构建选择执行shell命令,构建选择执行shell命令Git提交代码到gitlab,jinkens轮询检测到代码仓库的变更,触发构建
控制台输出
gitlab自动触发jenkins(添加gitlab插件
[root@node11 harbor]# scp /etc/docker/daemon.json node22:/etc/docker
[root@node11 ~]# cd /etc/yum.repos.d/
[root@node11 yum.repos.d]# ls
CentOS-Base.repo docker-ce.repo docker.conf redhat.repo westos.repo
[root@node11 yum.repos.d]# scp docker-ce.repo node22:/etc/yum.repos.d/
[root@node11 yum.repos.d]# scp CentOS-Base.repo node22:/etc/yum.repos.d/
[root@node11 yum.repos.d]# cd /etc/docker/
[root@node11 docker]# scp -r certs.d/ node22:/etc/docker
[root@node22 ~]# cd /etc/yum.repos.d/
[root@node22 yum.repos.d]# ls
docker-ce.repo redhat.repo westos.repo
[root@node22 yum.repos.d]# yum install -y docker-ce
[root@node22 yum.repos.d]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@node22 yum.repos.d]# docker info
[root@node22 yum.repos.d]# vim /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
[root@node22 yum.repos.d]# sysctl –system
[root@node22 docker]# vim daemon.json
[root@node22 docker]# chmod 777 /var/run/docker.sock
[root@node22 docker]# systemctl restart docker
[root@node22 yum.repos.d]# scp CentOS-Base.repo node55:/etc/yum.repos.d
[root@node22 yum.repos.d]# scp docker-ce.repo node55:/etc/yum.repos.d
[root@node22 ~]# cd /etc/sysctl.d/
[root@node22 sysctl.d]# ls
99-sysctl.conf docker.conf
[root@node22 sysctl.d]# scp docker.conf node55:/etc/sysctl.d
[root@node22 sysctl.d]# cd /etc/docker
[root@node22 docker]# ls
certs.d daemon.json key.json
[root@node22 docker]# scp -r certs.d/ node55:/etc/docker
[root@node22 docker]# scp -r daemon.json/ node55:/etc/docker
[root@node22 ~]# scp jdk-11.0.15_linux-x64_bin.rpm node55:
[root@node55 ~]# yum install -y jdk-11.0.15_linux-x64_bin.rpm
[root@node55 ~]# yum install -y fontconfig
[root@node55 ~]# yum install -y git
[root@node11 ~]# vim /etc/ssh/ssh_config
Node22:jkenkins node11:仓库 node33:gitlab node55:git agent
1、添加ansible交付任务:
• Jenkins服务器提前部署好到目标主机的ssh免密,并安装ansible软件包。
• 由于是以jenkins用户调用ansible命令,所以需要设置jenkins用户到目标主机的免密。
• 新建playbook代码仓库删除docker-1
下载ansible
[root@node22 ~]# yum install ansible-2.8.5-1.el7.noarch.rpm -y
新建gitlab中的ansible项目:
[root@node33 ~]# git clone [email protected]:root/ansible.git
[root@node33 ansible]# git remote -v
origin [email protected]:root/ansible.git (fetch)
origin [email protected]:root/ansible.git (push)
[root@node33 ansible]# vim ansible.cfg
[defaults]
command_warnings=False
remote_user=devops
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[root@node11 ~]# useradd -u 2000 devops
[root@node11 ~]# visudo
[root@node55 ~]# useradd -u 2000 devops
[root@node55 ~]# visudo
[root@node33 inventory]# vim test
[test]
192.168.0.11 http_port=8080
[root@node33 inventory]# cat prod
[prod]
192.168.0.55 http_port=80
[root@node11 conf]# scp httpd.conf node33:~/ansible
[root@node33 ansible]# mv httpd.conf httpd.conf.j2
[root@node33 ansible]# vim playbook.yaml
[root@node33 ansible]# vim httpd.conf.j2
[root@node33 ansible]# git add .
[root@node33 ansible]# git commit -m "v11"
[main 693f758] v11
5 files changed, 388 insertions(+)
create mode 100644 ansible.cfg
create mode 100644 httpd.conf.j2
create mode 100644 inventory/prod
create mode 100644 inventory/test
create mode 100644 playbook.yaml
[root@node33 ansible]# git push -uf origin main
Counting objects: 9, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 5.25 KiB | 0 bytes/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To [email protected]:root/ansible.git
7bd3592..693f758 main -> main
Branch main set up to track remote branch main from origin.
[root@node22 ~]# usermod -s /bin/bash jenkins
[root@node22 ~]# su - jenkins
-bash-4.2$ ssh-keygen
-bash-4.2$ ssh-copy-id [email protected]
-bash-4.2$ ssh-copy-id [email protected]