| 
 | 1 | +name: Publish OpenAPI Spec to APIHub  | 
 | 2 | + | 
 | 3 | +on:  | 
 | 4 | +  workflow_call:  | 
 | 5 | +    secrets:  | 
 | 6 | +      SWAGGERHUB_API_KEY:  | 
 | 7 | +        required: true  | 
 | 8 | +    inputs:  | 
 | 9 | +      upload_artifact_name:  | 
 | 10 | +        required: true  | 
 | 11 | +        type: string  | 
 | 12 | +      apihub_owner:  | 
 | 13 | +        required: true  | 
 | 14 | +        type: string  | 
 | 15 | +      api_name:  | 
 | 16 | +        required: true  | 
 | 17 | +        type: string  | 
 | 18 | +      api_version:  | 
 | 19 | +        required: true  | 
 | 20 | +        type: string  | 
 | 21 | +      is_release:  | 
 | 22 | +        required: false  | 
 | 23 | +        type: boolean  | 
 | 24 | +        default: false  | 
 | 25 | + | 
 | 26 | +jobs:  | 
 | 27 | +  Push-API-to-APIHub:  | 
 | 28 | +    runs-on: ubuntu-latest  | 
 | 29 | +    env:  | 
 | 30 | +      SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }}  | 
 | 31 | +    steps:  | 
 | 32 | +      - name: Download versioned OpenAPI spec  | 
 | 33 | +        uses: actions/download-artifact@v4  | 
 | 34 | +        with:  | 
 | 35 | +          name: ${{ inputs.upload_artifact_name }}  | 
 | 36 | +          path: ./${{ inputs.api_version }}  | 
 | 37 | + | 
 | 38 | +      - name: Set up Node.js  | 
 | 39 | +        uses: actions/setup-node@v4  | 
 | 40 | + | 
 | 41 | +      - name: Install SwaggerHub CLI  | 
 | 42 | +        run: npm install -g swaggerhub-cli  | 
 | 43 | + | 
 | 44 | +      - name: Set CLI_API_REF env var  | 
 | 45 | +        env:  | 
 | 46 | +          APIHUB_OWNER: ${{ inputs.apihub_owner }}  | 
 | 47 | +          API_NAME: ${{ inputs.api_name }}  | 
 | 48 | +          API_VERSION: ${{ inputs.api_version }}  | 
 | 49 | +        run: |  | 
 | 50 | +          echo "CLI_API_REF=${APIHUB_OWNER}/${API_NAME}/${API_VERSION}" >> "$GITHUB_ENV"  | 
 | 51 | +          echo "Using CLI_API_REF=${CLI_API_REF}"  | 
 | 52 | +
  | 
 | 53 | +      - name: Find OpenAPI file  | 
 | 54 | +        id: find_spec  | 
 | 55 | +        run: |  | 
 | 56 | +          FILE=$(find ./${{ inputs.api_version }} -type f -name "*.yml" | head -n 1)  | 
 | 57 | +          echo "openapi_spec_file=$FILE" >> "$GITHUB_OUTPUT"  | 
 | 58 | +
  | 
 | 59 | +      # https://github.com/SmartBear/swaggerhub-cli?tab=readme-ov-file#swaggerhub-apicreate  | 
 | 60 | +      - name: Push API definition into API Hub  | 
 | 61 | +        env:  | 
 | 62 | +          OPENAPI_SPEC_FILE: ${{ steps.find_spec.outputs.openapi_spec_file }}  | 
 | 63 | +        run: |  | 
 | 64 | +          if [[ "${{ inputs.is_release }}" == "true" ]]; then  | 
 | 65 | +            echo "PUBLIC: Publishing the RELEASED API to SwaggerHub"  | 
 | 66 | +            swaggerhub api:create "${CLI_API_REF}" --file "${OPENAPI_SPEC_FILE}" --published publish --visibility public  | 
 | 67 | +          else  | 
 | 68 | +            echo "PRIVATE: Publishing the DRAFT API to SwaggerHub"  | 
 | 69 | +            swaggerhub api:create "${CLI_API_REF}" --file "${OPENAPI_SPEC_FILE}" --visibility private  | 
 | 70 | +          fi  | 
 | 71 | +
  | 
 | 72 | +      # https://github.com/SmartBear/swaggerhub-cli?tab=readme-ov-file#swaggerhub-apiget  | 
 | 73 | +      - name: Check the API definition exists  | 
 | 74 | +        run: |  | 
 | 75 | +          set -o pipefail  | 
 | 76 | +          if ! swaggerhub api:get "${CLI_API_REF}" 2>&1 | tee error.log; then  | 
 | 77 | +            echo "❌ API definition was not found on APIHub"  | 
 | 78 | +            echo "--- CLI Error Output ---"  | 
 | 79 | +            cat error.log  | 
 | 80 | +            exit 1  | 
 | 81 | +          fi  | 
 | 82 | +
  | 
 | 83 | +      # https://github.com/SmartBear/swaggerhub-cli?tab=readme-ov-file#swaggerhub-apivalidate  | 
 | 84 | +      - name: Validate definition  | 
 | 85 | +        run: |  | 
 | 86 | +          set -o pipefail  | 
 | 87 | +          if ! swaggerhub api:validate "${CLI_API_REF}" 2>&1 | tee error.log; then  | 
 | 88 | +            echo "❌ OpenAPI definition failed validation"  | 
 | 89 | +            echo "--- CLI Error Output ---"  | 
 | 90 | +            cat error.log  | 
 | 91 | +            exit 1  | 
 | 92 | +          fi  | 
 | 93 | +
  | 
 | 94 | +        # https://github.com/SmartBear/swaggerhub-cli?tab=readme-ov-file#swaggerhub-apisetdefault  | 
 | 95 | +      - name: Set released version to the default API Definition  | 
 | 96 | +        if: inputs.is_release  | 
 | 97 | +        run: |  | 
 | 98 | +          swaggerhub api:setdefault "${CLI_API_REF}"  | 
0 commit comments