gitlab-cicd持续部署-保姆式基础教学gitlab-cicd 是一个强大的自动化技术支持任意规模的构建与测试持续集成(CI) 由您开始。您在合并请求MR中共享新代码并触发Pipeline。构建、测试和验证——极狐GitLab负责完成其余的工作。持续交付 (CD) 将您的辛勤努力付诸实践通过结构化部署Pipeline将经过CI验证的代码移交给您的应用程序。环境linux centos7.9基本配置安装阿里云镜像源[rootgitlab-cicd ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo[rootgitlab-cicd ~]# yum clean all[rootgitlab-cicd ~]# yum makecache安装相关依赖[rootgitlab-cicd ~]# yum -y install policycoreutils openssh-server openssh-clients postfix[rootgitlab-cicd ~]# yum -y install policycoreutils-python[rootgitlab-cicd ~]# systemctl enable sshd systemctl start sshd[rootgitlab-cicd ~]# systemctl enable postfix systemctl start postfix关闭防火墙以及selinux[rootgitlab-cicd ~]# systemctl stop firewalld systemctl disable firewalld[rootgitlab-cicd ~]# setenforce 0[rootgitlab-cicd ~]# sed -i s/SELINUXenforcing/SELINUXdisabled/ggitlab-ce安装与操作下载gitlab-ce软件清华源包https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-15.6.2-ce.0.el7.x86_64.rpmpackageCloud快很多https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-15.5.6-ce.0.el7.x86_64.rpm/download.rpm[rootgitlab-cicd ~]# wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-15.5.6-ce.0.el7.x86_64.rpm/download.rpm安装gitlab-ce[rootgitlab-cicd ~]# rpm -ivh download.rpm#或[rootgitlab-cicd ~]# yum install -y download.rpm修改配置文件[rootgitlab-cicd ~]# vim /etc/gitlab/gitlab.rb修改 external_url(主机实例地址)和nginx[listen_port]监听端口的keyexternal_urlhttp://10.0.54.200:80nginx[listen_port]80重启#重新加载配置文件[rootgitlab-cicd ~]# gitlab-ctl reconfigure#重启gitlab-ce客户端[rootgitlab-cicd ~]# gitlab-ctl restart访问gitlab用户为root默认密码在文件/etc/gitlab/initial_root_password生成的初始密码在24小时过期http://10.0.54.200:80/修改root密码点击头像进入Edit profile点击password输入密码并修改我这里设置成了123456asd生产环境建议设置复杂一点创建项目创建新分支测试项目git相关操作这里使用linux进行操作安装git工具[rootgitlab-cicd test]# yum install -y git将本地仓库与远程仓库建立连接[rootgitlab-cicd ~]# mkdir test[rootgitlab-cicd ~]# cd test/##设置邮箱[rootgitlab-cicd test]# git config --global user.email hj580231126.com##设置用户名[rootgitlab-cicd test]# git config --global user.name haojuetrace#初始化本地仓库[rootgitlab-cicd test]# git init#将本地仓库与远程仓库建立连接[rootgitlab-cicd test]# git remote add origin http://10.0.54.200/gitlab-instance-75e6e0ee/test.git创建测试文件[rootgitlab-cicd test]# echo haojuetrace index.html将测试文件推送到远程仓库##创建新分支[rootgitlab-cicd test]# git branch main#将所有文件添加到本地仓库[rootgitlab-cicd test]# git add .#提交所有更新过的文件[rootgitlab-cicd test]# git commit -m test index.html#将文件推送到远程仓库的master分支[rootgitlab-cicd test]# git push -uf origin master可以看到文件推送上来了GitLab Runner安装与操作添加gitlab-runner官方库[rootgitlab-cicd ~]# curl -O https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh[rootgitlab-cicd ~]# bash script.rpm.sh安装gitlab runner[rootgitlab-cicd ~]# yum install -y gitlab-ci-multi-runnergitlab-runner 注册获取gitlab-ci的Token项目主页 - Sttings - CI/CD - Runners Expand注册gitlab-runner需要按照步骤输入输入gitlab的服务URL这个使用的是https://gitlab.com/输入gitlab-ci的Toekn获取方式参考上图关于集成服务中对于这个runner的描述给这个gitlab-runner输入一个标记这个tag非常重要在后续的使用过程中需要使用这个tag来指定gitlab-runner是否运行在没有tag的build上面。在配置gitlab-ci的时候会有很多job每个job可以通过tags属性来选择runner。这里为true表示如果job没有配置tags也执行是否锁定runner到当前项目选择执行器gitlab-runner实现了很多执行器可用在不同场景中运行构建详情可见GitLab Runner Executors这里选用Shell模式[rootgitlab-cicd ~]# gitlab-runner registerRuntime platformarchamd64oslinuxpid41570revision133d7e76version15.6.1 WARNING: Theregistercommandhas been deprecatedinGitLab Runner15.6and will be replaced with adeploycommand. Formoreinformation, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872 Runninginsystem-mode. Enter the GitLab instance URL(for example, https://gitlab.com/): http://10.0.54.200/#刚查看到的实例地址Enter the registration token: GR13489412yCA5u_xnfYaT9kvTGTk#刚查看的注册tokenEnter a descriptionforthe runner:[gitlab-cicd]:test#描述Enter tagsforthe runner(comma-separated):test#设置一个标记具体看上面描述Enter optional maintenance noteforthe runner:true#是否运行在没有tag的build上面 这里true代表没有tag也执行Registering runner... succeededrunnerGR13489412yCA5u_x Enter an executor: parallels, shell, virtualbox, docker-sshmachine, custom, docker-ssh, dockermachine, instance, kubernetes, docker, ssh: shell#gitlab-runner执行器Runner registered successfully. Feelfreeto start it, butifits running already the config should be automatically reloaded!Configuration(with the authentication token)was savedin/etc/gitlab-runner/config.toml刷新刚才获取token的页面可以看到新增的一个Runnercicd演示测试演示进入到项目目录[rootgitlab-cicd ~]# cd test/编写.gitlab-ci.yml配置文件[rootgitlab-cicd test]# vim .gitlab-ci.ymlstages:# 分段- deploy deploy-job: tags: -teststage: deploy script: -echohello world更新项目数据[rootgitlab-cicd test]# echo test cicd index.html将文件推送到远程仓库[rootgitlab-cicd test]# git add .[rootgitlab-cicd test]# git commit -m cicd test[rootgitlab-cicd test]# git push -uf origin master可以看到文件推送上来了并且gitlab-runner执行成功[rootgitlab-cicd test]# it add .[rootgitlab-cicd test]# git commit -m cicd test[rootgitlab-cicd test]# git push -uf origin master可以看到文件推送上来了并且gitlab-runner执行成功