CICD 项目
| Ubuntu 26.04 | U_gitlab | 192.168.188.70 |
|---|---|---|
| Ubuntu 26.04 | U_docker | 192.168.188.30 |
部署 gitlab
# U_gitlab 执行aptupdateaptinstall-ycurlopenssh-server ca-certificates tzdata perl postfixcurl-fsSLhttps://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh|sudoos=ubuntudist=noblebashEXTERNAL_URL="http://192.168.188.70:8081"aptinstallgitlab-ce# 指定端口和时区cat>>/etc/gitlab/gitlab.rb<<'EOF' gitlab_rails['time_zone'] = 'Asia/Shanghai' # 指定时区 puma['worker_processes'] = 2 # 降低 GitLab Puma worker 数量 EOFgitlab-ctl reconfigure# 重载配置文件# 更改密码gitlab-rails console-eproduction user=User.where(id:1).first user.password='yijiuruchu'user.password_confirmation='yijiuruchu'user.save!exit# 配置文件:/etc/gitlab/gitlab.rb# 仓库代码、数据库数据:/var/opt/gitlab# 日志:/var/log/gitlab访问:http://192.168.188.70:8081/
点击右上角头像 --> Preferences --> Localization --> 中文
首页 --> 项目 --> 创建项目 --> 创建空白项目
# U_gitlab 执行ssh-keygen-trsa-P""-f~/.ssh/id_rsacat~/.ssh/id_rsa.pub# 获取密钥配置免密点击右上角头像 --> 偏好设置 --> 访问 --> SSH 密钥 --> 添加新密钥
# U_gitlab 执行cd~gitclone git@192.168.188.70:root/test.gitcd~/testgitconfig--globaluser.name"user1"gitconfig--globaluser.email"user1@user1.org"gitremote-v# 查看当前仓库配置的远程仓库地址echotest>testgitaddtestgitstatus-s# A test,提交到暂存区gitcommit-m"add test"# 提交到版本库gitstatus-sgitpush-uorigin main# 本地 main 分支的提交推送到远程仓库,并建立追踪关系# 后续可直接用 git push 推送部署 docker
# U_docker 执行aptupdateaptinstall-yapt-transport-https ca-certificatescurlsoftware-properties-commoncurl-fsSLhttps://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg|sudogpg--dearmor-o/usr/share/keyrings/docker-archive-keyring.gpgecho"deb [arch=$(dpkg --print-architecture)signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu$(lsb_release-cs)stable"|sudotee/etc/apt/sources.list.d/docker.list>/dev/nullaptupdateaptinstall-ydocker-ce docker-ce-cli containerd.io systemctlenabledocker--nowmkdir/etc/dockercat>/etc/docker/daemon.json<<'EOF' { "registry-mirrors": [ "https://docker.1ms.run" ], "insecure-registries": [ "192.168.188.30" ], "exec-opts": [ "native.cgroupdriver=systemd" ] } EOFsystemctl daemon-reload systemctl restartdockerdocker-v# 安装 docker-composewgethttps://github.com/docker/compose/releases/download/v5.3.1/docker-compose-linux-x86_64mvdocker-compose-linux-x86_64 /usr/local/bin/docker-composechmod+x /usr/local/bin/docker-composedocker-compose--version部署 harbor
# U_docker 执行wgethttps://github.com/goharbor/harbor/releases/download/v2.15.4/harbor-offline-installer-v2.15.4.tgztar-zxfharbor-offline-installer-v2.15.4.tgzcdharborcpharbor.yml.tmpl harbor.ymlsed-i's/hostname: reg.mydomain.com/hostname: 192.168.188.30/'harbor.ymlsed-i's/https:/#https:/'harbor.ymlsed-i's/ port: 443/ #port: 443/'harbor.ymlsed-i's/^[[:space:]]*certificate:.*/# certificate: \/your\/cert\/path/'harbor.ymlsed-i's/^[[:space:]]*private_key:.*/# private_key: \/your\/key\/path/'harbor.yml ./install.sh# 首次安装或彻底重装才能执行# 访问:http://192.168.188.30:80# 用户:admin# 密码:Admin@12345# 修改修改配置文件后需要执行的命令# ./prepare # 重载配置文件# U_docker 执行(测试)dockerlogin-uadmin-pAdmin@12345192.168.188.30# 登录dockerpull docker.1ms.run/nginx:1.24.0# 拉取dockertag docker.1ms.run/nginx:1.24.0192.168.188.30/test/nginx:1.24.0# 打标签dockerpush192.168.188.30/test/nginx:1.24.0# 上传dockerpull192.168.188.30/test/nginx:1.24.0# 下载部署 Jenkins
# U_docker 执行aptupdateapt-yinstallopenjdk-21-jre-headless fontconfigdockerpull docker.1ms.run/jenkins/jenkins:2.580.1-lts-jdk21dockertag docker.1ms.run/jenkins/jenkins:2.580.1-lts-jdk21 jenkins/jenkins:2.580.1-lts-jdk21dockervolume create jenkins-datadockerrun-d\-p8081:8080\-p50000:50000\-vjenkins-data:/var/jenkins_home\-v/var/run/docker.sock:/var/run/docker.sock\-v/usr/bin/docker:/usr/bin/docker\--userroot\--namejenkins\jenkins/jenkins:2.580.1-lts-jdk21dockerexecjenkinscat/var/jenkins_home/secrets/initialAdminPassword# 获取密钥# http://192.168.188.30:8081/访问 http://192.168.188.30:8081,选择安装推荐的插件,如果有下载失败的插件就选择重试
鼠标移动到右上角头像 --> Preferences --> Appearance --> 选择暗色的界面
jenkins 和 gitlab 连接
# U_docker 执行dockerexec-itjenkinsbash# 进入 Jenkins 容器ssh-keygen-trsa-b4096-mPEM-C"jenkins-gitlab"-N""-q-f~/.ssh/id_rsa# 生成密钥cat~/.ssh/id_rsa.pub# 公钥配置给 GitLabcat~/.ssh/id_rsa# 私钥配置给 Jenkins 凭据exit# 退出gitlab 配置 Jenkins 密钥
gitlab:点击右上角头像 --> 偏好设置 --> 访问 --> SSH 密钥 --> 添加新密钥
Jenkins 配置 SSH
右上角设置 --> 凭证管理 --> 添加凭证 --> SSH Username with private key
修改密钥检测策略
创建 Freestyle 项目
jenkins 中 gitlab 触发器的部署与使用
在 Jenkins 中配置 GitLab 触发器,可以实现代码提交或合并请求时自动触发 Jenkins 流水线
安装插件
右上角设置 --> 插件管理 --> Available plugins
配置自动触发
插件加载完后,在 jenkins 中选择之前构建的项目并配置自动触发
gitlab 配置出站请求,如果不配置,webhooks 会报错(URL 无效)
在 gitlab 中设定
添加新的 webhooks
推送事件
Jenkins 查看
自动触发测试
# U_docker 执行cd~/testechotest-01>>testgitaddtestgitcommit-m"test v1"gitpush-uorigin mainCICD 处理流程:代码推送到 gitlab 仓库,gitlab 将事件推送给 Jenkins,Jenkins 根据设定的构建命令内容去处理上传的代码文件
持续集成与持续交付示例
为了让容器构建镜像可以持续集成并自动上传到harbor 仓库,业务主机通过持续交付自动从仓库中下载镜像最近版本并实现业务更新
安装插件
配置构建节点
自动运行构建好的镜像生成容器
dockerrm-fmyapp||truedockerrmi-f192.168.188.30/test/nginx:latest||truesleep2dockerrun-d--namemyapp-p8082:80192.168.188.30/test/nginx:latest# U_gitlab 执行cd~/testcat>index.html<<'EOF' <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>部署测试</title> </head> <body> <h1>部署成功</h1> </body> </html> EOFcat>Dockerfile<<'EOF' FROM nginx:1.28.3 COPY index.html /usr/share/nginx/html/index.html EOFgitadd.gitcommit-m"nginx 1.28.3"gitpush-uorigin main# U_docker 执行dockerps|grepmyapp