Featured image of post 在Gitea中使用workflows实现持续部署

在Gitea中使用workflows实现持续部署

从Gitea 1.19版本开始,Gitea Actions成为了内置的CI/CD解决方案。

Gitea Actions与GitHub Actions相似且兼容,actions基本都可以复用

安装 Runner

1
2
wget https://dl.gitea.com/act_runner/0.2.13/act_runner-0.2.13-linux-amd64 -O ./act_runner
./act_runner generate-config > config.yaml

注册

  • 在Gitea中获取token:管理后台 -> Actions -> Runners -> Create new Runner,可以获取token
  • Gitea instance URL, 注意使用不要localhost,容器会请求Gitea仓库,可以使用内网IP
1
2
3
4
5
# 注册
./act_runner register
# 运行容器
echo "nohup ./act_runner daemon --config config.yaml > ./runner.log 2>&1 &" > start.sh
chmod +x start.sh && ./start.sh

workflow

支持部署到多台机器

 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
name: Build ❗Formal blog and Deploy  
  
on:  
  push:  
    branches:  
      - main  
  pull_request:  
    branches:  
      - main  
env:  
  OUTPUT_DIR: hugo_output  
  PUBLISH_DIR: hugo_output
  # 远端目录   
  LINODE_PUBLISH_DIR: ${{ secrets.M1_REMOTE_TARGET }}/${{ env.PUBLISH_DIR }}  
  
jobs:  
  build:  
    # https://gitea.com/gitea/runner-images  
    runs-on: ubuntu-latest  
    env:  
      HUGO_CACHEDIR: /tmp/hugo_cache # <- Define the env variable here, so that Hugo's cache dir is now predictible in your workflow and doesn't depend on the Hugo's version you're using.  
  
    steps:  
      - run: echo "🎉 任务已被 ${{ gitea.event_name }} 事件自动触发。"  
      - run: echo "🔎 您的分支是 ${{ gitea.ref }},仓库是 ${{ gitea.repository }}。"  
      - run: echo "👤 此次提交由 ${{ gitea.actor }} 完成。"  
  
      - name: Checkout Repository  
        uses: actions/checkout@v4  
        with:  
          submodules: true  
  
      # 增加 Hugo 模块缓存  
      - uses: actions/cache@v4  
        with:  
          path: ${{ env.HUGO_CACHEDIR }} # <- Use the same env variable just right here  
          key: ${{ runner.os }}-hugomod-${{ hashFiles('**/go.sum') }}  
          restore-keys: |  
            ${{ runner.os }}-hugomod-  
  
      - name: Validate Environment Variables  
        run: |  
          if [ -z "${{ env.OUTPUT_DIR }}" ] || [ -z "${{ env.LINODE_PUBLISH_DIR }}" ]; then  
            echo "❌ 错误:目录没有设置!"  
            echo "OUTPUT_DIR: ${{ env.OUTPUT_DIR }}"  
            echo "LINODE_PUBLISH_DIR: ${{ env.LINODE_PUBLISH_DIR }}"  
            exit 1  
          fi  
          echo "✅ 环境变量验证通过"  
          echo "OUTPUT_DIR: ${{ env.OUTPUT_DIR }}"  
          echo "M1_PUBLISH_DIR: ${{ env.LINODE_PUBLISH_DIR }}"
  
      - name: Install Hugo  
        uses: peaceiris/actions-hugo@v2  
        with:  
          hugo-version: 'latest'  
          extended: true  
          with-deps: true  
  
      - name: Clear Previous Build Output  
        run: |  
          mkdir -p ${{ env.OUTPUT_DIR }}  
          rm -rf ${{ env.OUTPUT_DIR }}/*  
  
      - name: Build Hugo Site  
        run: hugo --minify --destination ${{ env.OUTPUT_DIR }} --cleanDestinationDir  
  
#      - name: Install rsync  
#        run: sudo apt-get update && sudo apt-get install -y rsync  
#  
#      - name: Deploy Hugo Site  
#        run: |  
#          mkdir -p ${{ env.PUBLISH_DIR }}  
#          rsync -av --delete --checksum --no-times ${{ env.OUTPUT_DIR }}/ ${{ env.PUBLISH_DIR }}/  
      - name: Deploy to Linode  
        uses: easingthemes/ssh-deploy@main  
        with:  
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}  
          ARGS: "-rlgoDzvc -i --delete"  
          SOURCE: ${{ env.OUTPUT_DIR }}/*  
          REMOTE_HOST: ${{ secrets.LINODE_REMOTE_HOST }}  
          REMOTE_PORT: ${{ secrets.LINODE_REMOTE_PORT }}  
          REMOTE_USER: ${{ secrets.REMOTE_USER }}  
          TARGET: ${{ env.LINODE_PUBLISH_DIR }}  
          EXCLUDE: "/dist/, /node_modules/"  
  
      - name: List files in the repository  
        run: |  
          ls ${{ gitea.workspace }}  
      - run: echo "🍏 执行结束,此任务的状态是 ${{ job.status }}。"

Ubuntu换源

部署阿里云,如果慢的话,可以替换一下源,有点效果不大

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
jobs:  
  build:  
    steps:
      - name: Configure Aliyun Mirror  
        run: |  
          sudo tee /etc/apt/sources.list.d/ubuntu.sources >/dev/null <<EOF  
          Types: deb  
          URIs: http://mirrors.cloud.aliyuncs.com/ubuntu/  
          Suites: noble noble-updates noble-backports  
          Components: main restricted universe multiverse  
          Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg  
  
          Types: deb  
          URIs: http://mirrors.cloud.aliyuncs.com/ubuntu/  
          Suites: noble-security  
          Components: main restricted universe multiverse  
          Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg  
          EOF  
  
          echo "✅ 已配置阿里云内网镜像源"  
          sudo apt-get update

VPS配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 本地生成密钥
ssh-keygen -t ed25519 -C "xx@xx.com"

# 创建账号
useradd -m -s /bin/bash git

# 上传公钥
su git
mkdir -p ~/.ssh && echo "ssh-ed25519 xxxx" > ~/.ssh/authorized_keys
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

#验证
ssh git@xxx -i ~/.keys/git

参考文档

主题 StackJimmy 设计