name: CI on: push: branches: [main] pull_request: branches: [main] workflow_dispatch: env: REGISTRY: forgejo.riotpiao.com IMAGE: forgejo.riotpiao.com/rock/api-gateway DOCKER_HOST: tcp://localhost:2375 jobs: ci: name: CI runs-on: golang steps: - name: Install Node.js and Docker run: | apt-get update apt-get install -y nodejs docker.io - name: Checkout code uses: actions/checkout@v4 - name: Go vet run: go vet ./... - name: Go test run: go test ./... - name: Get short SHA id: sha run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - name: Registry login run: | echo "${REGISTRY_TOKEN}" | docker login "${REGISTRY}" \ --username "${REGISTRY_USER}" --password-stdin env: REGISTRY_USER: ${{ secrets.FORGEJO_REGISTRY_USER }} REGISTRY_TOKEN: ${{ secrets.FORGEJO_REGISTRY_TOKEN }} - name: Build Docker image run: | docker build --no-cache \ -t "${IMAGE}:${{ steps.sha.outputs.short_sha }}" \ -f Dockerfile . echo "Built image: ${IMAGE}:${{ steps.sha.outputs.short_sha }}" - name: Push test image (SHA tag only, not latest yet) run: | docker push "${IMAGE}:${{ steps.sha.outputs.short_sha }}" echo "✓ Pushed test image: ${IMAGE}:${{ steps.sha.outputs.short_sha }}" - name: Setup kubeconfig for integration test job run: | mkdir -p ~/.kube echo "${KUBECONFIG_B64}" | base64 -d > ~/.kube/config env: KUBECONFIG_B64: ${{ secrets.KUBECONFIG_B64 }} continue-on-error: true - name: Run integration tests via Kubernetes Job run: | echo "Running integration tests via Kubernetes Job..." echo "Image: ${IMAGE}:${{ steps.sha.outputs.short_sha }}" # Apply job template from repo kubectl apply -f k8s/integration-test-job.yaml # Update job to use new image kubectl set image job/api-gateway-integration-test \ api-gateway="${IMAGE}:${{ steps.sha.outputs.short_sha }}" \ -n api --record # Wait for job to complete (max 5 minutes) echo "Waiting for job to complete..." kubectl wait --for=condition=complete job/api-gateway-integration-test \ -n api --timeout=5m || true # Stream logs echo "Job logs:" kubectl logs -n api job/api-gateway-integration-test --all-containers=true --timestamps=true || true # Check if job succeeded SUCCEEDED=$(kubectl get job api-gateway-integration-test -n api -o jsonpath='{.status.succeeded}') FAILED=$(kubectl get job api-gateway-integration-test -n api -o jsonpath='{.status.failed}') echo "" echo "Job Status: Succeeded=$SUCCEEDED, Failed=$FAILED" if [ "$SUCCEEDED" = "1" ]; then echo "✓ Integration tests PASSED" exit 0 else echo "✗ Integration tests FAILED" exit 1 fi continue-on-error: false - name: Promote image to latest (only if tests passed) if: success() run: | docker pull "${IMAGE}:${{ steps.sha.outputs.short_sha }}" docker tag "${IMAGE}:${{ steps.sha.outputs.short_sha }}" "${IMAGE}:latest" docker push "${IMAGE}:latest" echo "✓ Promoted ${IMAGE}:${{ steps.sha.outputs.short_sha }} to latest" - name: Cleanup integration test job if: always() run: | echo "Cleaning up test job..." kubectl delete job api-gateway-integration-test -n api --ignore-not-found=true continue-on-error: true - name: Cleanup docker if: always() run: docker image prune -a --force 2>&1 | tail -3 || true