Release Bella OpenAPI #200
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Release Bella OpenAPI | |
| on: | |
| push: | |
| tags: | |
| - 'v*.*.*' | |
| workflow_run: | |
| workflows: ["Create Release Tag"] | |
| types: | |
| - completed | |
| branches: | |
| - main | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Get tag version | |
| id: get-tag | |
| run: | | |
| if [[ "${{ github.event_name }}" == "push" && "${{ github.ref_type }}" == "tag" ]]; then | |
| # 直接从标签推送事件中获取标签名 | |
| TAG="${{ github.ref_name }}" | |
| else | |
| # 从仓库中获取最新的标签 | |
| git fetch --tags | |
| TAG=$(git tag -l "v*" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) | |
| echo "Retrieved latest tag: $TAG" | |
| fi | |
| VERSION=$TAG | |
| echo "tag=$TAG" >> $GITHUB_OUTPUT | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Using tag and version: $TAG" | |
| - name: Set up JDK 8 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '8' | |
| distribution: 'temurin' | |
| cache: 'maven' | |
| server-id: central | |
| server-username: CENTRAL_USERNAME | |
| server-password: CENTRAL_TOKEN | |
| gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} | |
| gpg-passphrase: MAVEN_GPG_PASSPHRASE | |
| - name: Check if version exists in Maven Central | |
| id: version-check | |
| run: | | |
| cd api | |
| VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) | |
| GROUP_ID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout) | |
| # 将点号替换为斜杠,用于构建 URL 路径 | |
| GROUP_PATH=${GROUP_ID//./\/} | |
| echo "Current version: $VERSION, GroupId: $GROUP_ID, GroupPath: $GROUP_PATH" | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| # 检查 Parent POM 是否已发布 | |
| PARENT_URL="https://repo1.maven.org/maven2/${GROUP_PATH}/bella-openapi/$VERSION/bella-openapi-$VERSION.pom" | |
| echo "Checking Parent POM URL: $PARENT_URL" | |
| if curl -s --head --fail "$PARENT_URL" &>/dev/null; then | |
| echo "Parent POM version $VERSION already exists in Maven Central" | |
| echo "SKIP_MAVEN_PUBLISH=true" >> $GITHUB_ENV | |
| exit 0 | |
| else | |
| echo "Parent POM version $VERSION does not exist in Maven Central" | |
| fi | |
| # 检查 SDK 模块是否已发布 | |
| SDK_URL="https://repo1.maven.org/maven2/${GROUP_PATH}/sdk/$VERSION/sdk-$VERSION.jar" | |
| echo "Checking SDK URL: $SDK_URL" | |
| if curl -s --head --fail "$SDK_URL" &>/dev/null; then | |
| echo "SDK version $VERSION already exists in Maven Central" | |
| echo "SKIP_MAVEN_PUBLISH=true" >> $GITHUB_ENV | |
| exit 0 | |
| else | |
| echo "SDK version $VERSION does not exist in Maven Central" | |
| fi | |
| # 检查 SPI 模块是否已发布 | |
| SPI_URL="https://repo1.maven.org/maven2/${GROUP_PATH}/spi/$VERSION/spi-$VERSION.jar" | |
| echo "Checking SPI URL: $SPI_URL" | |
| if curl -s --head --fail "$SPI_URL" &>/dev/null; then | |
| echo "SPI version $VERSION already exists in Maven Central" | |
| echo "SKIP_MAVEN_PUBLISH=true" >> $GITHUB_ENV | |
| exit 0 | |
| else | |
| echo "SPI version $VERSION does not exist in Maven Central" | |
| fi | |
| # 如果所有检查都通过,设置为可以发布 | |
| echo "No existing versions found in Maven Central. Proceeding with publish." | |
| echo "SKIP_MAVEN_PUBLISH=false" >> $GITHUB_ENV | |
| - name: Publish to Central Portal | |
| id: maven-publish | |
| if: env.SKIP_MAVEN_PUBLISH != 'true' | |
| env: | |
| CENTRAL_USERNAME: ${{ secrets.CENTRAL_USERNAME }} | |
| CENTRAL_TOKEN: ${{ secrets.CENTRAL_TOKEN }} | |
| MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} | |
| run: | | |
| cd api | |
| mvn clean deploy -P release -pl sdk,spi -am -DskipTests -e | |
| echo "MAVEN_PUBLISHED=true" >> $GITHUB_ENV | |
| - name: Create SDK Tag | |
| if: env.MAVEN_PUBLISHED == 'true' | |
| run: | | |
| SDK_VERSION="${{ steps.version-check.outputs.version }}" | |
| SDK_TAG="sdk${SDK_VERSION}" | |
| # 配置 Git | |
| git config user.name "GitHub Actions Bot" | |
| git config user.email "actions@github.com" | |
| # 检查标签是否已存在 | |
| if git tag -l "$SDK_TAG" | grep -q "$SDK_TAG"; then | |
| echo "SDK tag $SDK_TAG already exists, skipping tag creation" | |
| else | |
| # 创建带注释的标签 | |
| git tag -a $SDK_TAG -m "SDK Release $SDK_VERSION" | |
| # 推送标签 | |
| git push origin $SDK_TAG | |
| echo "Successfully created and pushed SDK tag: $SDK_TAG" | |
| fi | |
| - name: Skip Maven Publish (Version already exists) | |
| if: env.SKIP_MAVEN_PUBLISH == 'true' | |
| run: | | |
| echo "Skipping Maven publish as version already exists in Maven Central" | |
| echo "Continuing with Docker image build and push" | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Login to Docker Registry | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Install docker-compose | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y docker-compose | |
| - name: Build and Push Docker images | |
| env: | |
| VERSION: ${{ steps.get-tag.outputs.version }} | |
| run: | | |
| ./start.sh --build --push --registry ${{ secrets.DOCKER_USERNAME }} --version $VERSION | |
| echo "DOCKER_PUBLISHED=true" >> $GITHUB_ENV | |
| echo "DOCKER_REGISTRY=${{ secrets.DOCKER_USERNAME }}" >> $GITHUB_ENV | |
| - name: Create GitHub Release | |
| if: success() | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: ${{ steps.get-tag.outputs.version }} | |
| release_name: Bella OpenAPI ${{ steps.get-tag.outputs.version }} | |
| body: | | |
| # Bella OpenAPI ${{ steps.get-tag.outputs.version }} | |
| ## SDK 信息 | |
| SDK 版本: ${{ steps.version-check.outputs.version }} | |
| ```xml | |
| <dependency> | |
| <groupId>top.bella</groupId> | |
| <artifactId>openapi-sdk</artifactId> | |
| <version>${{ steps.version-check.outputs.version }}</version> | |
| </dependency> | |
| ``` | |
| ## SPI 信息 | |
| SPI 版本: ${{ steps.version-check.outputs.version }} | |
| ```xml | |
| <dependency> | |
| <groupId>top.bella</groupId> | |
| <artifactId>openapi-spi</artifactId> | |
| <version>${{ steps.version-check.outputs.version }}</version> | |
| </dependency> | |
| ``` | |
| ## Docker 镜像 | |
| ### API 服务 | |
| ``` | |
| docker pull bellatop/bella-openapi-api:${{ steps.get-tag.outputs.version }} | |
| ``` | |
| ### Web 服务 | |
| ``` | |
| docker pull bellatop/bella-openapi-web:${{ steps.get-tag.outputs.version }} | |
| ``` | |
| 支持的平台: linux/amd64, linux/arm64 | |
| draft: false | |
| prerelease: false | |
| deploy: | |
| name: Deploy to Server | |
| runs-on: ubuntu-latest | |
| needs: publish | |
| if: success() | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup deployment parameters | |
| id: params | |
| run: | | |
| echo "server_ip=${{ secrets.DEPLOY_SERVER_IP }}" >> $GITHUB_OUTPUT | |
| echo "target_directory=${{ secrets.DEPLOY_TARGET_DIR }}" >> $GITHUB_OUTPUT | |
| echo "deploy_command=${{ secrets.DEPLOY_COMMAND }}" >> $GITHUB_OUTPUT | |
| - name: Install sshpass | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y sshpass | |
| - name: Setup SSH known hosts | |
| run: | | |
| mkdir -p ~/.ssh | |
| ssh-keyscan -H ${{ steps.params.outputs.server_ip }} >> ~/.ssh/known_hosts | |
| - name: Test SSH connection | |
| run: | | |
| sshpass -p "${{ secrets.DEPLOY_SSH_PASSWORD }}" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_SSH_USER }}@${{ steps.params.outputs.server_ip }} "echo 'SSH connection successful'" | |
| - name: Deploy to server | |
| run: | | |
| sshpass -p "${{ secrets.DEPLOY_SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_SSH_USER }}@${{ steps.params.outputs.server_ip }} << 'EOF' | |
| set -e | |
| echo "=== Bella OpenAPI Deployment Started ===" | |
| echo "Target directory: ${{ steps.params.outputs.target_directory }}" | |
| echo "Deploy command: ${{ steps.params.outputs.deploy_command }}" | |
| # Navigate to target directory | |
| cd ${{ steps.params.outputs.target_directory }} | |
| echo "Current directory: $(pwd)" | |
| # Show directory contents for debugging | |
| echo "Directory contents:" | |
| ls -la | |
| # Pull latest code | |
| echo "Pulling latest code..." | |
| git pull | |
| # Execute deployment command | |
| echo "Executing deployment command..." | |
| ${{ steps.params.outputs.deploy_command }} | |
| echo "=== Deployment completed successfully ===" | |
| EOF | |
| - name: Notify deployment status | |
| if: always() | |
| run: | | |
| if [ ${{ job.status }} == 'success' ]; then | |
| echo "✅ Deployment to ${{ steps.params.outputs.server_ip }} completed successfully" | |
| else | |
| echo "❌ Deployment to ${{ steps.params.outputs.server_ip }} failed" | |
| exit 1 | |
| fi |