ci: add release workflow
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
name: Release
|
||||
|
||||
# 发版规则:dev 合并到 master,且合并请求标题为 vX.X.X
|
||||
# 标题不是 vX.X.X 时只做构建验证,不发布
|
||||
# 也支持手动触发,按 package.json 版本发布,用于失败后重发
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
types:
|
||||
- closed
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: act_runner_node
|
||||
if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }}
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Resolve release version
|
||||
id: version
|
||||
run: |
|
||||
pkg_version=$(node -p "require('./package.json').version")
|
||||
echo "package.json 版本:$pkg_version"
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
echo "手动触发,按 package.json 版本发布"
|
||||
echo "version=$pkg_version" >> $GITHUB_OUTPUT
|
||||
echo "publish=true" >> $GITHUB_OUTPUT
|
||||
exit 0
|
||||
fi
|
||||
title="${{ github.event.pull_request.title }}"
|
||||
echo "PR 标题:$title"
|
||||
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
|
||||
env:
|
||||
NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }}
|
||||
NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
|
||||
run: |
|
||||
if [ -z "$NEXUS_USERNAME" ] || [ -z "$NEXUS_PASSWORD" ]; then
|
||||
echo "FAIL: 缺少 secrets.NEXUS_USERNAME 或 secrets.NEXUS_PASSWORD"
|
||||
exit 1
|
||||
fi
|
||||
auth=$(printf '%s:%s' "$NEXUS_USERNAME" "$NEXUS_PASSWORD" | base64 | tr -d '\n')
|
||||
{
|
||||
echo "registry=https://nexus.imyeyu.com/repository/npm-group/"
|
||||
echo "//nexus.imyeyu.com/repository/npm-group/:_auth=${auth}"
|
||||
echo "//nexus.imyeyu.com/repository/npm-group/:always-auth=true"
|
||||
echo "//nexus.imyeyu.com/repository/npm-timi/:_auth=${auth}"
|
||||
echo "//nexus.imyeyu.com/repository/npm-timi/:always-auth=true"
|
||||
} > ~/.npmrc
|
||||
echo "OK: nexus 认证已配置"
|
||||
|
||||
- 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-web-api@${{ steps.version.outputs.version }}" version > /dev/null 2>&1; then
|
||||
echo "FAIL: timi-web-api@${{ 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: v${{ steps.version.outputs.version }}
|
||||
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
|
||||
Reference in New Issue
Block a user