什么是CICD?
CI-持续集成
CD-持续交付
CD-持续部署
下面我们来看下如何用 gitlab+jenkins+docker 实现CICD
一 安装 gitlab
1.安装ssh
yum install -y curl policycoreutils-pythonopenssh-server
2.将SSH服务设置成开机自启动,安装命令:sudo systemctl enable sshd
3.启动SSH服务,安装命令:sudo systemctl start sshd
4.关闭防火墙
5.安装Postfix以发送通知邮件,安装命令:sudo yum install postfix
6.将postfix服务设置成开机自启动,安装命令:sudo systemctl enable postfix
7.启动postfix,安装命令:sudo systemctl start postfix
8.添加gitlab 清华yum源
创建 /etc/yum.repos.d/gitlab-ce.repo 文件,内容为
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
再执行
sudo yum makecache
sudo yum install gitlab-ce
编辑/etc/gitlab/gitlab.rb文件,external_url ‘http://locahost’ 改成自己的ip地址
##! address from AWS. For more details, see:
##! https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
external_url 'http://192.168.116.130:8000'
9.生效配置并启动gitlab
gitlab-ctl reconfigure
[root@localhost ~]# gitlab-ctl start
ok: run: alertmanager: (pid 1739) 0s
ok: run: gitaly: (pid 1726) 0s
ok: run: gitlab-exporter: (pid 1745) 0s
ok: run: gitlab-workhorse: (pid 1728) 0s
ok: run: grafana: (pid 1743) 0s
ok: run: logrotate: (pid 1711) 0s
ok: run: nginx: (pid 1752) 0s
ok: run: node-exporter: (pid 1751) 0s
ok: run: postgres-exporter: (pid 1741) 0s
ok: run: postgresql: (pid 1750) 0s
ok: run: prometheus: (pid 1732) 0s
ok: run: puma: (pid 1735) 0s
ok: run: redis: (pid 1725) 0s
ok: run: redis-exporter: (pid 1730) 0s
ok: run: sidekiq: (pid 1748) 0s
10.设置管理员账号
gitlab-rails console -e production
Loading production environment (Rails 6.1.3.2)
irb(main):001:0> u=User.find(1)
=> #
irb(main):002:0> u.password='test12345678'
=> "test12345678"
irb(main):003:0> u.save
Enqueued ActionMailer::MailDeliveryJob (Job ID: 63665a45-e611-428c-9903-8ac8fb1fccb9) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", {:args=>[#>]}
=> true
irb(main):004:0> exit
11.登陆gitlab
二. 安装jenkins
1.安装jdk 并配置环境变量(不细说)
2.yum 安装jenkins
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install epel-release # repository that provides 'daemonize'
yum install java-11-openjdk-devel
yum install jenkins
3.配置jenkins
设置端口号
vi /etc/sysconfig/jenkins
## Type: integer(0:65535)
## Default: 8080
## ServiceRestart: jenkins
#
# Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_PORT="8088"
设置jdk 路径
vi /etc/init.d/jenkins
# Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
# see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/local/jdk1.8.0_301/bin/java
4. 启动jenkins
service jenkins start
5.首次访问输入默认密码
查看默认密码
cat /var/lib/jenkins/secrets/initialAdminPassword
6.登录
7. 插件下载镜像地址修改为 http://mirror.xmission.com/jenkins/updates/update-center.json
重启生效
8.如果还是无法下载插件可以按下面两步修改
1、修改 jenkins 数据目录中的hudson.model.UpdateCenter.xml值为:
sed -i ‘s/https://updates.jenkins.io/update-center.json/https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json/g’ /root/.jenkins/hudson.model.UpdateCenter.xml
2、修改updates文件夹中的 default.json文件
sed -i ‘s/http://updates.jenkins-ci.org/download/https://mirrors.tuna.tsinghua.edu.cn/jenkins/g’ /root/.jenkins/updates/default.json
sed -i ‘s/http://www.google.com/http://www.baidu.com/g’ /root/.jenkins/updates/default.json
9.将jenkins账户改成root权限
vim /etc/sysconfig/jenkins 将jenkins_user 改为root 重启生效
## Type: string
## Default: "jenkins"
## ServiceRestart: jenkins
#
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins.
#
JENKINS_USER="root"
三 .安装docker
sudo yum install docker-ce
[root@centos1 updates]# docker -v
Docker version 20.10.8, build 3967b7d
四. gitlab创建工程
push 本地项目到gitlab
五. 配置jenkins
1.新建一个工程
2.配置项目
1.源码管理
2. 添加凭证
设置触发器
生成token
设置 gitlab的webhooks
构建
编写构建后shell脚本
六. 提交代码到git触发构建部署