Skip to content
name: Release on Commit
on:
push:
branches:
- main
workflow_dispatch: # 允许手动触发
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 拉取代码
uses: actions/checkout@v4
- name: 安装node
uses: actions/setup-node@v4
with:
node-version: 21
registry-url: "https://registry.npmjs.org"
- name: 安装pnpm
uses: pnpm/action-setup@v2
with:
version: 9.7.1
- name: 安装依赖
run: |
pnpm install
pnpm add node-karin@latest
- name: 放弃 package.json 的本地更改
run: |
git checkout HEAD package.json
- name: 删除 pnpm-lock.yaml
run: rm -rf pnpm-lock.yaml
- name: 编译
run: pnpm build
- name: 获取当前提交哈希
run: echo "COMMIT_HASH=${{ github.sha }}" >> $GITHUB_ENV
id: get_commit_hash
- name: 获取短哈希值
run: echo "SHORT_COMMIT_HASH=$(echo ${{ env.COMMIT_HASH }} | cut -c1-7)" >> $GITHUB_ENV
- name: 创建临时文件夹存储编译后的lib文件夹和resources文件夹
run: |
mkdir ./../temp
- name: 复制编译后的lib文件夹和resources文件夹到临时文件夹
run: |
cp -r ./lib ./../temp/lib
cp -r ./resources ./../temp/resources
- name: 检查并创建空的 build 分支
run: |
git fetch origin
# 检查 build 分支是否存在
if git show-ref --verify --quiet refs/heads/build; then
echo "Build branch exists locally. Switching to it."
git checkout build
else
# 检查远程是否有 build 分支
if git ls-remote --exit-code --heads origin build; then
echo "Build branch exists remotely. Fetching and switching to it."
git checkout build
else
# build 分支不存在,创建一个空的 build 分支
echo "Build branch does not exist. Creating an empty build branch."
git checkout --orphan build # 创建一个没有历史记录的空分支
git reset --hard # 重置所有文件
git clean -fdx # 删除未被 Git 管理的文件和文件夹
echo "Empty build branch created."
git commit --allow-empty -m "Initial empty commit on build branch"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} build # 推送到远程仓库
fi
fi
- name: 切换到 build 分支
run: |
git fetch origin build
git checkout build
- name: 删除build分支上的lib文件夹和resources文件夹
run: |
rm -rf lib
rm -rf resources
- name: 复制编译后的lib文件夹和resources文件夹到 build 分支
run: |
cp -r ./../temp/lib ./
cp -r ./../temp/resources ./
- name: 删除临时文件夹
run: |
rm -rf ./../temp
- name: 从main分支复制文件到build分支
run: |
git checkout main -- package.json CHANGELOG.md README.md config resources LICENSE
- name: 修订版本号
run: npm run commit all
if: success()
- name: 删除依赖文件夹 node_modules
run: |
rm -rf node_modules
- name: 将文件整理到 karin-plugin-kkk 文件夹
run: |
mkdir -p karin-plugin-kkk
find . -path "./.git" -prune -o -type f -print | while read file; do
mkdir -p "karin-plugin-kkk/$(dirname "$file")"
cp "$file" "karin-plugin-kkk/$file"
done
- name: 创建 build.zip
run: zip -r "build-${{ env.SHORT_COMMIT_HASH }}.zip" karin-plugin-kkk
- name: 上传 build.zip
uses: actions/upload-artifact@v4
with:
name: "build-${{ env.SHORT_COMMIT_HASH }}.zip"
path: "build-${{ env.SHORT_COMMIT_HASH }}.zip"
- name: 删除 karin-plugin-kkk 文件夹
run: rm -rf karin-plugin-kkk
- name: 发布到 npm
run: npm run pub-beta
if: success()
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: 创建发行版
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "pre-${{ env.SHORT_COMMIT_HASH }}"
release_name: "${{ env.PKG_VERSION }} Preview-Release"
body: |
### 🎉 🎉 🎉构建完成!
- 此版本是基于快照 [`${{ env.SHORT_COMMIT_HASH }}`](https://github.com/${{ github.repository }}/tree/${{ env.COMMIT_HASH }}) 所构建的,[**点击此处查看更改**](https://github.com/${{ github.repository }}/commit/${{ env.COMMIT_HASH }}) By [**${{ github.actor }}**](https://github.com/${{ github.actor }})
构建时间:${{ github.event.head_commit.timestamp }}
draft: false
prerelease: true
- name: 上传
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: "./build-${{ env.SHORT_COMMIT_HASH }}.zip"
asset_name: "build-${{ env.SHORT_COMMIT_HASH }}.zip"
asset_content_type: application/zip