gitlab-ci持续集成主要包括两方面:gitlab、runner。gitlab负责代码管理,runner负责版本管理、编译、推送。公司已有gitlab服务,现在只需要创建runner与gitlab对接即可。
- 下载ubuntu Gitlab-Runner安装包
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
- 安装gitlab-ci-multi-runner
sudo apt-get install gitlab-ci-multi-runner
- 安装完成后需要查看安装是否成功,执行:
sudo gitlab-runner status
如果显示gitlab-runner: Service is running!即为安装成功,此时会在/home目录下生成gitlab-runner文件夹。这里是最令人无语的地方,gitlab-runner创建了一个新用户,gitlab-runner只是普通
sudo usermod -aG sudo gitlab-runner
- 注册runner并和对应的工程关联起来,我们需要用到工程的token值,所以在这里我们先在gitLab上创建工程。在项目的settings下的CI/CD下找到token,然后在本地执行注册
sudo gitlab-runner register
接下来需要填写一些信息:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
URL
Please enter the gitlab-ci token for this runner:
token
Please enter the gitlab-ci description for this runner:
[DESKTOP-2P9GHDD]: gitbook publish
Please enter the gitlab-ci tags for this runner (comma separated):
master
Registering runner... succeeded runner=avuSXASJ
Please enter the executor: docker-ssh, parallels, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, docker:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
- 在本地仓库中创建隐藏文件.gitlab-ci.yml,内容为:
build:
stage: build
script:
- pwd
- tree
- gitbook init
- python3 summarybuilder.py
- gitbook install
- gitbook build
- ./scpf.sh
summarybuilder.py,./scpf.sh为自己根据需要添加的脚本,在本地仓库创建隐藏文件.gitignore,内容为:
**/_book
**/node_modules
**/*.pyc
*.swp
-
添加代码并push之后在jobs选项卡下就能看到编译结果,点击pass能看到控制台编辑结果
-
启动gitbook。在gitlab该项目的piplines中,我们找到gitbook的输出目录,即pwd命令所打印的目录,如/home/gitlab-runner/builds/00bbd75f/0/book/overview。登录centos后使用gitlab-runner用户,切换到上述路径后,执行:
nohup gitbook serve --port 15016 &
即完成了gitbook的启动(端口自己指定,默认4000),而且当gitbook发生变动后,他会自动发布变动。
参考
- https://www.cnblogs.com/fithon/p/6645020.html
- https://www.lefer.cn/posts/53574/
- https://www.lefer.cn/posts/25801/
- https://scarletsky.github.io/2016/07/29/use-gitlab-ci-for-continuous-integration/
- 实在不行就通过scp实现编译文件的传递,https://www.cnblogs.com/wanzaiyimeng/p/6700138.html
- docker,https://www.zoulei.net/2017/12/25/GitLabCICD_quickstart/
- 折叠目录,http://gitbook.zhangjikai.com/plugins.html#expandable-chapters-small