Skip to content
Latchkey LogoLatchkey home

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: prisma/prisma.github/workflows/build-engine-branch.ymlLicense Apache-2.0View source
B
CI health grade
72/100
Health score
2
Jobs per trigger
2
Fixes in the optimized version

In short

  1. CI health: B (good)
  2. The optimized version below points runs-on at Latchkey managed runners and adds job timeouts.

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

workflow (.yml)
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

The same workflow, on Latchkey

Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.

workflow (.yml), optimized
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:
    timeout-minutes: 30
    name: Parse "/engine-branch" command in PR body
    runs-on: ubuntu-latest
    runs-on: latchkey-small
    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:
    timeout-minutes: 30
    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

What changed

  1. Run on Latchkey managed runners with one line (runs-on), which apply the fixes below automatically and self-heal transient failures. Each Linux x64 job moves to the Latchkey size matching its vCPU count, latchkey-small for a standard 2 vCPU runner; macOS, Windows and arm64 jobs stay as written.
  2. Add a job timeout so a hung step cannot burn hours of runner time.

Left runs-on as written on the build job: matrix.os mixes Linux with macOS and Windows, which Latchkey does not run. Point only the Linux legs at Latchkey, for example with a matrix include entry that sets the runner.

3 third-party actions are referenced by a movable tag. Pin them to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.

What Latchkey heals here

This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:

  • Dependency installs

This workflow runs 2 jobs (4 with the matrix expanded) per trigger. 2 of the 4 run on a standard GitHub-hosted Linux x64 runner, which in a private repository bills $0.006 a minute against $0.0025 on latchkey-small, 58% lower; in a public repository like this one they are free. 2 of the 4 run on macOS and Windows, which Latchkey does not offer, so those stay where they are. Estimate the monthly difference with your own run counts.

Actions used in this workflow

  • actions/checkout
  • actions/cache
  • dtolnay/rust-toolchain
  • cargo-bins/cargo-binstall
  • jaxxstorm/action-install-gh-release

Frequently asked questions

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.

References