apimock 5.0.1

HTTP(S) mock server. Drop JSON files into a folder and your API immediately exists.
Documentation
name: Executable

on:
  release:
    types: [created]

permissions:
  contents: write
  id-token: write

defaults:
  run:
    shell: bash

env:
  PRODUCT_BASENAME: apimock
  TAG: ${{ github.ref_name }}            # tag or branch name
  JOB_WORKDIR: tmp-${{ github.run_id }}  # unique number

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - name: Linux-aarch64-musl
            target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            bin-ext:
            archive-ext: .tar.gz
          - name: Linux-x64-gnu
            target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            bin-ext:
            archive-ext: .tar.gz
          - name: Linux-x64-musl
            target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            bin-ext:
            archive-ext: .tar.gz
          - name: macOS-aarch64
            target: aarch64-apple-darwin
            os: macos-latest
            bin-ext:
            archive-ext: .zip
          - name: Windows-x64
            target: x86_64-pc-windows-msvc
            os: windows-latest
            bin-ext: .exe
            archive-ext: .zip

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      # [ build ]
      - name: Install Rust
        run: bash .github/workflows/scripts/install-rust.sh stable ${{ matrix.target }}
      
      - name: Cache cargo dependencies and build
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Build Rust executable
        run: |
          cargo build --release --target ${{ matrix.target }} --bin ${{ env.PRODUCT_BASENAME }} --locked
      
      # [ release asset ]
      - name: Prepare for release asset
        run: |
          BUILT_FILE_PATH=target/${{ matrix.target }}/release/${{ env.PRODUCT_BASENAME }}${{ matrix.bin-ext }}
          RELEASE_ASSET_BASENAME=${{ env.PRODUCT_BASENAME }}@${{ matrix.name }}-${{ env.TAG }}
          RELEASE_SRC_DIR=${RELEASE_ASSET_BASENAME}
          RELEASE_ASSET_FILENAME=${RELEASE_ASSET_BASENAME}${{ matrix.archive-ext }}
          
          mkdir -p "${{ env.JOB_WORKDIR }}/${RELEASE_SRC_DIR}"

          cp "examples/config/default/apimock.toml" "${{ env.JOB_WORKDIR }}/${RELEASE_SRC_DIR}/"
          cp "examples/config/default/apimock-rule-set.toml" "${{ env.JOB_WORKDIR }}/${RELEASE_SRC_DIR}/"
          cp "examples/config/default/apimock-middleware.rhai" "${{ env.JOB_WORKDIR }}/${RELEASE_SRC_DIR}/"
          cp "${BUILT_FILE_PATH}" "${{ env.JOB_WORKDIR }}/${RELEASE_SRC_DIR}/"
          
          echo "RELEASE_SRC_DIR=${RELEASE_SRC_DIR}" >> $GITHUB_ENV
          echo "RELEASE_ASSET_FILENAME=${RELEASE_ASSET_FILENAME}" >> $GITHUB_ENV

      # BSD tar on macOS: first 8MB of the file are sometimes all NUL byte
      # refs: https://github.com/actions/cache/issues/403 , https://github.com/rust-lang/cargo/issues/8603
      - name: Mitigate macOS tar bug
        if: matrix.target == 'aarch64-apple-darwin'
        run: |
          sudo /usr/sbin/purge
      
      - name: Create archive as release asset - Linux
        if: >
          matrix.target != 'aarch64-apple-darwin' &&
          matrix.target != 'x86_64-pc-windows-msvc'
        run: |
          cd "${{ env.JOB_WORKDIR }}"
          tar czf "../${RELEASE_ASSET_FILENAME}" "${RELEASE_SRC_DIR}"
      
      - name: Create archive as release asset - Windows / macOS
        if: >
          matrix.target == 'aarch64-apple-darwin' ||
          matrix.target == 'x86_64-pc-windows-msvc'
        run: |
          cd "${{ env.JOB_WORKDIR }}"
          7z a "../${RELEASE_ASSET_FILENAME}" "${RELEASE_SRC_DIR}"
      
      - name: Update release with new asset
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release upload ${{ github.ref_name }} ${RELEASE_ASSET_FILENAME}

      # [ artifact passed to next workflow(s) ]
      - name: Upload built binary
        uses: actions/upload-artifact@v7
        with:
          name: ${{ env.PRODUCT_BASENAME }}-${{ matrix.name }}-${{ env.TAG }}
          path: target/${{ matrix.target }}/release/${{ env.PRODUCT_BASENAME }}${{ matrix.bin-ext }}
  
  npm-platforms-publish:
    runs-on: ${{ matrix.os }}
    needs: build
    strategy:
      matrix:
        include:
          # - name: Linux-aarch64-musl
          #   target: aarch64-unknown-linux-musl
          #   os: ubuntu-latest
          - name: Linux-x64-gnu
            npm-target: linux-x64-gnu
            os: ubuntu-latest
          # - name: Linux-x64-musl
          #   target: x86_64-unknown-linux-musl
          #   os: ubuntu-latest
          - name: macOS-aarch64
            npm-target: darwin-arm64
            os: macos-latest
          - name: Windows-x64
            npm-target: win32-x64-msvc
            os: windows-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      
      - name: Setup node
        uses: actions/setup-node@v6
        with:
          node-version: 24
          registry-url: 'https://registry.npmjs.org'
      
      - name: Download built binary
        uses: actions/download-artifact@v8
        with:
          name: ${{ env.PRODUCT_BASENAME }}-${{ matrix.name }}-${{ env.TAG }}
          path: npm/${{ matrix.npm-target }}
      
      - name: npm publish
        working-directory: npm/${{ matrix.npm-target }}
        run: |
          npm install --package-lock-only
          npm ci
          npm publish

  npm-core-publish:
    runs-on: ubuntu-latest
    needs: npm-platforms-publish
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      
      - name: Setup node
        uses: actions/setup-node@v6
        with:
          node-version: 24
          registry-url: 'https://registry.npmjs.org'
      
      - name: npm publish
        working-directory: npm
        run: |
          cp -f ../README.md .
          npm install --package-lock-only --ignore-scripts
          npm ci --ignore-scripts
          npm publish