Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
ci: run Postman API collections against the live stack
Ronald A. Richardson7a8ab4e64d62
diff --git a/.github/actions/postman-contract/action.yml b/.github/actions/postman-contract/action.ymlnew file mode 100644index 00000000..ed5ad914--- /dev/null+++ b/.github/actions/postman-contract/action.yml@@ -0,0 +1,128 @@+name: Postman API Contract+description: >-+ Mint a Fleetbase API key against an already-running stack and run the official+ Postman collections (from fleetbase/postman) against http://localhost:8000.++inputs:+ collections:+ description: Comma- or newline-separated collection names under postman/collections.+ required: false+ default: |+ Fleetbase API+ Fleetbase Core API+ Fleetbase Storefront API+ Fleetbase Ledger API+ base-url:+ description: Base URL of the running API.+ required: false+ default: http://localhost:8000+ namespace:+ description: API version namespace.+ required: false+ default: v1+ postman-ref:+ description: Ref of the fleetbase/postman repo to check out.+ required: false+ default: main+ postman-api-key:+ description: Postman CLI API key. When empty, the whole action no-ops.+ required: false+ default: ''+ github-token:+ description: Token used to check out fleetbase/postman.+ required: false+ default: ''++runs:+ using: composite+ steps:+ - name: Check Postman API key availability+ id: pmauth+ shell: bash+ run: |+ if [[ -n "${{ inputs.postman-api-key }}" ]]; then+ echo "available=true" >> "$GITHUB_OUTPUT"+ else+ echo "available=false" >> "$GITHUB_OUTPUT"+ echo "::warning::POSTMAN_API_KEY is not set; skipping the Postman contract run."+ fi++ - name: Mint API key from the running stack+ id: key+ if: steps.pmauth.outputs.available == 'true'+ shell: bash+ run: |+ set -euo pipefail+ KEY="$(docker compose exec -T application php artisan tinker < scripts/ci/mint-api-key.php 2>/dev/null \+ | grep -oE 'flb_(live|test)_[A-Za-z0-9_-]+' | tail -1)"+ if [[ -z "$KEY" ]]; then+ echo "::error::Could not mint an API key from the running stack."+ exit 1+ fi+ echo "::add-mask::$KEY"+ echo "api_key=$KEY" >> "$GITHUB_OUTPUT"+ echo "Verifying key against ${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current ..."+ curl -fsS -H "Authorization: Bearer $KEY" \+ "${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current" >/dev/null+ echo "✔ API key authenticates."++ - name: Install Postman CLI+ if: steps.pmauth.outputs.available == 'true'+ shell: bash+ run: |+ curl -fsSL -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh+ postman --version++ - name: Postman CLI login+ if: steps.pmauth.outputs.available == 'true'+ shell: bash+ run: postman login --with-api-key "${{ inputs.postman-api-key }}"++ - name: Checkout Postman collections+ if: steps.pmauth.outputs.available == 'true'+ uses: actions/checkout@v4+ with:+ repository: fleetbase/postman+ ref: ${{ inputs.postman-ref }}+ token: ${{ inputs.github-token }}+ path: .postman++ - name: Run Postman collections+ if: steps.pmauth.outputs.available == 'true'+ shell: bash+ env:+ API_KEY: ${{ steps.key.outputs.api_key }}+ COLLECTIONS: ${{ inputs.collections }}+ BASE_URL: ${{ inputs.base-url }}+ NAMESPACE: ${{ inputs.namespace }}+ run: |+ set -uo pipefail+ overall=0+ # Accept commas or newlines as separators; the loop runs in the current+ # shell (process substitution) so $overall survives.+ while IFS= read -r raw; do+ name="$(echo "$raw" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"+ [[ -z "$name" ]] && continue+ dir=".postman/postman/collections/$name"+ if [[ ! -d "$dir" ]]; then+ echo "::warning::Collection directory not found: $dir"+ continue+ fi+ echo "::group::Collection: $name"+ extra=()+ if [[ "$name" == "Fleetbase Integrated Vendor Flow" ]]; then+ extra+=(--iteration-data ".postman/collections/workflows/integrated-vendor/data/happy-path.example.json")+ fi+ if postman collection run "$dir" \+ --env-var "base_url=${BASE_URL}" \+ --env-var "namespace=${NAMESPACE}" \+ --env-var "api_key=${API_KEY}" \+ "${extra[@]}"; then+ echo "✔ $name passed"+ else+ echo "::warning::Collection failed: $name"+ overall=1+ fi+ echo "::endgroup::"+ done < <(printf '%s' "$COLLECTIONS" | tr ',' '\n')+ exit $overalldiff --git a/.github/workflows/api-contract.yml b/.github/workflows/api-contract.ymlnew file mode 100644index 00000000..bda56d04--- /dev/null+++ b/.github/workflows/api-contract.yml@@ -0,0 +1,134 @@+name: API Contract (Postman)++# Reusable workflow: boot a full Fleetbase stack and run the official Postman+# collections against the live API. Called centrally (build from source) and by+# module repos (fleetops/storefront/ledger/core-api) which pull the published+# image and run only their own collection:+#+# jobs:+# contract:+# uses: fleetbase/fleetbase/.github/workflows/api-contract.yml@main+# with: { collections: "Fleetbase Storefront API", build-from-source: false }+# secrets: inherit+#+# Requires org-level secrets POSTMAN_API_KEY and _GITHUB_AUTH_TOKEN (inherited).++on:+ workflow_call:+ inputs:+ collections:+ description: Comma/newline-separated collection names under postman/collections.+ type: string+ required: false+ default: |+ Fleetbase API+ Fleetbase Core API+ Fleetbase Storefront API+ Fleetbase Ledger API+ build-from-source:+ description: Build the API image from this repo (central) instead of pulling :latest.+ type: boolean+ required: false+ default: false+ fleetbase-ref:+ description: Ref of fleetbase/fleetbase to boot.+ type: string+ required: false+ default: main+ postman-ref:+ description: Ref of fleetbase/postman to run.+ type: string+ required: false+ default: main++permissions:+ contents: read++jobs:+ # Cheap gate so callers don't spend ~10 minutes booting the stack when the+ # Postman secret isn't configured yet. Skips the whole contract job if absent.+ gate:+ name: Check Postman secret+ runs-on: ubuntu-latest+ outputs:+ enabled: ${{ steps.check.outputs.enabled }}+ steps:+ - id: check+ run: |+ if [[ -n "${{ secrets.POSTMAN_API_KEY }}" ]]; then+ echo "enabled=true" >> "$GITHUB_OUTPUT"+ else+ echo "enabled=false" >> "$GITHUB_OUTPUT"+ echo "::warning::POSTMAN_API_KEY is not set; skipping the API contract run."+ fi++ contract:+ name: Postman contract against live API+ needs: gate+ if: needs.gate.outputs.enabled == 'true'+ runs-on: ubuntu-latest+ timeout-minutes: 45+ steps:+ - name: Checkout fleetbase/fleetbase+ uses: actions/checkout@v4+ with:+ repository: fleetbase/fleetbase+ ref: ${{ inputs.fleetbase-ref }}+ submodules: recursive+ token: ${{ secrets._GITHUB_AUTH_TOKEN }}++ - name: Set up Docker Buildx+ uses: docker/setup-buildx-action@v3++ - name: Build API image from source+ if: ${{ inputs.build-from-source }}+ run: |+ docker build \+ --file docker/Dockerfile \+ --target app-release \+ --build-arg GITHUB_AUTH_KEY="${{ secrets._GITHUB_AUTH_TOKEN }}" \+ --tag fleetbase/fleetbase-api:latest \+ .++ - name: Pull published API image+ if: ${{ !inputs.build-from-source }}+ run: docker pull fleetbase/fleetbase-api:latest++ - name: Ensure clean database volume+ run: |+ sudo rm -rf docker/database/mysql+ mkdir -p docker/database/mysql++ - name: Run non-interactive installer+ run: |+ [ -f api/.env ] || cp api/.env.example api/.env 2>/dev/null || touch api/.env+ bash scripts/docker-install.sh --non-interactive++ - name: Wait for API health+ run: |+ set -uo pipefail+ for i in $(seq 1 30); do+ if curl -fsS --max-time 5 http://localhost:8000/health | grep -q '"status"'; then+ echo "API is healthy"; exit 0+ fi+ sleep 5+ done+ echo "::error::API did not become healthy"; exit 1++ - name: Run Postman contract+ uses: ./.github/actions/postman-contract+ with:+ collections: ${{ inputs.collections }}+ postman-ref: ${{ inputs.postman-ref }}+ postman-api-key: ${{ secrets.POSTMAN_API_KEY }}+ github-token: ${{ secrets._GITHUB_AUTH_TOKEN }}++ - name: Dump logs on failure+ if: failure()+ run: |+ docker compose ps || true+ docker compose logs --no-color --tail=300 || true++ - name: Tear down+ if: always()+ run: docker compose down -v || truediff --git a/.github/workflows/install-smoke.yml b/.github/workflows/install-smoke.ymlindex b553b156..e30708f7 100644--- a/.github/workflows/install-smoke.yml+++ b/.github/workflows/install-smoke.yml@@ -125,6 +125,18 @@ jobs:wait_for "Console" "http://localhost:4200" "<!DOCTYPE html>" || \wait_for "Console" "http://localhost:4200" "<html"+ # Run the official Postman collections against the live stack we just booted.+ # Reuses this job's running containers (no second boot). Non-blocking for now:+ # the full collections include data-dependent requests that stay red until the+ # collections are curated in fleetbase/postman. Drop continue-on-error once green.+ - name: Postman API contract+ if: steps.auth.outputs.available == 'true'+ continue-on-error: true+ uses: ./.github/actions/postman-contract+ with:+ postman-api-key: ${{ secrets.POSTMAN_API_KEY }}+ github-token: ${{ secrets._GITHUB_AUTH_TOKEN }}+- name: Dump logs on failureif: failure() && steps.auth.outputs.available == 'true'run: |diff --git a/scripts/ci/mint-api-key.php b/scripts/ci/mint-api-key.phpnew file mode 100644index 00000000..68ece5a9--- /dev/null+++ b/scripts/ci/mint-api-key.php@@ -0,0 +1,42 @@+<?php++/*+ * CI helper: mint a working Fleetbase public-API key against a booted stack.+ *+ * Run it by piping into the application container's tinker:+ * docker compose exec -T application php artisan tinker < scripts/ci/mint-api-key.php+ *+ * It creates (idempotently) an organization + owner user + a live API credential and+ * prints the `flb_live_...` key on its own line. The public API middleware+ * (AuthenticateOnceWithBasicAuth) authenticates `Authorization: Bearer <key>` against+ * the api_credentials.key column, and requires the company to have an owner.+ *+ * The key value is generated by ApiCredentialObserver on insert, so the credential+ * MUST be created through the Eloquent model (not a raw insert), then reloaded.+ */++use Fleetbase\Models\ApiCredential;+use Fleetbase\Models\Company;+use Fleetbase\Models\User;++$company = Company::firstOrCreate(['name' => 'CI Contract Org']);++$user = User::firstOrCreate(+ ['email' => '[email protected]'],+ ['name' => 'CI Contract User', 'company_uuid' => $company->uuid, 'status' => 'active']+);+$user->type = 'user';+$user->password = 'CiContract1!' . bin2hex(random_bytes(6));+$user->save();++// The company must have an owner or the API-key middleware rejects requests (401).+$company->setOwner($user, true);+$company->save();++// test_mode=false => flb_live_ key stored on the default (mysql) connection.+$credential = ApiCredential::firstOrCreate(+ ['company_uuid' => $company->uuid, 'name' => 'CI Contract Key'],+ ['user_uuid' => $user->uuid, 'test_mode' => false]+);++echo PHP_EOL . $credential->fresh()->key . PHP_EOL;