Ph 209/rate limit controller #709
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: Build | |
| on: | |
| push: | |
| branches: | |
| # do not run on main, the create-release workflow will to this by adding action:create-release to the commit message | |
| - '140-*' | |
| - 'PH-120/*' | |
| - 'PH-99/*' | |
| - 'PH-131/*' | |
| - 'PH-133/*' | |
| - 'PH-101/*' | |
| - 'PH-196/*' | |
| - 'PH-198/*' | |
| - 'PH-201/*' | |
| - 'PH-205/*' | |
| - 'PH-209/*' | |
| pull_request: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| inputs: | |
| run_integration_tests: | |
| description: 'Run integration tests?' | |
| default: 'false' | |
| required: false | |
| permissions: | |
| contents: read | |
| packages: write | |
| jobs: | |
| go-test: | |
| uses: ./.github/workflows/go-test.yaml | |
| secrets: inherit | |
| ko-build: | |
| # Run this job on push events and pull requests | |
| if: github.event_name == 'push' | |
| needs: [go-test] | |
| uses: metraction/github-actions/.github/workflows/ko-build.yaml@main | |
| secrets: inherit | |
| push-helm-chart: | |
| uses: metraction/github-actions/.github/workflows/helm-push.yaml@main | |
| needs: [ko-build] | |
| with: | |
| chart_dir: helm/pharos | |
| chart_name: pharos | |
| destination: . | |
| registry: oci://ghcr.io/metraction/charts | |
| secrets: inherit | |
| get-version: | |
| if: github.event.inputs.run_integration_tests == 'true' || contains(github.event.head_commit.message, 'testit') || contains(github.event.head_commit.message, 'testbe') || contains(github.ref, 'integration-test') | |
| runs-on: ubuntu-latest | |
| outputs: | |
| version: ${{ steps.version.outputs.version }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get version from semver.sh | |
| id: version | |
| run: | | |
| VERSION=$(curl -sSL "https://raw.githubusercontent.com/metraction/github-actions/refs/heads/main/scripts/semver.sh" | bash) | |
| VERSION=$(echo "$VERSION" | sed 's/+/_/g') | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Using version: $VERSION" | |
| integration-test: | |
| if: github.event.inputs.run_integration_tests == 'true' || contains(github.event.head_commit.message, 'testit') || contains(github.ref, 'integration-test') | |
| needs: [get-version, ko-build] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read # Required for checkout | |
| packages: read # Required to pull images from GHCR | |
| services: | |
| redis: | |
| image: redis:6.2-alpine | |
| ports: | |
| - 6379:6379 | |
| options: >- | |
| --health-cmd "redis-cli ping" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| postgres: | |
| image: postgres:15-alpine | |
| ports: | |
| - 5432:5432 | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| env: | |
| POSTGRES_DB: pharos | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| scanner: | |
| # The image is tagged with the version from semver.sh | |
| image: ghcr.io/metraction/pharos/pharos:${{ needs.get-version.outputs.version }} | |
| env: | |
| PHAROS_REDIS_DSN: redis:6379 | |
| PHAROS_SCANNER_CACHEENDPOINT: redis://redis:6379 | |
| publisher: | |
| # The image is tagged with the version from semver.sh | |
| image: ghcr.io/metraction/pharos/pharos:${{ needs.get-version.outputs.version }} | |
| ports: | |
| - 8080:8080 | |
| env: | |
| PHAROS_REDIS_DSN: redis:6379 | |
| PHAROS_SCANNER_CACHEENDPOINT: redis://redis:6379 | |
| PHAROS_DATABASE_DSN: postgres://postgres:postgres@postgres:5432/pharos?sslmode=disable | |
| PHAROS_COMMAND: http | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.23' | |
| - name: Wait for services to be healthy | |
| run: | | |
| echo "Waiting for services to initialize..." | |
| sleep 15 # Adjust as needed, or implement a more robust health check for the scanner | |
| - name: Run http integration tests | |
| run: | | |
| # # Wait for HTTP service to be ready | |
| echo "Waiting for HTTP service to be available..." | |
| timeout 15 bash -c 'until curl -s http://localhost:8080/api/docs; do sleep 1; done' | |
| # Send request and capture response | |
| echo "Sending test request to API..." | |
| response=$(curl -s -w "\n%{http_code}" -X POST http://localhost:8080/api/pharosscantask/syncscan \ | |
| -H "Content-Type: application/json" \ | |
| -d '{ | |
| "jobId": "2", | |
| "status": "", | |
| "engine": "", | |
| "error": "", | |
| "authdsn": "registry:///?tlscheck=false", | |
| "imagespec": "rancher/nginx-ingress-controller:nginx-1.11.3-rancher1", | |
| "platform": "linux/amd64", | |
| "context": { | |
| "namespace": "ingress-nginx" | |
| }, | |
| "contextRootKey": "namespace=ingress-nginx,", | |
| "rxdigest": "", | |
| "rxplatform": "", | |
| "cachettl": 0, | |
| "scanttl": 60000000000, | |
| "created": "2025-07-02T10:20:35.784021+02:00", | |
| "updated": "2025-07-02T10:20:35.784021+02:00" | |
| }') | |
| # Extract status code | |
| status_code=$(echo "$response" | tail -n1) | |
| response_body=$(echo "$response" | sed '$d') | |
| # Check if request was successful | |
| echo "Response body: $response_body" | |
| echo "Status code: $status_code" | |
| if [[ "$status_code" -ge 200 && "$status_code" -lt 300 ]]; then | |
| echo "HTTP request successful" | |
| else | |
| echo "HTTP request failed with status $status_code" | |
| exit 1 | |
| fi | |
| - name: Run controller integration tests | |
| env: | |
| DATABASE_DSN: "postgres://postgres:postgres@localhost:5432?sslmode=disable" | |
| DATABASE_DRIVER: "postgres" | |
| run: | | |
| go test ./internal/controllers -timeout=5m -v | |
| benchmark-test: | |
| if: github.event.inputs.run_benchmark_tests == 'true' || contains(github.event.head_commit.message, 'testbe') | |
| needs: [get-version, ko-build] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read # Required for checkout | |
| packages: read # Required to pull images from GHCR | |
| services: | |
| redis: | |
| image: redis:6.2-alpine | |
| ports: | |
| - 6379:6379 | |
| options: >- | |
| --health-cmd "redis-cli ping" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| postgres: | |
| image: postgres:15-alpine | |
| ports: | |
| - 5432:5432 | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| env: | |
| POSTGRES_HOST_AUTH_METHOD: trust | |
| scanner: | |
| # The image is tagged with the version from semver.sh | |
| image: ghcr.io/metraction/pharos/pharos:${{ needs.get-version.outputs.version }} | |
| env: | |
| PHAROS_REDIS_DSN: redis:6379 | |
| PHAROS_SCANNER_CACHEENDPOINT: redis://redis:6379 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.23' # Or your project's Go version | |
| - name: Wait for services to be healthy | |
| run: | | |
| echo "Waiting for services to initialize..." | |
| sleep 15 # Adjust as needed, or implement a more robust health check for the scanner | |
| - name: Run benchmark tests | |
| env: | |
| DOCKER_REGISTRIES_AUTH: "registry://spastai\\@gmail.com:${{ secrets.GITHUB_TOKEN }}@ghcr.io/metraction" | |
| run: | | |
| # These tests should publish to Redis, and the scanner service should consume. | |
| # The benchmark test (routing/publish_test.go) is configured to use localhost:6379 for Redis. | |
| go test ./internal/routing -bench=. -benchmem -timeout=5m | |
| - name: Show scanner service logs | |
| run: | | |
| echo "Scanner service logs:" | |
| docker logs ${{ job.services.scanner.id }} | |
| - name: Check scanner service logs on failure | |
| if: failure() | |
| run: | | |
| echo "Tests failed. Scanner logs are available in the 'scanner' service logs in the GitHub Actions UI." | |
| # The logs for services are automatically collected by the runner and displayed in the UI. | |
| # This step is just a pointer for easier debugging. |