Search code examples
githubgithub-actionssuper-linter

How to configure super linter on GitHub for all branches


Currently I have this file to check for linting errors when a PR is created on GitHub, but it targets only a single branch i.e branch_name and I have to change the branch name for every branch, how can I configure this to run on the branch that the PR is currently targeting?

I have tried to use pattern matching by using ** as described here but that doesn't work.

name: Lint Code Base

on:
  pull_request:
    branches: [branch_name]

jobs:
  build:
    name: Lint Code Base
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Lint Code Base
        uses: github/super-linter@v4
        env:
          VALIDATE_ALL_CODEBASE: false
          DEFAULT_BRANCH: branch_name
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          DISABLE_ERRORS: true
          
          PYTHON_FLAKE8_CONFIG_FILE: .flake8
          VALIDATE_PYTHON_FLAKE8: true

Solution

  • To trigger on any PR, and have super-linter run for the diff between the head branch and the base branch of the PR, you have to

    1. not filter the pull_request on any branches, and
    2. set the DEFAULT_BRANCH environment variable to the base branch of the PR.

    Arguably, DEFAULT_BRANCH isn't a great name for that variable, because it's used to build the list of changed files, and PRs can be opened into branches other than the default branch of a repository.

    Additionally:

    • you should use the latest version of the checkout action: as of today, it's v4.1.1
    • super-linter has moved into its own organization, and the action is now maintained at super-linter/super-linter
    • I like pinning my runner version so it doesn't magically change (and break) one day, but that's a personal preference
    • I don't think fetch-depth: 0 is necessary, but the super-linter README recommends it anyway

    Taken all together:

    name: Lint Code Base
    
    on: pull_request
    
    jobs:
      build:
        name: Lint Code Base
        runs-on: ubuntu-22.04
    
        steps:
          - name: Checkout Code
            uses: actions/[email protected]
            with:
              fetch-depth: 0
    
          - name: Lint Code Base
            uses: super-linter/[email protected]
            env:
              VALIDATE_ALL_CODEBASE: false
              DEFAULT_BRANCH: ${{ github.event.pull_request.base.ref }}
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              DISABLE_ERRORS: true
              
              PYTHON_FLAKE8_CONFIG_FILE: .flake8
              VALIDATE_PYTHON_FLAKE8: true