// TABLE OF CONTENTS
  1. GitHub Actions在GEO自动化中的定位
  2. 定时AI搜索检测Workflow搭建
  3. 结构化数据自动验证Workflow
  4. Sitemap自动更新与URL推送Workflow
  5. GEO效果报告自动生成Workflow
  6. Workflow维护与优化最佳实践
CHAPTER 01

GitHub Actions在GEO自动化中的定位

GitHub Actions不仅是代码CI/CD工具,其定时任务(schedule)和丰富的Actions生态使其成为GEO流程自动化的理想平台。相比传统cron调度,GitHub Actions无需维护服务器、自带日志记录、支持 Secrets管理API密钥、可与代码仓库联动触发。

GEO自动化中GitHub Actions的典型应用场景:定时执行AI搜索检测脚本(每日/每周自动检测品牌可见度)、内容发布时自动验证结构化数据(推送代码时触发Schema验证)、自动生成和更新sitemap.xml、自动向百度/Google提交新URL、定期生成GEO效果报告并推送通知。

优势分析:零服务器成本(GitHub免费额度每月2000分钟)、代码与自动化流程同仓库管理(版本可追溯)、Secrets加密存储API密钥(安全性好)、支持矩阵策略并行执行多平台检测、社区Actions生态丰富可复用。

自动化场景 触发方式 执行频率 所需资源
AI搜索检测 schedule定时 每日/每周 Python脚本+API密钥
结构化数据验证 push触发 每次提交 验证脚本
Sitemap更新 push触发 内容更新时 生成脚本
URL主动推送 push触发 内容发布时 百度/Google API
效果报告生成 schedule定时 每周 分析脚本+通知API
CHAPTER 02

定时AI搜索检测Workflow搭建

使用GitHub Actions的schedule触发器可以定时执行AI搜索检测脚本。Workflow配置文件放在仓库的.github/workflows/目录下,使用YAML格式定义。

Workflow设计要点:使用cron语法定义执行时间(注意GitHub Actions的cron使用UTC时区,北京时间需要减8小时)、使用matrix策略并行检测多个AI平台、通过Secrets注入API密钥和登录Cookie、检测结果以Artifact形式保存(保留30天)、检测失败时通过GitHub Issues或Slack通知。

环境准备:在GitHub仓库Settings→Secrets中配置所需的密钥(OPENAI_API_KEY、PROFOUND_API_KEY、SLACK_WEBHOOK等)。Python依赖通过requirements.txt安装。Playwright浏览器引擎通过playwright install命令安装。

example.py python
# .github/workflows/geo-detection.yml
name: GEO AI Search Detection

on:
  schedule:
    # 每天北京时间8:00执行(UTC 0:00)
    - cron: '0 0 * * *'
    # 每周一北京时间8:00执行全量检测
    - cron: '0 0 * * 1'
  workflow_dispatch:  # 支持手动触发

jobs:
  detect:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform: [chatgpt, perplexity, kimi, wenxin]
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          playwright install chromium --with-deps

      - name: Run detection
        env:
          PLATFORM: ${{ matrix.platform }}
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          BRAND_NAME: ${{ secrets.BRAND_NAME }}
          COOKIE_WENXIN: ${{ secrets.COOKIE_WENXIN }}
        run: |
          python -X utf8 run_detection.py --platform=$PLATFORM --mode=daily

      - name: Upload results
        uses: actions/upload-artifact@v4
        with:
          name: detection-results-${{ matrix.platform }}
          path: results/
          retention-days: 30

      - name: Notify on failure
        if: failure()
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
          SLACK_MESSAGE: 'GEO检测失败: ${{ matrix.platform }}'
          SLACK_COLOR: '#FF0000'
CHAPTER 03

结构化数据自动验证Workflow

每次向仓库推送新的内容页面时,应自动验证页面的结构化数据是否正确。这可以在内容发布前发现结构化数据错误,避免错误数据影响AI搜索引擎的内容理解。

验证内容:JSON-LD语法正确性(确保JSON格式无误)、Schema属性完整性(检查必填属性是否填写)、属性值合法性(检查URL格式、日期格式、枚举值是否正确)、结构化数据与页面内容一致性(检查schema中的标题/描述与页面显示是否一致)。

验证Workflow设计:在push事件触发时执行,仅检查变更的HTML文件(使用git diff获取变更文件列表)、使用Python脚本解析HTML中的JSON-LD并验证、验证失败时阻止合并(如果是PR)或创建Issue通知(如果是直接push)、验证通过后在PR中添加评论确认。

example.py python
# .github/workflows/validate-schema.yml
name: Validate Structured Data

on:
  pull_request:
    paths:
      - 'docs/**/*.html'
      - 'pages/**/*.html'
  push:
    paths:
      - 'docs/**/*.html'
      - 'pages/**/*.html'

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2  # 获取上一个commit用于diff

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: pip install beautifulsoup4 jsonschema

      - name: Get changed HTML files
        id: changed
        run: |
          FILES=$(git diff --name-only HEAD^ HEAD -- 'docs/*.html' 'pages/*.html' | tr '\n' ' ')
          echo "files=$FILES" >> $GITHUB_OUTPUT
          echo "Changed files: $FILES"

      - name: Validate structured data
        if: steps.changed.outputs.files != ''
        run: |
          python -X utf8 validate_schema.py ${{ steps.changed.outputs.files }}

      - name: Create issue on failure
        if: failure()
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: '结构化数据验证失败',
              body: '请检查最近提交的HTML文件中的JSON-LD结构化数据。\n\n提交: ' + context.sha,
              labels: ['bug', 'geo-validation']
            })

# validate_schema.py 核心逻辑
import sys
import json
from bs4 import BeautifulSoup

REQUIRED_ARTICLE_FIELDS = ['headline', 'datePublished', 'author']
REQUIRED_FAQ_FIELDS = ['mainEntity']

def validate_html(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        soup = BeautifulSoup(f, 'html.parser')

    scripts = soup.find_all('script', type='application/ld+json')
    if not scripts:
        print(f'WARNING: {filepath} - 无结构化数据')
        return True  # 无结构化数据不阻止,仅警告

    errors = []
    for script in scripts:
        try:
            data = json.loads(script.string)
            schema_type = data.get('@type', '')
            if schema_type == 'Article':
                for field in REQUIRED_ARTICLE_FIELDS:
                    if field not in data:
                        errors.append(f'Article缺少必填字段: {field}')
            elif schema_type == 'FAQPage':
                for field in REQUIRED_FAQ_FIELDS:
                    if field not in data:
                        errors.append(f'FAQPage缺少必填字段: {field}')
        except json.JSONDecodeError as e:
            errors.append(f'JSON-LD语法错误: {e}')

    if errors:
        print(f'FAIL: {filepath}')
        for e in errors:
            print(f'  - {e}')
        return False
    print(f'PASS: {filepath}')
    return True
CHAPTER 04

Sitemap自动更新与URL推送Workflow

当网站内容更新时,需要同步更新sitemap.xml并主动向搜索引擎提交新URL。这个过程可以通过GitHub Actions在内容推送时自动完成。

Sitemap自动更新:检测到docs/目录下有新增或修改的HTML文件时,自动重新生成sitemap.xml,包括更新URL列表和lastmod日期。生成后自动提交到仓库并推送到部署服务器。

URL主动推送:向百度站长平台和Google Search Console提交新增URL。百度使用主动推送API(POST方式批量提交URL),Google使用Indexing API(需要OAuth认证)。推送频率应合理控制,避免频繁推送相同URL。

部署联动:sitemap更新后,通过FTP/API自动部署到线上服务器。如果使用CDN,需要触发CDN缓存刷新。部署完成后自动验证sitemap.xml的可访问性(HTTP 200检查)。

example.py python
# .github/workflows/update-sitemap.yml
name: Update Sitemap & Push URLs

on:
  push:
    paths:
      - 'docs/**/*.html'
    branches:
      - main

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Get changed HTML files
        id: changed
        run: |
          FILES=$(git diff --name-only HEAD^ HEAD -- 'docs/*.html' | tr '\n' ' ')
          echo "files=$FILES" >> $GITHUB_OUTPUT

      - name: Generate sitemap
        if: steps.changed.outputs.files != ''
        run: python -X utf8 generate_sitemap.py --base-url=https://your-domain.com

      - name: Commit sitemap
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'
          git add sitemap.xml
          git diff --quiet HEAD sitemap.xml || git commit -m 'Auto-update sitemap'
          git push

      - name: Push URLs to Baidu
        if: steps.changed.outputs.files != ''
        env:
          BAIDU_PUSH_TOKEN: ${{ secrets.BAIDU_PUSH_TOKEN }}
        run: |
          python -X utf8 push_urls_baidu.py --files='${{ steps.changed.outputs.files }}' \
            --token=$BAIDU_PUSH_TOKEN

      - name: Deploy via FTP
        if: steps.changed.outputs.files != ''
        env:
          FTP_HOST: ${{ secrets.FTP_HOST }}
          FTP_USER: ${{ secrets.FTP_USER }}
          FTP_PASS: ${{ secrets.FTP_PASS }}
        run: |
          python -X utf8 deploy_ftp.py --files='${{ steps.changed.outputs.files }}' \
            --sitemap=sitemap.xml
CHAPTER 05

GEO效果报告自动生成Workflow

每周自动生成GEO效果报告并推送到团队通知渠道,是保持团队对GEO效果持续关注的有效手段。GitHub Actions可以在指定时间自动执行报告生成脚本。

报告内容包含:本周核心指标摘要(提及率、AI搜索流量、引用页面数)、与上周和上月的对比变化、各AI平台效果对比、Top10最佳/最差关键词、本周异常事件汇总、下周优化建议。

报告生成流程:从数据库或JSONL文件中拉取本周检测数据→计算核心指标和对比变化→使用matplotlib生成趋势图表→使用Python将指标和图表组装为HTML格式报告→通过邮件或Slack推送给团队。

报告推送渠道:邮件推送(使用Python smtplib发送HTML邮件,附带图表附件)、Slack推送(通过Slack Webhook发送摘要消息+报告链接)、GitHub Issues(在仓库中创建Issue存档报告,便于历史查阅)。

example.py python
# .github/workflows/weekly-report.yml
name: Weekly GEO Report

on:
  schedule:
    # 每周一北京时间9:00(UTC 1:00)
    - cron: '0 1 * * 1'
  workflow_dispatch:

jobs:
  report:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          pip install pandas matplotlib jinja2

      - name: Download last week's detection results
        uses: actions/download-artifact@v4
        with:
          name: detection-results-chatgpt
          path: results/
        continue-on-error: true

      - name: Generate weekly report
        env:
          BRAND_NAME: ${{ secrets.BRAND_NAME }}
        run: |
          python -X utf8 generate_weekly_report.py --brand="$BRAND_NAME" \
            --output=weekly_report.html

      - name: Upload report
        uses: actions/upload-artifact@v4
        with:
          name: weekly-geo-report
          path: weekly_report.html
          retention-days: 90

      - name: Send to Slack
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        run: |
          python -X utf8 send_slack_report.py --file=weekly_report.html \
            --webhook=$SLACK_WEBHOOK

      - name: Create GitHub Issue
        uses: actions/github-script@v7
        with:
          script: |
            const date = new Date().toISOString().split('T')[0];
            github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: `GEO周报 ${date}`,
              body: '周报已生成,请查看Artifact: weekly-geo-report',
              labels: ['report', 'geo']
            })
CHAPTER 06

Workflow维护与优化最佳实践

GitHub Actions Workflow需要持续维护以保持稳定运行。常见问题包括:定时任务执行延迟(GitHub Actions的schedule不保证准时执行,高峰期可能延迟30-60分钟)、Actions版本过期(使用的第三方Actions需要定期更新版本)、Secrets过期(API密钥和Cookie需要定期更新)。

维护清单:每月检查Workflow执行成功率(低于90%需排查原因)、每季度更新Actions版本(使用最新稳定版)、每半年审查Secrets有效性(特别是Cookie类Secrets会过期)、定期优化执行时间(减少不必要步骤降低Actions使用时长)。

成本优化:GitHub Actions免费额度每月2000分钟(私有仓库),定时检测任务每月约消耗300-500分钟(每日1次×30天×约2分钟/次)。如果超出免费额度,可以优化为仅工作日执行或减少检测关键词数量。公共仓库不消耗免费额度。

安全最佳实践:Secrets不打印在日志中(使用::add-mask::屏蔽敏感输出)、Workflow使用最小权限原则(permissions字段限制GITHUB_TOKEN权限)、第三方Actions使用前审查其代码(特别是有write权限的Actions)、定期审查Workflow运行历史检查异常执行。

Workflow优化清单

使用缓存加速依赖安装(actions/cache缓存pip包)

使用矩阵策略并行执行多平台检测

合并相似的Workflow减少执行次数

使用条件执行避免不必要的步骤

定期清理旧Artifact释放存储空间

监控Actions使用时长避免超出免费额度