CI说明
CI 脚本由三个文件组成
-
.gitlab-ci.yml 驱动脚本
-
build.sh 编译网站的脚本
-
deploy_preview.sh 部署网站到 preview 机器的脚本
目录结构
- root
- Readme.md
- .gitignore
- relearn
- hugo.toml
- .gitlab-ci.yml
- build.sh
- deploy_preview.sh
.gitlab-ci.yml
stages:
- build
- deploy_preview
- publish
build_doc:
tags:
- kylin-11-docs
stage: build
image: 192.168.40.10:5000/hugo:v0.148.2ext
artifacts:
paths:
- "*/public/"
when: on_success
expire_in: "30 days"
script:
- ./build.sh "preview.docs.njet.org.cn"
deplo_to_preview:
dependencies:
- build_doc
tags:
- kylin-11-docs
stage: deploy_preview
image: 192.168.40.10:5000/ubuntu_18.04:to_11
script:
- ./deploy_preview.sh
rules:
- if: $CI_COMMIT_BRANCH == "main" # 确保在 main 分支上
when: always # 确保在 main 分支上总是运行
- when: never # 兜底,如果没有满足以上条件,则不运行
Caution
每次增加新版本,都需要修改build.sh及deploy_pveview.sh build.sh
#!/usr/bin/bash
set -xe
# 替换网站
domain="$1"
pwd=$(pwd)
sed -i "s/docs.njet.org.cn/$domain/" hugo.toml
cd v1.0
sed -i "s/version = .*/version = 'v1.0'/" hugo.toml
sed -i "1s#baseURL = .*#baseURL = 'https://docs.njet.org.cn/contrib/v1.0'#" hugo.toml
hugo build
cd -
cd v2.0
# 每次编译之前 替换一下version 即可
sed -i "s/version = .*/version = 'v2.0'/" hugo.toml
sed -i "1s#baseURL = .*#baseURL = 'https://docs.njet.org.cn/contrib/v2.0'#" hugo.toml
hugo build
echo "打包完成"
deploy_preview.sh
#!/bin/bash
set -xe
# ssh root@192.168.40.11 rm -rf /html/contrib/v1.0/*
# ssh root@192.168.40.11 rm -rf /html/contrib/v2.0/*
scp -r v1.0/public/* root@192.168.40.11:/html/contrib/v1.0/
scp -r v2.0/public/* root@192.168.40.11:/html/contrib/v2.0/
# 重新链接 html/contrib/latest 为新建的版本
ssh root@192.168.40.11 rm -rf /html/contrib/latest
ssh root@192.168.40.11 ln -s /html/contrib/v2.0 /html/contrib/latest