continuous-integrationgithub-actionsworkflow

Github workflow : Why my jobs are skipped when use if & contain?


name: Build-test

on:
  pull_request:
    branches: [dev]
    types: ["opened", "synchronize"]

jobs:
  test-front-end-customer:
    if: contains(github.event.pull_request.changed_files, 'packages/front-end-customer/')
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
    # ...

We started mono-repo project using yarn-workspaces.

My projects are organized in the following format: (rootDir)/packages/(project-name)

I want to run test when PR event is opend.

I want to test each package when it changes. So I set the IF condition: contains changed files in the package.

Did I misunderstand the docs? All of my jobs are skipped for all PR even though there are changes in each package. If i remove the condition, it works well.


Solution

  • Context value

    github.event.pull_request.changed_files
    

    is a number of changes files.

    In order to find a list of files that changed, you need to run this in a separate step named ‘diff’

    git diff --name-only ${{ GitHub.head_ref }} ${{ GitHub.base_ref }}
    

    Then you need to place this multi line text into the output variable ‘changed_files’ of that step.

    Then you can use

    if: contains(steps.diff.outputs.changed_files, 'packages/front-end-customer/')