mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-14 14:17:11 +00:00
fix(ci): harden release automation workflows
This commit is contained in:
1 parent
2ee87a4532
commit
5616c68471
6 files changed
+223
-119
No files matched your search
+102
-50
@@ -26,8 +26,8 @@ log_info() { echo -e "${GREEN}[i]${NC} $1"; }
|
||||
log_warn() { echo -e "${YELLOW}[!]${NC} $1"; }
|
||||
log_error() { echo -e "${RED}[X]${NC} $1"; }
|
||||
|
||||
# Ensure we have the latest tags
|
||||
git fetch --tags origin main
|
||||
# Ensure we have the latest tags and branch refs
|
||||
git fetch --tags origin main dev
|
||||
|
||||
# Get latest stable tag from main (exclude prereleases like -dev, -beta, -rc)
|
||||
# Match only clean semver tags: vX.Y.Z
|
||||
@@ -39,58 +39,100 @@ if [ -z "$STABLE_TAG" ]; then
|
||||
fi
|
||||
|
||||
STABLE=${STABLE_TAG#v}
|
||||
CURRENT_VERSION=$(jq -r '.version' package.json)
|
||||
HEAD_SUBJECT=$(git log -1 --pretty=%s 2>/dev/null || echo "")
|
||||
DEV_VERSION_REGEX="^${STABLE//./\\.}-dev\\.[0-9]+$"
|
||||
PREVIOUS_DEV_TAG=""
|
||||
RECOVERY_MODE=false
|
||||
|
||||
log_info "Current stable version: ${STABLE}"
|
||||
|
||||
# Find latest dev tag for this stable version
|
||||
LATEST_DEV=$(git tag -l "v${STABLE}-dev.*" --sort=-v:refname | head -1 || echo "")
|
||||
if [[ "$CURRENT_VERSION" =~ $DEV_VERSION_REGEX ]] && [[ "$HEAD_SUBJECT" == "chore(release): ${CURRENT_VERSION} [skip ci]" ]]; then
|
||||
VERSION="$CURRENT_VERSION"
|
||||
CURRENT_TAG="v${VERSION}"
|
||||
PREVIOUS_DEV_TAG=$(git tag -l "v${STABLE}-dev.*" --sort=-v:refname | grep -vx "$CURRENT_TAG" | head -1 || echo "")
|
||||
RECOVERY_MODE=true
|
||||
log_warn "Recovery mode for ${VERSION}"
|
||||
|
||||
# Calculate next dev number
|
||||
if [ -z "$LATEST_DEV" ]; then
|
||||
DEV_NUM=1
|
||||
log_info "No existing dev tags for ${STABLE}, starting at dev.1"
|
||||
if git rev-parse "${CURRENT_TAG}" >/dev/null 2>&1; then
|
||||
TAG_COMMIT=$(git rev-parse "${CURRENT_TAG}^{commit}")
|
||||
HEAD_COMMIT=$(git rev-parse HEAD)
|
||||
if [[ "$TAG_COMMIT" != "$HEAD_COMMIT" ]]; then
|
||||
log_error "Tag ${CURRENT_TAG} exists but does not point to HEAD"
|
||||
exit 1
|
||||
fi
|
||||
log_info "Reusing existing tag ${CURRENT_TAG}"
|
||||
else
|
||||
git tag "${CURRENT_TAG}"
|
||||
log_warn "Recreated missing tag ${CURRENT_TAG} on release commit"
|
||||
fi
|
||||
else
|
||||
DEV_NUM=$(echo "$LATEST_DEV" | sed 's/.*dev\.\([0-9]*\)/\1/')
|
||||
DEV_NUM=$((DEV_NUM + 1))
|
||||
log_info "Latest dev tag: ${LATEST_DEV}, incrementing to dev.${DEV_NUM}"
|
||||
# Find latest dev tag for this stable version
|
||||
LATEST_DEV=$(git tag -l "v${STABLE}-dev.*" --sort=-v:refname | head -1 || echo "")
|
||||
PREVIOUS_DEV_TAG="$LATEST_DEV"
|
||||
|
||||
# Calculate next dev number
|
||||
if [ -z "$LATEST_DEV" ]; then
|
||||
DEV_NUM=1
|
||||
log_info "No existing dev tags for ${STABLE}, starting at dev.1"
|
||||
else
|
||||
DEV_NUM=$(echo "$LATEST_DEV" | sed 's/.*dev\.\([0-9]*\)/\1/')
|
||||
DEV_NUM=$((DEV_NUM + 1))
|
||||
log_info "Latest dev tag: ${LATEST_DEV}, incrementing to dev.${DEV_NUM}"
|
||||
fi
|
||||
|
||||
VERSION="${STABLE}-dev.${DEV_NUM}"
|
||||
CURRENT_TAG="v${VERSION}"
|
||||
log_info "New version: ${VERSION}"
|
||||
|
||||
# Check if tag already exists (safety check)
|
||||
if git rev-parse "${CURRENT_TAG}" >/dev/null 2>&1; then
|
||||
log_error "Tag ${CURRENT_TAG} already exists!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Update package.json
|
||||
npm version "$VERSION" --no-git-tag-version
|
||||
log_info "Updated package.json to ${VERSION}"
|
||||
|
||||
# Configure git for GitHub Actions
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
|
||||
# Commit version change
|
||||
git add package.json
|
||||
git commit -m "chore(release): ${VERSION} [skip ci]"
|
||||
log_info "Created release commit"
|
||||
|
||||
# Create tag
|
||||
git tag "${CURRENT_TAG}"
|
||||
log_info "Created tag ${CURRENT_TAG}"
|
||||
fi
|
||||
|
||||
VERSION="${STABLE}-dev.${DEV_NUM}"
|
||||
log_info "New version: ${VERSION}"
|
||||
|
||||
# Check if tag already exists (safety check)
|
||||
if git rev-parse "v${VERSION}" >/dev/null 2>&1; then
|
||||
log_error "Tag v${VERSION} already exists!"
|
||||
exit 1
|
||||
PACKAGE_NAME=$(jq -r '.name' package.json)
|
||||
if npm view "${PACKAGE_NAME}@${VERSION}" version >/dev/null 2>&1; then
|
||||
log_info "npm already has ${PACKAGE_NAME}@${VERSION}, skipping publish"
|
||||
else
|
||||
npm publish --tag dev
|
||||
log_info "Published to npm with @dev tag"
|
||||
fi
|
||||
|
||||
# Update package.json
|
||||
npm version "$VERSION" --no-git-tag-version
|
||||
log_info "Updated package.json to ${VERSION}"
|
||||
if git merge-base --is-ancestor HEAD origin/dev >/dev/null 2>&1; then
|
||||
log_info "origin/dev already contains release commit"
|
||||
else
|
||||
git push origin HEAD:dev
|
||||
log_info "Pushed release commit to origin/dev"
|
||||
fi
|
||||
|
||||
# Configure git for GitHub Actions
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
|
||||
# Commit version change
|
||||
git add package.json
|
||||
git commit -m "chore(release): ${VERSION} [skip ci]"
|
||||
log_info "Created release commit"
|
||||
|
||||
# Create tag
|
||||
git tag "v${VERSION}"
|
||||
log_info "Created tag v${VERSION}"
|
||||
|
||||
# Push commit and tag
|
||||
git push origin dev
|
||||
git push origin "v${VERSION}"
|
||||
log_info "Pushed to origin"
|
||||
|
||||
# Publish to npm with dev tag
|
||||
npm publish --tag dev
|
||||
log_info "Published to npm with @dev tag"
|
||||
if git ls-remote --exit-code --tags origin "refs/tags/${CURRENT_TAG}" >/dev/null 2>&1; then
|
||||
log_info "Remote tag ${CURRENT_TAG} already exists"
|
||||
else
|
||||
git push origin "${CURRENT_TAG}"
|
||||
log_info "Pushed tag ${CURRENT_TAG}"
|
||||
fi
|
||||
|
||||
# Generate release notes from commits since last tag
|
||||
PREV_TAG=$(git tag -l "v*" --sort=-v:refname | sed -n '2p' || echo "")
|
||||
PREV_TAG="${PREVIOUS_DEV_TAG:-$STABLE_TAG}"
|
||||
if [ -n "$PREV_TAG" ]; then
|
||||
# Get commits between previous tag and the one before our release commit
|
||||
NOTES=$(git log --pretty=format:"- %s" "${PREV_TAG}..HEAD~1" 2>/dev/null | grep -v "chore(release):" | head -15 || echo "- Dev release")
|
||||
@@ -99,12 +141,16 @@ else
|
||||
fi
|
||||
|
||||
# Create GitHub prerelease
|
||||
gh release create "v${VERSION}" \
|
||||
--title "v${VERSION}" \
|
||||
--notes "${NOTES}" \
|
||||
--prerelease
|
||||
if gh release view "${CURRENT_TAG}" >/dev/null 2>&1; then
|
||||
log_info "GitHub prerelease ${CURRENT_TAG} already exists"
|
||||
else
|
||||
gh release create "${CURRENT_TAG}" \
|
||||
--title "${CURRENT_TAG}" \
|
||||
--notes "${NOTES}" \
|
||||
--prerelease
|
||||
|
||||
log_info "Created GitHub prerelease"
|
||||
log_info "Created GitHub prerelease"
|
||||
fi
|
||||
|
||||
# Save release info for Discord notification
|
||||
# This file is read by send-discord-release.cjs for dev releases
|
||||
@@ -117,7 +163,13 @@ EOF
|
||||
log_info "Saved release info for Discord notification"
|
||||
|
||||
# Output for GitHub Actions
|
||||
echo "version=${VERSION}" >> "${GITHUB_OUTPUT:-/dev/null}"
|
||||
echo "released=true" >> "${GITHUB_OUTPUT:-/dev/null}"
|
||||
{
|
||||
echo "current_tag=${CURRENT_TAG}"
|
||||
echo "previous_dev_tag=${PREVIOUS_DEV_TAG}"
|
||||
echo "recovery_mode=${RECOVERY_MODE}"
|
||||
echo "released=true"
|
||||
echo "stable_tag=${STABLE_TAG}"
|
||||
echo "version=${VERSION}"
|
||||
} >> "${GITHUB_OUTPUT:-/dev/null}"
|
||||
|
||||
log_info "Dev release ${VERSION} complete!"
|
||||
Reference in new issue
Block a user