Skip to content

Take different jobs (matrix) into account #202

@Clashsoft

Description

@Clashsoft

My repo has one workflow file and uses a matrix strategy to build each service individually:

Image
jobs:
  build:
    name: Build and push
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        service:
          - app: ...
            dockerfile: ...
          - ...
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set base ref for Nx affected
        uses: nrwl/nx-set-shas@v4
        # --- here ---
      - name: Set up pnpm
        uses: pnpm/action-setup@v4
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 23
          cache: 'pnpm'
      - name: Install dependencies
        run: PUPPETEER_SKIP_DOWNLOAD=true pnpm install
      - name: Check if Nx affected
        id: nx-affected
        # Use nx show --affected to check if the current commit affects the service
        run: |
          set -x
          pnpm exec nx show projects --affected --target=build --projects=${{ matrix.service.app }}
          affected="$(pnpm exec nx show projects --affected --target=build --projects=${{ matrix.service.app }})"
          echo "Affected: $affected"
          if [ -z "$affected" ]
          then
            echo "No changes detected for ${{ matrix.service.app }}. Skipping build."
            echo "skip=true" >> $GITHUB_OUTPUT
          fi
      - name: Do the actual build **if necessary**...
        if: "!steps.nx-affected.outputs.skip"
        ...

Unfortunately, if any job fails to build, the entire workflow run is considered failed for the purposes of nx-set-shas.
That means the last workflow run where all services built successfully taken as the base ref, which might increase the number of services that need to be built.

It would be beneficial to add the matrix job ID to nx-check-shas so it can find the last workflow run where that matrix job was last successful.
In other words, each matrix job could use a different base sha depending on the service being built or matrix job in general.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions