Yishi Lin

  • Home

  • Archives

  • Dataset

  • Blog

  • Categories

  • Search

Hexo Deployment with GitLab CD

Posted on 2018-11-04 In 瞎折腾

终极目标

目标:修改好Hexo源文件,push到GitLab后,可以自动编译加部署到VPS。有了自动部署,就可以在任何可以git的地方修改博客啦,不用每台机子都装一大堆环境啦!

原材料

  1. Hexo站点一个,源码放在GitLab的private repo里
  2. VPS一个,之前靠人肉rsync编译好的站点文件上去

碎碎念

说实话,我觉得我还是只会在我的Mac上更新的,而且十有八九我还是会在本地编译和看看效果的。Anyway,折腾一下,也算是体验一把GitLab CI/CD 吧!

配置Hexo Deploy

首先需要配置hexo deploy。照着文档Hexo Deploy - Rsync修改_config.yml就可以了。由于之前一直靠rsync同步文件,所以这一步很简单,不需要额外安装或者配置其它东西。

配置修改后hexo d一下,确认配置正确。

Using SSH keys with GitLab CI/CD

相关文档

  1. GitLab Continuous Integration (GitLab CI/CD)
  2. Using SSH keys with GitLab CI/CD
  3. node - Docker

步骤

  1. 生成一对新的 SSH key(用已有的key也可以),把公钥放到server上。
  2. 顺着文档2完成“Add the private key as a variable to your project”这一步。
  3. 准备好.gitlab-ci.yml。

踩坑

  1. 在项目设置里配好SSH_PRIVATE_KEY这个variable后,CI始终报错,报错如下。一顿折腾,发现是手贱在配置variable的时候打勾了“protected”,这样会导致variable只对protected branch/tag可见。

    1
    2
    $ echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    Enter passphrase for (stdin): ERROR: Job failed: exit code 1
  2. hexo d在rsync时报错如下。最后发现是docker需要安装rsync。

    1
    2
    3
    4
    5
    6
    7
    8
    $ hexo d
    INFO Deploying: rsync
    FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
    Error: spawn rsync ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:238:19)
    at onErrorNT (internal/child_process.js:413:16)
    at process.internalTickCallback (internal/process/next_tick.js:72:19)
    ERROR: Job failed: exit code 1

点击More可以看到完整的.gitlab-ci.yml.

把.gitlab-ci.yml贴在这里,虽然不知道是不是最优雅的写法,但是能用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
image: node:11.1.0

before_script:
##
## Install ssh-agent if not already installed, it is required by Docker.
## (change apt-get to yum if you use an RPM-based image)
##
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'

##
## Run ssh-agent (inside the build environment)
##
- eval $(ssh-agent -s)

##
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
## We're using tr to fix line endings which makes ed25519 keys work
## without extra base64 encoding.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null

##
## Create the SSH directory and give it the right permissions
##
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh

##
## Use ssh-keyscan to scan the keys of your private server. Replace gitlab.com
## with your own domain name. You can copy and repeat that command if you have
## more than one server to connect to.
##
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts

## Clone the submodule (theme)
- git submodule sync --recursive
- git submodule update --init --recursive

## Make sure that rsync is installed
- 'which rsync || ( apt-get update -y && apt-get install rsync -y)'

build:
cache:
paths:
- node_modules/

script:
- npm install hexo-cli -g
- npm install
- node -v
- npm -v
- hexo -v
- hexo clean && hexo g
- hexo d
only:
- master
# hexo # gitlab ci
升级Hexo及NexT
因果推断学习笔记:资料收集
  • Table of Contents
  • Overview
Yishi Lin

Yishi Lin

24 posts
11 categories
25 tags
RSS
GitHub E-Mail
  1. 1. 终极目标
  2. 2. 配置Hexo Deploy
  3. 3. Using SSH keys with GitLab CI/CD
© 2013 – 2021 Yishi Lin
Powered by Hexo v3.9.0
|
Theme – NexT.Gemini v7.3.0