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

Deploy action runner on same server as Gitea

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

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 (unless you upload your ssh public key to Gitea to grant access shown below), it is recommend to run action runner using user git.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    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
    ./act_runner --config config.yaml register # In new version 0.2.11, you need to register by running this command
    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.

Deploy action runner on another server

The biggest difference here is that we need to grant access to remote repo as the git user in other server does not have access to a private repo. For safety concern, I only give read permission to another server holding action runner.

Log in the server you want deploy action runner as user git

1
ssh-keygen -t ed25519 # generate a new ssh key for git

Go to your repo webpage and create a new deploy keys using your ssh public key. Other steps is the same as before.

Screenshot 2024-12-21 at 22.18.19

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
name: Blog CI & CD

on:
push:
branches:
- main

env:
REPO_URL: git@your.domain:user/Hexo.git # or /var/lib/gitea/data/gitea-repositories/doublefire.chen/hexo.git, it depends on how you deploy action server, and you need to git clone to /home/git/Hexo for initiation
CLONED_REPO_DIR: /home/git/Hexo
DEPLOYED_DIR: /var/www/blog

jobs:
build:
name: Build blog
runs-on: linux_amd64
steps:
- name: Pull
run: |
cd $CLONED_REPO_DIR
git pull origin main

- name: Build
run: |
cd $CLONED_REPO_DIR
source /home/git/.bashrc
hexo clean
hexo generate
- name: Deploy
run: |
cp -rf $CLONED_REPO_DIR/public/* $DEPLOYED_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
  6. https://docs.gitea.com/usage/actions/act-runner