update(article): 使用Gitea Actions+1Panel自动构建并部署Go项目

This commit is contained in:
mei 2025-08-27 15:46:25 +08:00
parent e854d3a0b9
commit e897cd0c4a
8 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,137 @@
---
title: 使用Gitea Actions+1Panel自动构建并部署Go项目
tags:
- 学习笔记
- linux
- Gitea
- 1Panel
categories:
- Linux
index_img: /img/2025/gitea-actions-1panel-go/actions.png
banner_img: /img/2025/gitea-actions-1panel-go/actions.png
permalink: /articles/2025/gitea-actions-1panel-go.html
date: 2025-08-27 15:38:45
---
Gitea 和 1Panel 都非常好用Gitea 提供了可以自托管的 Actions 功能1Panel 在最近的版本中提供了api功能用这些功能可以很方便的将 Go 项目快速自动构建并部署上线(其他语言的项目也可以,不过会稍微复杂一些)
### 1. 为 Gitea 添加 runner
1. 在 Gitea 的`管理后台-Actions-Runners`中点击"创建Runner",复制注册令牌备用
![runner](/img/2025/gitea-actions-1panel-go/runner.png)
2. 选择一台服务器来安装 Runner,你可以直接在1Panel的应用商店安装act runner,也可以单独购买一台服务器来安装runner,这里推荐购买一台海外服务器,可以解决第三方库下载缓慢的问题
在安装runner时配置中的注册令牌是刚刚在gitea中获取到的令牌运行器名称随便起一个就行运行器标签可以参照 [Gitea 文档](https://docs.gitea.com/usage/actions/act-runner#labels),一般使用默认的就行
runner安装完成后在刚刚获取注册令牌的页面检查runner是否上线
### 2. 获取1Panel API令牌
在 1Panel 的面板设置中开启 API 接口接口有效期推荐填写0,即不不验证有效期ip白名单可以按需填写然后将接口密钥复制备用
![1panel-api-key](/img/2025/gitea-actions-1panel-go/1panel-api-key.png)
然后将接口密钥设置为 Gitea 仓库的 Secret
![gitea-secret](/img/2025/gitea-actions-1panel-go/gitea-secret.png)
顺便把面板地址也配置为Secret:`PANEL_URL`(后面不带`/`,不加安全人口),比如`https://1p.mmeiblog.cn:33333`
### 3. 编写 Actions
Gitea Actions 的语法与 Github Actions 的语法基本一样只是有部分特性不被支持Gitea Actions 也支持使用 Github 上的 Actions 仓库,语法与 Github Actions 一样
创建`.gitea/workflows/release.yml`文件,这里我将 Secret 设置为构建时的环境变量来使用Go版本可以自己设置,这里**把`APP_NAME`的值换成你的应用名称,这很重要**
```yaml
name: Go Build and Release
on:
push
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.23"
- name: Get dependencies
run: go mod tidy
- name: Build project
run: go build -o main
- name: Update Server by 1panel
env:
PANEL_API_KEY: ${{ secrets.PANEL_API_KEY }}
PANEL_URL: ${{ secrets.PANEL_URL }}
APP_NAME: "your-app-name"
run: chmod +x script/update.sh && ./script/update.sh
```
### 4. 创建运行环境
先创建一个应用目录:`mkdir /opt/apps/your-app-name`,应用名称自己替换为上一步中的`APP_NAME`
然后在 1Panel 的`网站-运行环境-Go`中创建运行环境(最好加上时区的环境变量),启动命令为`chmod +x main && ./main`
![go-runtime](/img/2025/gitea-actions-1panel-go/go-runtime.png)
然后点击F12开发者工具点击网络然后点一下面板中应用的"重启",可以看到开发者工具中出现了一个名称为`operate`的请求,在负载中找到`ID`,记下备用
![1p-request](/img/2025/gitea-actions-1panel-go/1p-request.png)
### 编写上传脚本
创建脚本:
```bash
# 先切换到项目根目录下
mkdir script
cd script && touch update.sh
```
这里我在actions中运行一个bash脚本你也可以把他塞进actions中不过那样比较丑然后按照注释**替换id为上一步中拿到的ID**
```bash
#!/bin/bash
# 把这里的ID替换为上一步中拿到的ID
id=6
# generate 1panel token
clientToken=$PANEL_API_KEY
timestamp=$(date +%s)
input="1panel$clientToken$timestamp"
token=$(echo -n "$input" | md5sum | awk '{print $1}')
# delete file
curl --location --request POST "$PANEL_URL/api/v2/files/del?operateNode=undefined" \
-H "1Panel-Token: $token" \
-H "1Panel-Timestamp: $timestamp" \
--header 'Content-Type: application/json' \
--data-raw '{"path":"/opt/apps/'"$APP_NAME"'/main","isDir":false,"forceDelete":true}'
# updaload file
curl --location --request POST "$PANEL_URL/api/v2/files/upload" \
-H "1Panel-Token: $token" \
-H "1Panel-Timestamp: $timestamp" \
--form 'path=/opt/apps/'"$APP_NAME" \
--form 'file=@main'
# restart runtime
curl --location --request POST "$PANEL_URL/api/v2/runtimes/operate" \
-H "1Panel-Token: $token" \
-H "1Panel-Timestamp: $timestamp" \
--header 'Content-Type: application/json' \
--data-raw '{
"ID": '"$id"',
"operate": "restart"
}'
```
这样在每次提交时actions就会自动构建并部署应用到你的服务器上
![actions](/img/2025/gitea-actions-1panel-go/actions.png)
在最后吐槽一下1panel的api文档参数就写一半剩下一半参数自己猜

View File

@ -73,6 +73,7 @@ sticky: 99
| 狐蒂云 | 743forever.com | szhdy.com | 有 | 有 | B1-20242378 | | 狐蒂云 | 743forever.com | szhdy.com | 有 | 有 | B1-20242378 |
| spaceship cdn | supersonic.ai | spaceship.com | 无 | 无 | 无 | | spaceship cdn | supersonic.ai | spaceship.com | 无 | 无 | 无 |
| 浪浪云 | langcdn.top </br> llyvps.top | langlangy.cn | 有 | 有 | B1-20253161 | | 浪浪云 | langcdn.top </br> llyvps.top | langlangy.cn | 有 | 有 | B1-20253161 |
| 嘿嘿CDN | asiaheidns.cn | cdn.munianxi.cn | 有 | 无 | 无 |
## 详情 ## 详情

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 279 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB