# Build custom engine branch if needed workflow (prisma/prisma)

> The Build custom engine branch if needed workflow from prisma/prisma, explained and optimized by Latchkey.

Source: https://latchkey.dev/workflows/prisma-prisma-build-engine-branch  
Upstream: [prisma/prisma](https://github.com/prisma/prisma/blob/main/.github/workflows/build-engine-branch.yml) (.github/workflows/build-engine-branch.yml), licence Apache-2.0

## What it does

This is the Build custom engine branch if needed workflow from the prisma/prisma repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

## The workflow

```yaml
name: Build custom engine branch if needed
on:
  workflow_call:
    outputs:
      branchName:
        description: The name of the branch of checked out engine
        value: ${{ jobs.parseCommand.outputs.branchName }}
      engineHash:
        description: Commit hash of the checked out engine
        value: ${{ jobs.build.outputs.engineHash }}

jobs:
  parseCommand:
    name: Parse "/engine-branch" command in PR body
    runs-on: ubuntu-latest
    outputs:
      branchName: ${{ steps.detectCustomEngineBranch.outputs.branchName }}
    steps:
      - name: Extract custom engine branch name from PR body
        id: detectCustomEngineBranch
        env:
          PR_BODY: ${{ github.event.pull_request.body }}
        if: |
          github.event_name == 'pull_request' &&
          contains(env.PR_BODY, '/engine-branch') &&
          (
            github.event.pull_request.author_association == 'OWNER' ||
            github.event.pull_request.author_association == 'MEMBER' ||
            github.event.pull_request.author_association == 'COLLABORATOR' ||
            github.event.pull_request.author_association == 'CONTRIBUTOR'
          )
        run: |
          BRANCH_NAME=$(echo "$PR_BODY" | grep -o '/engine-branch \S*' | awk '{print $2}')
          if [ -z "$BRANCH_NAME" ]; then
            echo "Branch name not specified in /engine-branch command."
            exit 1
          fi

          echo "Requested custom engine from branch: $BRANCH_NAME"
          echo "branchName=$BRANCH_NAME" >> "$GITHUB_OUTPUT"

  build:
    name: Build
    strategy:
      matrix:
        os: [ubuntu-latest, macos-14, windows-latest]
    runs-on: ${{ matrix.os }}
    needs: parseCommand
    if: ${{ needs.parseCommand.outputs.branchName != '' }}
    outputs:
      engineHash: ${{ steps.engine-hash.outputs.engineHash }}
    steps:
      - uses: actions/checkout@v6
        with:
          repository: prisma/prisma-engines
          ref: ${{ needs.parseCommand.outputs.branchName }}

      - name: Get current engine hash
        id: engine-hash
        shell: bash
        run: echo "engineHash=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"

      - uses: actions/cache@v5
        name: Artifacts cache
        id: artifacts-cache
        with:
          path: |
            target/release/schema-engine
            target/release/schema-engine.exe
            target/prisma-schema-wasm
            query-compiler/query-compiler-wasm/pkg
            schema-engine/schema-engine-wasm/pkg
          key: ${{ runner.os }}-engine-${{ steps.engine-hash.outputs.engineHash }}-${{ github.event.number }}

      - uses: dtolnay/rust-toolchain@stable
        if: steps.artifacts-cache.outputs.cache-hit != 'true'

      - uses: actions/cache@v5
        name: Dependencies cache
        if: steps.artifacts-cache.outputs.cache-hit != 'true'
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build native engines with cargo
        if: steps.artifacts-cache.outputs.cache-hit != 'true'
        shell: bash
        run: |
          cargo build --release -p schema-engine-cli

      - uses: cargo-bins/cargo-binstall@main

      - name: Extract wasm-bindgen version and install matching CLI
        if: steps.artifacts-cache.outputs.cache-hit != 'true'
        shell: bash
        run: |
          REQUESTED_WASM_BINDGEN_VERSION=$(grep -A 2 '^\[\[package\]\]$' Cargo.lock | grep 'name = "wasm-bindgen"' -A 1 | grep 'version' | sed 's/.*"\(.*\)".*/\1/')
          if [ -n "$REQUESTED_WASM_BINDGEN_VERSION" ]; then
            echo "Found wasm-bindgen version in Cargo.lock: $REQUESTED_WASM_BINDGEN_VERSION"
            if which wasm-bindgen; then
              INSTALLED_WASM_BINDGEN_VERSION=$(wasm-bindgen --version | cut -d' ' -f2)
              if [ "$INSTALLED_WASM_BINDGEN_VERSION" == "$REQUESTED_WASM_BINDGEN_VERSION" ]; then
                echo "Skipping wasm-bindgen CLI installation, version $INSTALLED_WASM_BINDGEN_VERSION has already been installed"
              else
                echo "ERROR: wasm-bindgen CLI $INSTALLED_WASM_BINDGEN_VERSION has already been installed, but the required version is $REQUESTED_WASM_BINDGEN_VERSION"
                exit 1
              fi
            else
              echo "Installing wasm-bindgen CLI $REQUESTED_WASM_BINDGEN_VERSION"
              cargo binstall wasm-bindgen-cli --version "$REQUESTED_WASM_BINDGEN_VERSION"
            fi
          else
            echo "ERROR: Could not find wasm-bindgen version in Cargo.lock"
            exit 1
          fi

      - name: Install Binaryen (includes wasm-opt)
        uses: jaxxstorm/action-install-gh-release@v2.1.0
        with:
          repo: WebAssembly/binaryen
          tag: version_122
          binaries-location: binaryen-version_122/bin
          platform: ${{ runner.os == 'macOS' && 'macos' || runner.os == 'Windows' && 'windows' || 'linux' }}
          cache: true

      - name: Install coreutils on macOS (includes numfmt)
        if: runner.os == 'macOS'
        run: brew install coreutils

      - name: Add coreutils to PATH on Windows (includes numfmt)
        if: runner.os == 'Windows'
        shell: bash
        run: |
          echo "C:/msys64/usr/bin" >> "$GITHUB_PATH"
          echo "C:/msys64/mingw64/bin" >> "$GITHUB_PATH"

      - name: Build prisma-schema-wasm
        if: steps.artifacts-cache.outputs.cache-hit != 'true'
        shell: bash
        run: |
          make PROFILE=release build-schema-wasm

      - name: Build query-compiler-wasm
        if: steps.artifacts-cache.outputs.cache-hit != 'true'
        shell: bash
        run: |
          make WASM_BUILD_PROFILE=release build-qc-wasm

      - name: Build schema-engine-wasm
        if: steps.artifacts-cache.outputs.cache-hit != 'true'
        shell: bash
        run: |
          make WASM_BUILD_PROFILE=release build-se-wasm
```

## FAQ

### What does the Build custom engine branch if needed workflow (prisma/prisma) workflow do?

This is the Build custom engine branch if needed workflow from the prisma/prisma repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.

### What CI health grade does this workflow get?

This Automation and other workflow grades B. Paste your own workflow into the Latchkey grader to see its grade and the exact fixes.

### How can I improve this Automation and other workflow?

Add job timeouts. Latchkey applies these automatically on managed runners when you point runs-on at Latchkey.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
