之前折腾局域网搭建 Gitbook,并写了一篇简易教程:Gitbook + Jenkins + Gitlab 搭建内网自动构建的 Gitbook。最近兴趣使然又用 docker 搭建了一套方便部署的内网 Gitbook 镜像,因此也总结一篇简易教程如下。
Install Docker
1 | sudo apt install docker |
Gitbook
Gitbook Image
下载 Gitbook Docker 镜像,我选择了 billryan/gitbook 镜像:
1 | docker pull billryan/gitbook |
mkdir gitbook
创建一个 gitbook 路径,我们也可以将启动的镜像存储为另一个镜像:
1 | # init |
存储一个本地 docker 镜像,方便迁移到其他机器,存储和加载命令如下:
1 | # Save docker image |
Gitbook build
我做了一个脚本 gitbook.sh
给 Jenkins
提供执行脚本,内容如下:
1 |
|
如果我们不用脚本或者想以一种简单的方式执行 docker,可以在 .bashrc
里添加别名。
1 | alias gitbook='docker run --rm -v $GITBOOK_HOME:/gitbook -p 4000:4000 andylee/gitbook:1.0 gitbook' |
GitLab
GitLab Image
Dockerfile
mkdir gitlab
创建一个 gitlab 路径,为了保证 Clone with HTTP
能正常使用,利用官方镜像做一个指定容器端口的镜像(官方默认为 80 ,一般会被占用),编写 Dockerfile 如下:
1 | # Use the official image |
Build Docker
1 | docker build --tag andylee/gitlab:1.0 . # Execute in the folder where Dockerfile is |
Run GitLab Docker
我做了一个gitlab.sh
脚本来运行 GitLab Docker,如下:
1 |
|
Config GitLab
配置 gitlab.rb
.
1 | # config/gitlab.rb |
Issues
问题一:
1 | gitlab Import url is blocked: "Requests to the local network are not allowed" |
Solution:
1 | Admin -> Settings -> Network -> Outbound Requests -> Allow requests to the local network from hooks and services |
问题二:
1 | Cloning into 'document'... |
Solution:
配置的 ssh 访问端口需要与启动容器时的映射端口保持一致,当前问题是由于 gitlab.rb
中错把端口配置为了 2202.
Jenkins
Jenkins Image
1 | docker pull jenkins/jenkins |
Run Jenkins Docker
我做了一个jenkins.sh
脚本来运行 Jenkins Docker,如下:
1 |
|
Config
这里只说一下注意事项和变化点,详细配置方式见Gitbook + Jenkins + Gitlab 搭建内网自动构建的 Gitbook。
Configure system -> gitlab, 需要注意
Gitlab host URL
只需要填 server 地址。Configure of project.
- Build Triggers
1
2# 选中如下选项
Build when a change is pushed to GitLab. GitLab webhook URL: http://192.168.81.64:4003/project/gitbook - Build->Execute shell
1
2
3# 首先需要将容器中的 ~/.ssh/id_rsa.pub 的内容追加到主机的 ~/.ssh/authorized_keys 中,不然会要求输入 ssh 密码。
# 通过 ssh 在主机中执行 gitbook.sh 脚本
ssh -n -l lee 192.168.81.64 "gitbook.sh"jenkins_home/jobs/gitbook/config.xml:
<command>ssh -n -l lee 192.168.81.64 "gitbook.sh"</command>
- 因为新版本的 Jenkins(当前使用的是 2.249) 已经不支持 disable CSRF, 我们必须在 Gitlab project 中的 webhook 配置上用户名,详细见如下问题二。
Issue
问题一:
1 | An error occurred during installation: No such plugin: cloudbees-folder |
Solution:
1 | 1. modify https as http in /var/lib/docker/volumes/jenkins-data/_data/hudson.model.UpdateCenter.xml |
问题二:
1 | HTTP ERROR 403 No valid crumb was included in the request |
Solution:
- 在
Jenkins
中,通过USER -> Configure -> API Token
增加并保存 token 值(api key)。 - 在
GitLab project
的Setting ->Webhooks
按照如下要求添加 webhook ,按照要求(此处要注意格式,需要第一步 Jenkins 额外创建一个api key ,让 GitLab 能够有权限触发 Jenkins)1
2
3
4
5
6
7
8URL:
http://username:apikey@jenkins.url/project/myPullrequest/
Secret Token: # 似乎不是必须的
apikey
Example:
http://root:1146cb664bbe589fd1a88fe42d24b64315@192.168.81.64:4003/project/gitbook
Nginx
Nginx Image
1 | docker pull nginx |
Run Nginx Docker
我做了一个nginx.sh
脚本来运行 nginx Docker,如下:
1 |
|