1.相关文档
官方文档: GitLab Pages | GitLab
配置pages:GitLab Pages administration | GitLab
pages功能能够让gitlab成为一个web服务器,用于展示前端静态页面。配合.gitlab-ci.yml
的pages关键字,将ci/cd生产的静态文件直接发布到pages。
2.开启pages功能
2.1 前提条件
- pages需要一个独立的泛域名
- 需要通配符域名解析记录(非必须,但是如果没有通配符域名解析记录则需要每个组都手动添加一条解析记录)
- 需要通配符域名证书
假设我们这里的通配符域名为:*.pages.futureman.xin
最后每个项目访问的路径,类似于:https://.pages.futureman.xin/<project_slug>
2.2制作通配符域名证书
2.2.1使用Let’s Encrypt获取证书(建议使用)
参考:入门指南 - Let's Encrypt - 免费的SSL/TLS证书
2.2.2自签证书
mkdir myca
cd myca
openssl genrsa -out ca.key 2048
openssl req -new -x509 -days 365 -key ca.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=Acme Root CA" -out ca.crt
ls
openssl req -newkey rsa:2048 -nodes -keyout pages-nginx.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=*.pages.futureman.xin" -out pages-nginx.csr
openssl x509 -req -extfile <(printf "subjectAltName=DNS:*.pages.futureman.xin") -days 365 -in pages-nginx.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out pages-nginx.crt
2.3 配置pages功能
修改配置:
vim /etc/gitlab/gitlab.rb
pages_external_url "https://pages.futureman.xin" # not a subdomain of external_url
pages_nginx['enable'] = true
# 如果直接使用gitlab的nginx提供pages web访问
pages_nginx['redirect_http_to_https'] = true
pages_nginx['ssl_certificate'] = "/etc/gitlab/ssl/pages-nginx.crt"
pages_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/pages-nginx.key"
# 如果使用nginx,haproxy之类的代理提供pages web访问
# pages_nginx['listen_port'] = 80
# pages_nginx['listen_https'] = false
# pages_nginx['redirect_http_to_https'] = false
gitlab-ctl reconfigure
3.发布pages
3.1 运行CI/CD pipeline
在启用了pages功能之后就可以通过CI/CD pipeline 发布pages页面了。
参考CI/CD pipeline:
pages:
script:
- mkdir public
- cp index.html public
artifacts:
paths:
- public
tags:
- shell
pages stage运行之后,会自动生成一个deploy的job:
功能测试可以参考:示例测试项目
3.2 查看发布的页面
当pages CI/CD pipeline运行完毕之后,依次点击项目的:设置–Pages,进入pages设置界面,在这里我们能看到当前项目pages的访问地址,直接点击就可以访问了: