前面有讲到如何安装gitlab,它本身不仅仅可以实现代码仓库版本管理,当然可以做到cicd,简化手动部署到烦扰,实现自动化编译和部署
要做实现cicd,如果是虚拟机部署到话,当然离不开gitlab runner了,下面将会介绍如何安装和应用到项目当中
安装
选择一台你需要安装到gitlab runner服务器,这里建议你的项目部署到哪台机器,哪台机器就需要安装gitlab runner,当然项目部署的机器和gitlab runner机器也可以分开,分开就需要你自己写脚步或者使用Ansible自动部署了。
- 官网安装指导:https://docs.gitlab.com/runner/install/linux-manually.html
- 下面介绍的是cpu amd64架构的centos系统中安装
- 下载rmp包:curl -LJO “https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm”
- 安装完成后输入gitlab-runner 有提示就说明成功了
注册gitlab runner
这里有两种方式,一种是直接在当前项目设置中找到cicd选项,配置runner,这个runner就只应用在当前项目;还有一种是可以在项目的group中配置cicd runner,这个好处是不需要给这个group下面的所有的项目都配置runner,大家公用一个就行。下面的操作是在group下配置runner(gitlab 11版本一下不支持)
- 找到group设置下面的cicd
- 在安装gitlab runner机器操作
gitlab-runner register
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://你的gitlab地址
Enter the registration token:
上面截图的token
Enter a description for the runner:
[centos7]: xxxx-group-runner
Enter tags for the runner (comma-separated):
xxxx-group-runner 这个很重要,后面的gitlab.yaml文件用到
Registering runner... succeeded runner=YcTcnmWn
Enter an executor: docker-ssh, parallels, shell, virtualbox, docker-ssh+machine, kubernetes, custom, docker, ssh, docker+machine:
shell
- 查看是否运行
gitlab-runner list
- 如果配置错了,还可以删除
gitlab-runner verify --delete --name xxx
配置.gitlab-ci.yaml
- 上代码
stages:
- production
variables:
PROJECT_DIR: "/opt/root/projects/xxx"
before_script:
- cd $PROJECT_DIR
- pwd
after_script:
- cd $PROJECT_DIR
- pwd
- source vens/bin/activate
- pip install -r requirements.txt
- supervisorctl -c supervisor.conf update
- supervisorctl -c supervisor.conf restart all
deploy_for_production:
stage: production
tags:
- 上面注册runner填写的tag名称
only:
- master
script:
- git reset --hard
- git checkout master
- git pull origin master
- 到gitlab页面查看是否在跑
错误解决
- Could not resolve host: gitlab; Unknown error
Running with gitlab-runner 14.0.1 (c1edb478)
on centos7 0cf37374
Preparing the "shell" executor
Using Shell executor...
Preparing environment
Running on centos7...
Getting source from Git repository
Fetching changes...
Initialized empty Git repository in /home/gitlab-
Created fresh repository.
fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab/xxx/xxx.git/': Could not resolve host: gitlab; Unknown error
ERROR: Job failed: exit status 1
解决方式:
[root@centos7 ~]# vim /etc/gitlab-runner/config.toml
[[runners]]
clone_url = "http://gitlab地址"
- gitlab-runner权限问题
$ supervisorctl -c supervisor.conf update
error: <class 'socket.error'>, [Errno 13] Permission denied: file: /usr/lib64/python2.7/socket.py line: 224
解决方式
chmod -R 777 /path/to/supervisor目录
- .git/index.lock’: Permission denied
解决方式
把项目权限给gitlab-runner
chown -R gitlab-runner:users /path/to/your/projects
原创文章,作者:站长,如若转载,请注明出处:https://wsppx.cn/289/%e7%bd%91%e7%bb%9c%e5%bc%80%e5%8f%91/devops/