9.5 KiB
View site: https://angulareportfolio.pw
Build an Hexo site with GitLab Pages
Settings
- Theme: Butterfly
- GitLab CI - view
.gitlab-ci.yml - Hexo - view
_config.yml - Package - view
package.json
Requirements
Hexo is a SSG powered by NodeJS. So we'll need this first:
Script
We already have a Node image on Docker Library, so we'll require it to run our script on the environment of NodeJS.
This will bring npm and git core with it: image: node:latest
Then we'll need to run:
$ npm install -g hexo-cli
$ npm install
$ hexo generate
We'll set the the `npm` modules and its dependecies as a `cache` to speed up our builds:
cache:
paths:
- node_modules/
GitLab-CI with lftp
If I want GitLab CI/CD to deploy my website each time that I push to GitLab, I can use something called lftp. Here is what you can do in the gitlab-ci.yml configuration file.
image: ubuntu:latest
before_script:
- apt update -qy # Check for update
- apt install -y lftp # Install lftp
- apt install curl # Install curl
- curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
- apt install nodejs # Install NodeJS
- npm install -g hexo-cli # Install Hexo itself
- npm install # Install Hexo modules and dependencies
- npm install hexo-renderer-pug # Install Butterfly pug
- npm install hexo-renderer-stylus # Install stylus renderer
deploy via lftp:
stage: deploy
script:
- hexo clean # Hexo clean previous static site
- hexo generate # Hexo generate Static Site
- lftp -e "mirror -R public/ $FTP_REMOTE_PATH/" -u $FTP_USER,$FTP_PASS $FTP_HOST $FTP_PORT
artifacts:
paths:
- public
only:
- master # this job will affect only the 'master' branch
GitLab-CI
We need to prepare our gitlab-ci.yml configuration file to do as we described previously.
We'll also need a pages job and finally a public folder with our artifacts, affecting only our master branch:
# requiring the environment of NodeJS 4.2.2
image: node:latest
# add 'node_modules' to cache for speeding up builds
cache:
paths:
- node_modules/ # Node modules and dependencies
before_script:
- npm install -g hexo-cli # Install Hexo itself
- npm install # Install Hexo modules and dependencies
- npm install hexo-renderer-pug # Install Butterfly pug
- npm install hexo-renderer-stylus # Install stylus renderer
# the 'pages' job will deploy and build your site to the 'public' path
pages:
stage: deploy
script:
- hexo clean # Hexo clean previous static site
- hexo generate # Hexo generate Static Site
artifacts:
paths:
- public
only:
- master # this job will affect only the 'master' branch
GitLab-CI with AWS
In case if you want to deploy your website via AWS S3, here are the CI script:
build website:
stage: build
# requiring the environment of NodeJS 4.2.2
image: node:latest
# add 'node_modules' to cache for speeding up builds
cache:
paths:
- node_modules/ # Node modules and dependencies
script:
- npm install -g hexo-cli # Install Hexo itself
- npm install # Install Hexo modules and dependencies
- hexo version
# the 'pages' job will deploy and build your site to the 'public' path
artifacts:
paths:
- public
deploy to s3:
stage: deploy
image:
name: amazon/aws-cli
entrypoint: [""]
script:
- hexo clean
- hexo generate # Hexo generate Static Site
- aws --version
- aws s3 rm s3://$S3_BUCKET --recursive
- aws s3 cp public s3://$S3_BUCKET --recursive
GitHub Action
In case you want to deploy your website via GitHub Action, here are some of the code you can use:
# workflow name
name: Hexo Blog CI
# master branch on push, auto run
on:
push:
branches:
- master
jobs:
build-hexo:
runs-on: ubuntu-latest
steps:
# check it to your workflow can access it
# from: https://github.com/actions/checkout
- name: Checkout Repository master branch
uses: actions/checkout@master
# from: https://github.com/actions/setup-node
- name: Setup Node.js 10.x
uses: actions/setup-node@master
with:
node-version: "10.x"
- name: Setup Hexo Dependencies
run: |
npm install hexo-cli -g
npm install
- name: Setup Deploy Private Key
env:
HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
- name: Setup Git Infomation
run: |
git config --global user.name '名字'
git config --global user.email '邮件'
- name: Deploy Hexo
run: |
hexo clean
hexo generate
hexo deploy
GitHub Action with FTP
on: push
name: 🚀 Deploy website on push
jobs:
web-deploy:
name: 🎉 Deploy
runs-on: ubuntu-latest
steps:
- name: 🚚 Get latest code
uses: actions/checkout@v2
- name: 📂 Sync files
uses: SamKirkland/FTP-Deploy-Action@4.1.0
with:
server: ftp.samkirkland.com
username: myFtpUserName
password: ${{ secrets.ftp_password }}
port: 21
server-dir: # Setting the server-directory
GitHub Action with Hexo and FTP
# workflow name
name: Hexo Blog CI
# master branch on push, auto run
on:
push:
branches:
- master
jobs:
build-hexo:
runs-on: ubuntu-latest
steps:
# check it to your workflow can access it
# from: https://github.com/actions/checkout
- name: Checkout Repository master branch
uses: actions/checkout@master
# from: https://github.com/actions/setup-node
- name: Setup Node.js 10.x
uses: actions/setup-node@master
with:
node-version: "10.x"
- name: Setup Hexo Dependencies
run: |
npm install hexo-cli -g
npm install
- name: Setup Deploy Private Key
env:
HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
- name: Setup Git Infomation
run: |
git config --global user.name 'Leon Xu'
git config --global user.email 'dcwinfield362@gmail.com'
- name: Deploy Hexo
run: |
hexo clean
hexo generate
hexo deploy
- name: Upload result from build job
uses: actions/upload-artifact@v2
with:
name: public
path: /public
deploy-to-website:
runs-on: ubuntu-latest
steps:
# check it to your workflow can access it
# from: https://github.com/actions/checkout
- name: Get latest code
uses: actions/checkout@v2
# from: https://github.com/actions/setup-node
- name: Sync files
uses: SamKirkland/FTP-Deploy-Action@4.1.0
with:
server: ftpupload.net
username: epiz_29134768
password: ${{ secrets.ftp_password }}
port: 21
server-dir: /home/vol3_1/epizy.com/epiz_29134768/test.hijklmno.gq/htdocs/
Testing our builds before pushing content to the master branch
What if we want to test our builds in another branch before afecting master?
We'll just need to set a test job to do this for us:
image: node:latest
cache:
paths:
- node_modules/
before_script:
- npm install -g hexo-cli
- npm install
# add a job called 'test'
test:
stage: test
script:
- hexo generate
except:
- master # the 'test' job will affect all branches expect 'master'
# the 'pages' job will deploy and build your site to the 'public' path
pages:
stage: deploy
script:
- hexo generate
artifacts:
paths:
- public
only:
- master # this job will affect only the 'master' branch
Usage
Note: we assume you have Hexo 5 installed, up and running locally.
- Fork, clone or download this project
- Install Hexo Server if you don't have it already:
npm install hexo-server --save - Configure Hexo
_config.ymlaccording to your project - Navigate to the project folder (
cd path/to/project) - Run
hexo server
How to change Hexo version?
Modify package.json:
{
"hexo": {
"version": "5.4.0"
}
}