How to deploy this blog use CI CD in Gitea

As I said before, I need to migrate all service from RN to Ali cloud. Before this blog is using git hook to achieve automatization. Now I want to try to use Gitea Action, a new and more powerful tool, to do automatization. Also, you will learn how to migrate blog between two server.

Migrate your repo from old Gitea to new Gitea

Use your local terminal and switch work path as blog git repo directory.

1
vim .git/config #replace the remote url of repo
1
2
3
[remote "origin"]
url = git@your_remote_repo_git_url
fetch = +refs/heads/*:refs/remotes/origin/*

Enable Action in Gitea

Log in your Gitea server as root.

1
vim /etc/gitea/app.ini
1
2
3
#add following at the end of app.ini
[actions]
ENABLED = true
1
service gitea restart

Then go to your repo web page, click settings and enable action for your repo.

Screenshot 2024-05-30 at 15.27.08

Install Action runner for Gitea

  1. First, go to your Gitea user settings UI, and create a new action runner key

Log in your Gitea server as root.

Screenshot 2024-05-30 at 16.22.38

  1. Download the latest action runner of any version you like from official release page. As git repo file owns to user git, and in new version of git command, only the same ownership can run git clone, it is recommend to run action runner using user git.

    1
    2
    3
    4
    5
    6
    7
    8
    su - git
    mkdir gitea_actions
    cd gitea_actions
    wget https://dl.gitea.com/act_runner/0.2.10/act_runner-0.2.10-linux-amd64
    mv act_runner-0.2.10-linux-amd64 act_runner
    sudo chmod +x act_runner
    ./act_runner generate-config > config.yaml # Then input coressponding info
    vim config.yaml # edit the labels if you did not input label before
    1
    2
    3
    runner:
    labels:
    - "linux_amd64:host"

    The label of runner is very important. If it leave as default, the runner will runs in docker, which is not convenient for us to build blog static file with hexo, so we need the runner runs locally. So we need to set it as “linux_amd64:host”. The label will determine whether runner runs in docker or local. For more details, just go to and read official document.

    1
    2
    3
    su - git
    cd gitea_actions
    nohup ./act_runner -c config.yaml daemon &

    Then go to Gitea runner web page, you will find your runner online.

Install hexo for user git

1
2
3
4
5
su - git
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
nvm install node # "node" is an alias for the latest version
nvm use node # using the latest version node in all shell
npm install -g hexo-cli

Deploy actions in your blog repo

At first, maybe you need to delete the hook in Gitea server if you deploy your blog following my old tutorial. The path is /var/lib/gitea/data/gitea-repositories/doublefire.chen/hexo.git/hooks/post-receive.d/post-receive, just delete or make it empty.

Open your terminal locally and switch to your blog repo.

1
2
3
4
mkdir .gitea
mkdir .gitea/workflows
cd .gitea/workflows
vim ci-cd.yml # you need to replace some info as yours.
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
name: Blog CI & CD

on:
push:
branches:
- main

env:
BARE_REPO_DIR: /var/lib/gitea/data/gitea-repositories/doublefire.chen/hexo.git # use your repo path
CLONED_REPO_DIR: /var/tmp/blog
DEPLOYED_DIR: /var/www/blog # use your path of web server

jobs:
build:
name: Build blog
runs-on: linux_amd64
steps:
- name: Clone
run: |
rm -rf $CLONED_REPO_DIR
mkdir $CLONED_REPO_DIR
git clone $BARE_REPO_DIR $CLONED_REPO_DIR

- name: Build
run: |
cd $CLONED_REPO_DIR
source /home/git/.bashrc
hexo clean
hexo generate -c 1 # this limitation for maximum concurrent quantity is very important to avoid out of memory

deploy:
name: Deploy blog
runs-on: linux_amd64
needs: Build blog
steps:
- name: Replace
run: |
rm -rf $DEPLOYED_DIR/*
cp -rf $CLONED_REPO_DIR/public/* $DEPLOYED_DIR/

- name: Clean
run: |
rm -rf $CLONED_REPO_DIR

Configure your web server

I am using nginx so I will give a example of nginx.

Log in your Gitea server as root.

1
2
3
4
5
6
7
apt update
apt install nginx
apt install python3-certbot-nginx
apt install certbot
rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default
vim /etc/nginx/sites-available/aka.cy # you can use another name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
listen [::]:80;
server_name aka.cy;

# Redirect all HTTP requests to HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/aka.cy/fullchain.pem; #comment these lines before get a certificate
ssl_certificate_key /etc/letsencrypt/live/aka.cy/privkey.pem;
server_name aka.cy;
root /var/www/blog;
index index.html;
}
1
2
sudo certbot certonly --nginx -d aka.cy
service nginx restart

Then, you can just write and commit, push to remote Gitea server, and the Gitea actions will build and deploy your blog automatically.

Reference

  1. https://blog.shipengx.com/archives/2577fd4c.html
  2. https://gitea.com/gitea/act_runner
  3. https://stackoverflow.com/questions/76998107/can-i-run-gitea-actions-witout-docker
  4. https://docs.gitea.com/next/usage/actions/act-runner#labels
  5. https://hexo.io/zh-cn/docs/commands#generate