尝试集成CI/CD进行测试 #26
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: Java CI with Maven, Nacos, and Python | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| jobs: | |
| unit-tests: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 20 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| cache: maven | |
| - name: Run Maven Tests | |
| run: | | |
| set -euo pipefail | |
| mvn -B -ntp clean test | |
| - name: Upload Surefire Reports | |
| if: always() | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: surefire-reports | |
| path: | | |
| **/target/surefire-reports/*.txt | |
| **/target/surefire-reports/*.xml | |
| go-client-check: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 10 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version-file: go_client/go.mod | |
| cache-dependency-path: go_client/go.sum | |
| - name: Verify Go Client Build | |
| working-directory: go_client | |
| run: | | |
| set -euo pipefail | |
| go mod download | |
| go test ./... | |
| go build -o /tmp/go-rpc-client main.go | |
| integration-test: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 30 | |
| needs: [unit-tests, go-client-check] | |
| services: | |
| nacos: | |
| image: nacos/nacos-server:v2.2.3 | |
| env: | |
| MODE: standalone | |
| AUTH_ENABLE: false | |
| ports: | |
| - 8848:8848 | |
| - 9848:9848 | |
| # Wait for Nacos to be fully ready | |
| options: >- | |
| --name nacos-server | |
| --health-cmd "curl -f http://localhost:8848/nacos/v1/console/health/readiness" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 10 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| cache: maven | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.x' | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version-file: go_client/go.mod | |
| cache-dependency-path: go_client/go.sum | |
| - name: Install Python Dependencies | |
| run: | | |
| set -euo pipefail | |
| python -m pip install --upgrade pip | |
| pip install grpcio grpcio-tools protobuf | |
| - name: Build Jars For Integration Test | |
| run: | | |
| set -euo pipefail | |
| mvn -B -ntp -DskipTests clean package | |
| - name: Wait For Nacos Ports | |
| run: | | |
| set -euo pipefail | |
| echo "Waiting for Nacos HTTP API on 8848..." | |
| timeout 60s bash -c 'until curl -sf http://localhost:8848/nacos/v1/console/health/readiness > /dev/null; do sleep 1; done' | |
| echo "Waiting for Nacos gRPC port on 9848..." | |
| timeout 60s bash -c 'until echo > /dev/tcp/localhost/9848; do sleep 1; done' | |
| echo "Nacos 8848/9848 are both reachable." | |
| - name: Start RPC Provider (Java Server) | |
| run: | | |
| set -euo pipefail | |
| # Build classpath including project jars and dependencies | |
| CLASSPATH="rpc-provider/target/rpc-provider-1.0-SNAPSHOT.jar:rpc-transport-netty/target/rpc-transport-netty-1.0-SNAPSHOT.jar:rpc-core/target/rpc-core-1.0-SNAPSHOT.jar:rpc-common/target/rpc-common-1.0-SNAPSHOT.jar:rpc-api/target/rpc-api-1.0-SNAPSHOT.jar:$(mvn -q dependency:build-classpath -Dmdep.outputFile=/dev/stdout -pl rpc-provider -am)" | |
| echo "Starting Java Server..." | |
| echo "Provider protocol override: rpc.protocol=grpc" | |
| # Run in background with nohup | |
| nohup java -Drpc.protocol=grpc -cp "$CLASSPATH" com.xiaoyu.rpc.provider.ProviderApp > server.log 2>&1 & | |
| # Wait for server port 8080 to be available | |
| echo "Waiting for Java Server to start on port 8080..." | |
| timeout 60s bash -c 'until echo > /dev/tcp/localhost/8080; do sleep 1; done' | |
| echo "Java Server is running!" | |
| # Optional: Print initial logs | |
| head -n 20 server.log | |
| - name: Wait For Service Registration In Nacos | |
| run: | | |
| set -euo pipefail | |
| echo "Waiting for HelloService registration..." | |
| if timeout 60s bash -c 'until grep -q "Service registered: com.xiaoyu.rpc.api.HelloService" server.log; do sleep 1; done'; then | |
| echo "HelloService registration confirmed." | |
| else | |
| echo "Service registration wait timed out. Recent server logs:" | |
| tail -n 120 server.log || true | |
| exit 1 | |
| fi | |
| - name: Run Multi-Client Chains In Parallel | |
| run: | | |
| set -euo pipefail | |
| mkdir -p .ci-status | |
| CLASSPATH="rpc-consumer/target/rpc-consumer-1.0-SNAPSHOT.jar:rpc-transport-netty/target/rpc-transport-netty-1.0-SNAPSHOT.jar:rpc-core/target/rpc-core-1.0-SNAPSHOT.jar:rpc-common/target/rpc-common-1.0-SNAPSHOT.jar:rpc-api/target/rpc-api-1.0-SNAPSHOT.jar:$(mvn -q dependency:build-classpath -Dmdep.outputFile=/dev/stdout -pl rpc-consumer -am)" | |
| run_java() { | |
| echo "Running Java Client (grpc)..." | |
| if java -Drpc.protocol=grpc -Drpc.serializer=protobuf -cp "$CLASSPATH" com.xiaoyu.rpc.consumer.ConsumerApp | tee java-client.log \ | |
| && grep -q "Result1:" java-client.log \ | |
| && grep -q "Result2:" java-client.log; then | |
| echo "PASS" > .ci-status/java.status | |
| else | |
| echo "FAIL" > .ci-status/java.status | |
| fi | |
| } | |
| run_python() { | |
| echo "Running Python Client..." | |
| if (cd python_client && python client.py) | tee python-client.log \ | |
| && grep -q "Message: Success" python-client.log; then | |
| echo "PASS" > .ci-status/python.status | |
| else | |
| echo "FAIL" > .ci-status/python.status | |
| fi | |
| } | |
| run_go() { | |
| echo "Running Go Client..." | |
| if (cd go_client && go run main.go) | tee go-client.log \ | |
| && grep -q "Message: Success" go-client.log; then | |
| echo "PASS" > .ci-status/go.status | |
| else | |
| echo "FAIL" > .ci-status/go.status | |
| fi | |
| } | |
| run_java & | |
| PID_JAVA=$! | |
| run_python & | |
| PID_PY=$! | |
| run_go & | |
| PID_GO=$! | |
| wait "${PID_JAVA}" || true | |
| wait "${PID_PY}" || true | |
| wait "${PID_GO}" || true | |
| - name: Aggregate Multi-Client Results | |
| run: | | |
| set -euo pipefail | |
| JAVA_RESULT=$(cat .ci-status/java.status 2>/dev/null || echo "FAIL") | |
| PY_RESULT=$(cat .ci-status/python.status 2>/dev/null || echo "FAIL") | |
| GO_RESULT=$(cat .ci-status/go.status 2>/dev/null || echo "FAIL") | |
| echo "Multi-client chain summary:" | |
| echo " Java : ${JAVA_RESULT}" | |
| echo " Python : ${PY_RESULT}" | |
| echo " Go : ${GO_RESULT}" | |
| if [[ "${JAVA_RESULT}" != "PASS" || "${PY_RESULT}" != "PASS" || "${GO_RESULT}" != "PASS" ]]; then | |
| echo "At least one client chain failed." | |
| exit 1 | |
| fi | |
| echo "All three client chains passed." | |
| - name: Upload CI Logs | |
| if: always() | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ci-logs | |
| path: | | |
| server.log | |
| java-client.log | |
| python-client.log | |
| go-client.log | |
| .ci-status/*.status |