ci: add release workflow

This commit is contained in:
Timi
2026-11-05 17:24:43 +08:00
parent 947ae4cac2
commit e71744df5a
2 changed files with 125 additions and 0 deletions
+124
View File
@@ -0,0 +1,124 @@
name: Release
# 发版规则:dev 合并到 master,且合并请求标题为 vX.X.X
# 标题不是 vX.X.X 时只做构建验证,不发布
on:
pull_request:
branches:
- master
types:
- closed
jobs:
release:
runs-on: act_runner_node
if: ${{ github.event.pull_request.merged == true }}
env:
NEXUS_AUTH: ${{ secrets.NEXUS_AUTH }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Resolve release version
id: version
run: |
title="${{ github.event.pull_request.title }}"
pkg_version=$(node -p "require('./package.json').version")
echo "PR 标题:$title"
echo "package.json 版本:$pkg_version"
if echo "$title" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
if [ "v$pkg_version" != "$title" ]; then
echo "FAIL: 标题 $title 与 package.json 版本 $pkg_version 不一致"
exit 1
fi
echo "version=$pkg_version" >> $GITHUB_OUTPUT
echo "publish=true" >> $GITHUB_OUTPUT
else
echo "标题不是 vX.X.X,仅做构建验证,不发布"
echo "publish=false" >> $GITHUB_OUTPUT
fi
- name: Configure nexus auth
run: |
if [ -z "$NEXUS_AUTH" ]; then
echo "FAIL: 缺少 secrets.NEXUS_AUTH"
exit 1
fi
{
echo "//nexus.imyeyu.com/repository/npm-group/:_auth=${NEXUS_AUTH}"
echo "//nexus.imyeyu.com/repository/npm-group/:always-auth=true"
echo "//nexus.imyeyu.com/repository/npm-timi/:_auth=${NEXUS_AUTH}"
echo "//nexus.imyeyu.com/repository/npm-timi/:always-auth=true"
} > ~/.npmrc
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Type check
run: pnpm run check
- name: Build
run: pnpm run build
- name: Ensure version not published
if: ${{ steps.version.outputs.publish == 'true' }}
run: |
if npm view "timi-ts@${{ steps.version.outputs.version }}" version > /dev/null 2>&1; then
echo "FAIL: timi-ts@${{ steps.version.outputs.version }} 已存在于 nexus,请提升 package.json 版本"
exit 1
fi
echo "OK: 版本未占用"
- name: Publish to nexus
if: ${{ steps.version.outputs.publish == 'true' }}
run: npm publish --registry=https://nexus.imyeyu.com/repository/npm-timi/
- name: Create release
if: ${{ steps.version.outputs.publish == 'true' }}
env:
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
GITEA_SERVER_URL: ${{ github.server_url }}
GITEA_INTERNAL_URL: ${{ vars.TIMI_GITEA_INTERNAL_URL }}
GITEA_REPOSITORY: ${{ github.repository }}
RELEASE_TAG: ${{ github.event.pull_request.title }}
RELEASE_TARGET: ${{ github.sha }}
run: |
if [ -z "$GITEA_TOKEN" ]; then
echo "Missing secrets.RUNNER_TOKEN"
exit 1
fi
if [ -n "$GITEA_INTERNAL_URL" ]; then
api_base_url="$GITEA_INTERNAL_URL"
else
api_base_url="$GITEA_SERVER_URL"
fi
api_url="$api_base_url/api/v1/repos/$GITEA_REPOSITORY/releases"
payload=$(cat <<EOF
{
"tag_name": "$RELEASE_TAG",
"name": "$RELEASE_TAG",
"target_commitish": "$RELEASE_TARGET",
"draft": false,
"prerelease": false
}
EOF
)
echo "Creating release $RELEASE_TAG at $RELEASE_TARGET"
http_code=$(curl -sS -w "%{http_code}" -o /tmp/release.json -X POST "$api_url" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
--connect-timeout 30 --max-time 60 -d "$payload") || http_code="000"
echo "HTTP Status: $http_code"
if [ "$http_code" = "201" ]; then
echo "OK: Release created"
elif [ "$http_code" = "409" ]; then
echo "OK: Release already exists"
else
cat /tmp/release.json 2>/dev/null || true
echo "FAIL: 创建 release 失败"
exit 1
fi
+1
View File
@@ -0,0 +1 @@
registry=https://nexus.imyeyu.com/repository/npm-group/