You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 1.运行命令
sudo gitlab-ci-multi-runner register
// 2.根据提示输入`URL`
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.***.com/ci
// 3.根据提示输入`token`
Please enter the gitlab-ci token for this runner:
TB8nknzg1woVb4pCx666
// 4.然后输入runner的描述
Please enter the gitlab-ci description for this runner:
wall-runner
// 5.输入标签,可以多个,用逗号隔开即可
Please enter the gitlab-ci tags for this runner (comma separated):
test
// 6.是否运行无此标签的构建
Whether to run untagged builds [true/false]:
true
// 7.将Runer锁定到当前项目
Whether to lock the Runner to current project [true/false]:
true
// 8.选择Runner的类型
Please enter the executor: ssh, docker+machine, kubernetes, docker, docker-ssh, parallels, shell, virtualbox, docker-ssh+machine:
shell
前言
利用
markdown
+Hexo
写文章,整体体验已经很棒。在写作过程中,节省了我不少时间。但是,美中不足的,就是发布的时候,需要手动输入命令,
build
好文件,再用scp
部署到服务器上。本文,用于记录解决这个痛点的过程。采取的解决方案就是持续集成。
以下是我用于部署个人站点的服务器概况:
正常情况下,注册
GitLab-Runner
的服务器和部署生产文件的服务器是分开的。因为穷🌚,我只有一台服务器,所以两者都部署到一起,大家就别太纠结这个点了。
一、持续集成
GitLab 8.0以上的版本就有提供持续集成服务。只要在项目中添加一个
.gitlab-ci.yml
文件,然后再添加一个Runner
,即可进行持续集成。我对自动发布博客的总体实现思路:
添加
Runner
用于监听git push
操作,然后用.gitlab-ci.yml
指导步骤的执行,最后用shell
脚本copy目标文件到指定目录下。二、注册Runner
1. 查看注册必需的
URL
和token
浏览器打开一个GitLab项目,到
Settings
-CI/CD Pipelines
下,可以看到一个Specific Runners
块,主要有以下内容:其中第2项的
URL
和第3项的token
,是注册Runner
所必需的。Runner
凭借token
注册监听对应的URL
。2. 在服务器上配置
GitLab-Runner
这里,我用SecureCRT连接上服务器,进行以下操作:
这样,一个
GitLab-Runner
就创建成功。刷新浏览器页面,在Settings
-CI/CD Pipelines
下可以看到runner已经绑定成功。三. 配置
.gitlab-ci.yml
在要添加持续集成功能的项目的根目录下,创建
.gitlab-ci.yml
文件,编写构建步骤。在编写之前,先大致了解下写法:
stages
关键字定义Pipeline
中的各个构建阶段的先后顺序cache
关键字定义每个构建阶段,不需要清除的文件job1
和job2
。也有真正的stage
名,用于stages
中标识先后的顺序script
用于定义当前构建阶段需要执行的命令only
用于指定哪个Git分支的push操作才能触发自动构建以下是我在
blog
项目应用的.gitlab-ci.yml
四、用于部署的Shell脚本
前言中,有提到一个痛点就是
scp
部署文件。因为网速的原因,每次跑scp
命令都要等好几分钟,电脑也不能关机。得等到传输完成,才可以。升级为持续集成后,就不需要在本地跑命令了,都统一在服务器上跑。
而能代替文件传输这个步骤的,就是写一个
Shell
脚本,让服务器自动copy文件到对应的目录下。以下是我应用的
Shell
脚本pub.sh
作用就是将
public
文件夹下所有文件copy到/mnt/blog/
下。五、权限问题
因为我是同一台服务器上跑命令,所以当前
Runner
进程必须对相关文件夹有写入和读取权限。所以,我把几个文件夹的读写权限赋予
Runner
进程。使用
chown
命令,对文件夹对拥有者权限进行更改:如果
Runner
服务器和生产环境服务器是相互独立的,则可以使用ssh
的方式去连接。配置好密钥和绕过指纹检查即可。六、享受愉快的持续集成体验
经过上述的配置,每次
push
代码到master
分支。Runner
监听到操作后,就会启动自动构建,完成部署。这样,我发表新文章,只需要负责把
markdown
写好,push
代码到GitLab
。其他的工作,服务器会自动帮我做好。写好文章,我也可以愉快地关机休息,不用去打理其他的事,感觉真棒!
而且,每次构建记录都有保存在GitLab上。可以在
Pipelines
中查看每次构建的结果。还可以在
README.md
加入构建状态图标:有需要的,就买个服务器折腾下,挺好玩的🌚
附上阿里云服务器的优惠券
The text was updated successfully, but these errors were encountered: