安装Gitea/Caddy

基于docker:

官方文档 https://docs.gitea.com/zh-cn/installation/install-with-docker

手动创建docker网络

docker network create my-net

新建docker-compose.yml,其中networks 部分定义了一个外部网络 my-net,并将其设置为 external: true,表示这个网络是由 Docker Compose 之外的其他方式创建的

version: "3"

networks:
  my-net:
    external: true

services:
  gitea:
    image: gitea/gitea:1.21.4
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
    restart: always
    networks:
      - my-net
    volumes:
      - ./gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "222:22"
  caddy:
  	image: caddy:latest
  	container_name: caddy
  	restart: always
    networks:
      - my-net
    volumes:
      - ./caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./caddy/site:/site
    ports:
      - "80:80"
      - "443:443"

Act_runner实现持续部署

参考官方文档:

Act Runner https://docs.gitea.com/usage/actions/act-runner

注意network要与gitea的network匹配,通过compose创建的network会基于当前文件夹加上前缀

docker run \
	-e GITEA_INSTANCE_URL=http://gitea:3000/ \
	-e GITEA_RUNNER_REGISTRATION_TOKEN=token \
	-e GITEA_RUNNER_NAME=blog-runner \
  -e GITEA_RUNNER_LABELS=ubuntu-22.04:host \
	-v /usr/local/moac/caddy/site:/site \
	-v /usr/local/moac/repo_blog:/repo_blog \
	--name my_runner \
	--network=my-net \
	-d gitea/act_runner:nightly

.gitignore

# ---> Hugo
# Generated files by hugo
public/
resources/_gen/
assets/jsconfig.json
hugo_stats.json

# Executable may be added to repository
hugo
hugo.exe
hugo.darwin
hugo.linux

# Temporary lock file while building
.hugo_build.lock

工作流

创建位于仓库根目录的.gitea/workflows路径,新建demo.yaml文件

注意推送的branche分支是什么,一定要push到对应的分支上才能触发工作流

name: Blog CI & CD

on:
  push:
    branches:
      - master
      - main

env:
  # repo所在路径
  REPO_DIR: /repo_blog/blog
  # hugo所在路径
  HUGO_DIR: /repo_blog/blog/blog
  # hugo生成静态资源存放路径
  SITE: /site/public

jobs:
  build:
    name: Build_blog
    runs-on: ubuntu-22.04
    steps:
      - name: pull
      # pull远程仓库
        run: |
          cd $REPO_DIR && git pull
                              
  deploy:
    name: Deploy_blog
    runs-on: ubuntu-22.04
    needs: Build_blog
    steps:
      - name: Deploy
      # $REPO_DIR/blog/hugo -d $REPO_DIR/blog
        run: |
          cd $HUGO_DIR && ./hugo -d $SITE                    

Caddy静态文件服务器

Caddyfile 其中gitea:3000 使用的是容器名:端口形式

gitea.moac.run {
        reverse_proxy gitea:3000
        encode gzip
}
moac.run www.moac.run {
        root * /site/public/
        encode gzip
        file_server
}