Skip to content

修复: 解决action问题 #15

修复: 解决action问题

修复: 解决action问题 #15

name: Build and Release
on:
push:
branches: [ main, master ]
# 在推送标签时也触发
tags:
- 'v*' # 匹配格式如 v1.0.0 的标签
# 允许手动触发工作流
workflow_dispatch:
# 添加权限配置
permissions:
contents: write # Needed for checkout and for softprops/action-gh-release to create releases
pages: write # Needed for actions/deploy-pages
id-token: write # Needed for actions/deploy-pages
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 设置Node.js环境
uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'
- name: 修复npm依赖问题
run: |
rm -rf package-lock.json node_modules
npm install
- name: 设置私钥
run: node scripts/setup-private-key.js
env:
EXTENSION_PRIVATE_KEY: ${{ secrets.EXTENSION_PRIVATE_KEY }}
- name: 构建扩展
run: npm run build:extension
- name: 创建ZIP包
run: npm run build:zip
- name: 创建CRX包
run: npm run build:crx
- name: 创建发布文件
run: npm run build:release
- name: 上传构建产物 (整个dist目录)
uses: actions/upload-artifact@v4
with:
name: built-extension-dist # Artifact包含整个dist目录结构
path: dist/ # 上传dist目录及其所有内容
# 构建和部署GitHub Pages
deploy-pages:
needs: build
runs-on: ubuntu-latest
# 确保在主分支推送或手动触发时运行,但不在标签推送时运行
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') || github.event_name == 'workflow_dispatch'
# 使用GitHub Pages环境
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: 检出代码
uses: actions/checkout@v4 # Checkout is fine here, not strictly necessary if only using artifacts
- name: 下载构建产物 (built-extension-dist)
uses: actions/download-artifact@v4
with:
name: built-extension-dist
path: temp-download # 下载到 temp-download, 里面会有 dist 目录
- name: 准备页面内容
run: |
mkdir -p docs/website/assets/images
mkdir -p docs/website/assets/videos
echo "检查下载的构建产物路径:"
ls -R temp-download
# 源文件在 temp-download/dist/release
SOURCE_RELEASE_DIR="temp-download/dist/release"
if [ -d "$SOURCE_RELEASE_DIR" ]; then
cp -r "$SOURCE_RELEASE_DIR"/* docs/website/ || echo "从 $SOURCE_RELEASE_DIR 复制文件失败"
VERSION_INFO_PATH="docs/website/version-info.json"
if [ -f "$VERSION_INFO_PATH" ]; then
VERSION=$(cat "$VERSION_INFO_PATH" | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[", ]//g')
else
echo "警告: $VERSION_INFO_PATH 未找到,使用默认版本。"
VERSION="0.0.0"
fi
cp "docs/website/pro-color-v${VERSION}.zip" docs/website/pro-color-latest.zip || echo "创建 pro-color-latest.zip 失败"
cp "docs/website/pro-color-v${VERSION}.crx" docs/website/pro-color-latest.crx || echo "创建 pro-color-latest.crx 失败"
# version.json 已经被 cp -r 复制过去了
else
echo "错误: 源目录 $SOURCE_RELEASE_DIR 未找到!"
exit 1
fi
# 配置GitHub Pages
- name: 设置Pages
id: pages
uses: actions/configure-pages@v4
# 打包页面内容
- name: 打包页面内容
uses: actions/upload-pages-artifact@v3
with:
path: docs/website
# 部署到GitHub Pages
- name: 部署到GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
# 只在标签推送时进行发布
release:
needs: build
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4 # For git log in changelog
- name: 下载构建产物 (built-extension-dist)
uses: actions/download-artifact@v4
with:
name: built-extension-dist
path: ./ # 下载到工作区根目录, 会创建 dist/release, dist/zip 等
- name: 获取版本号
id: get_version
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
- name: 创建更新日志
id: changelog
run: |
VERSION=${{ steps.get_version.outputs.VERSION }}
echo "## Pro Color 浏览器扩展 v${VERSION}" > CHANGELOG.md
echo "" >> CHANGELOG.md
echo "发布日期: $(date +'%Y-%m-%d')" >> CHANGELOG.md
echo "" >> CHANGELOG.md
echo "### 更新内容" >> CHANGELOG.md
echo "" >> CHANGELOG.md
# 如果有上一个标签,显示自上一个标签以来的提交
PREV_TAG=$(git tag --sort=-v:refname | grep -A 1 "v${VERSION}" | tail -n 1)
if [ ! -z "$PREV_TAG" ] && [ "$PREV_TAG" != "v${VERSION}" ]; then
echo "自 ${PREV_TAG} 以来的更新:" >> CHANGELOG.md
git log ${PREV_TAG}..HEAD --pretty=format:"- %s" --reverse >> CHANGELOG.md
else
echo "- 首次发布" >> CHANGELOG.md
fi
# 将更新日志内容设置为步骤输出
CHANGELOG_CONTENT=$(cat CHANGELOG.md)
echo "CHANGELOG_BODY<<EOF" >> $GITHUB_OUTPUT # Use a different name for output
echo "$CHANGELOG_CONTENT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: 创建GitHub Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ github.ref }}
name: Pro Color v${{ steps.get_version.outputs.VERSION }}
body: ${{ steps.changelog.outputs.CHANGELOG_BODY }}
draft: false
prerelease: false
files: |
dist/release/pro-color-v${{ steps.get_version.outputs.VERSION }}.zip
dist/release/pro-color-v${{ steps.get_version.outputs.VERSION }}.crx
dist/release/version-info.json