
CICD 项目Ubuntu 26.04U_gitlab192.168.188.70Ubuntu 26.04U_docker192.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|sudoosubuntudistnoblebashEXTERNAL_URLhttp://192.168.188.70:8081aptinstallgitlab-ce# 指定端口和时区cat/etc/gitlab/gitlab.rbEOF gitlab_rails[time_zone] Asia/Shanghai # 指定时区 puma[worker_processes] 2 # 降低 GitLab Puma worker 数量 EOFgitlab-ctl reconfigure# 重载配置文件# 更改密码gitlab-rails console-eproduction userUser.where(id:1).first user.passwordyijiuruchuuser.password_confirmationyijiuruchuuser.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 git192.168.188.70:root/test.gitcd~/testgitconfig--globaluser.nameuser1gitconfig--globaluser.emailuser1user1.orggitremote-v# 查看当前仓库配置的远程仓库地址echotesttestgitaddtestgitstatus-s# A test提交到暂存区gitcommit-madd 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.gpgechodeb [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.jsonEOF { registry-mirrors: [ https://docker.1ms.run ], insecure-registries: [ 192.168.188.30 ], exec-opts: [ native.cgroupdriversystemd ] } 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-composechmodx /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-is/hostname: reg.mydomain.com/hostname: 192.168.188.30/harbor.ymlsed-is/https:/#https:/harbor.ymlsed-is/ port: 443/ #port: 443/harbor.ymlsed-is/^[[:space:]]*certificate:.*/# certificate: \/your\/cert\/path/harbor.ymlsed-is/^[[:space:]]*private_key:.*/# private_key: \/your\/key\/path/harbor.yml ./install.sh# 首次安装或彻底重装才能执行# 访问http://192.168.188.30:80# 用户admin# 密码Admin12345# 修改修改配置文件后需要执行的命令# ./prepare # 重载配置文件# U_docker 执行测试dockerlogin-uadmin-pAdmin12345192.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-Cjenkins-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-01testgitaddtestgitcommit-mtest v1gitpush-uorigin mainCICD 处理流程代码推送到 gitlab 仓库gitlab 将事件推送给 JenkinsJenkins 根据设定的构建命令内容去处理上传的代码文件持续集成与持续交付示例为了让容器构建镜像可以持续集成并自动上传到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~/testcatindex.htmlEOF !DOCTYPE html html head meta charsetUTF-8 title部署测试/title /head body h1部署成功/h1 /body /html EOFcatDockerfileEOF FROM nginx:1.28.3 COPY index.html /usr/share/nginx/html/index.html EOFgitadd.gitcommit-mnginx 1.28.3gitpush-uorigin main# U_docker 执行dockerps|grepmyapp