Skip to content

v0.1.2: Agent 无窗口后台运行优化 #10

v0.1.2: Agent 无窗口后台运行优化

v0.1.2: Agent 无窗口后台运行优化 #10

Workflow file for this run

# ===================================
# API Monitor Go Agent Build CI/CD
# ===================================
# 触发条件:
# - 推送到 main 分支(agent-go 目录有变更)
# - 创建版本标签(如 v1.0.0)
# - 手动触发(workflow_dispatch)
name: Build Go Agent
on:
push:
branches: [main]
tags: ['v*']
paths:
- 'agent-go/**'
- '.github/workflows/agent-build.yml'
workflow_dispatch:
inputs:
version:
description: 'Agent 版本号'
required: false
default: '2.0.0'
permissions:
contents: write
env:
GO_VERSION: '1.21'
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- goos: linux
goarch: amd64
suffix: ''
- goos: linux
goarch: arm64
suffix: ''
- goos: windows
goarch: amd64
suffix: '.exe'
- goos: darwin
goarch: amd64
suffix: ''
- goos: darwin
goarch: arm64
suffix: ''
steps:
# 检出代码
- name: Checkout repository
uses: actions/checkout@v4
# 设置 Go 环境
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: agent-go/go.sum
# 获取版本号
- name: Get version
id: version
run: |
if [ "${{ github.event.inputs.version }}" != "" ]; then
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
else
echo "version=2.0.0-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
fi
# 构建 Agent
- name: Build Agent
working-directory: agent-go
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
CGO_ENABLED: 0
run: |
OUTPUT_NAME="agent-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }}"
# 统一编译参数,使用运行时 -b 参数控制后台模式
go build -ldflags="-s -w -X main.VERSION=${{ steps.version.outputs.version }}" -o "../dist/${OUTPUT_NAME}"
echo "Built: ${OUTPUT_NAME}"
# 上传构建产物
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: agent-${{ matrix.goos }}-${{ matrix.goarch }}
path: dist/agent-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }}
retention-days: 30
# 创建 Release(仅在 tag 推送时)
release:
needs: build
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Prepare release assets
run: |
mkdir -p release
find artifacts -type f -exec cp {} release/ \;
ls -la release/
- name: Create Release
uses: softprops/action-gh-release@v1
with:
files: release/*
generate_release_notes: true
body: |
## API Monitor Agent v${{ github.ref_name }}
### 下载
| 平台 | 架构 | 文件 |
|------|------|------|
| Linux | amd64 | `agent-linux-amd64` |
| Linux | arm64 | `agent-linux-arm64` |
| Windows | amd64 | `agent-windows-amd64.exe` |
| macOS | amd64 | `agent-darwin-amd64` |
| macOS | arm64 | `agent-darwin-arm64` |
### 使用方法
```bash
# 下载
wget https://github.com/${{ github.repository }}/releases/download/${{ github.ref_name }}/agent-linux-amd64
chmod +x agent-linux-amd64
# 运行
./agent-linux-amd64 --id <SERVER_ID> -k <AGENT_KEY> -s <SERVER_URL>
```
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 输出构建摘要
summary:
needs: build
runs-on: ubuntu-latest
steps:
- name: Build Summary
run: |
echo "## 🚀 Go Agent 构建成功" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 构建平台" >> $GITHUB_STEP_SUMMARY
echo "| 平台 | 架构 |" >> $GITHUB_STEP_SUMMARY
echo "|------|------|" >> $GITHUB_STEP_SUMMARY
echo "| Linux | amd64 |" >> $GITHUB_STEP_SUMMARY
echo "| Linux | arm64 |" >> $GITHUB_STEP_SUMMARY
echo "| Windows | amd64 |" >> $GITHUB_STEP_SUMMARY
echo "| macOS | amd64 |" >> $GITHUB_STEP_SUMMARY
echo "| macOS | arm64 |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 下一步" >> $GITHUB_STEP_SUMMARY
echo "- 在 Actions 页面下载构建产物" >> $GITHUB_STEP_SUMMARY
echo "- 或创建 Release 自动发布" >> $GITHUB_STEP_SUMMARY