Skip to main content

A Jenkinsfile linter that validates Jenkinsfiles using Jenkins API

Project description

Jenkinsfile Lint

CI codecov

A Python-based Jenkinsfile linter that validates Jenkinsfiles using Jenkins API.

Table of Contents

Features

  • Validates Jenkinsfiles using Jenkins REST API
  • Works as a pre-commit hook
  • Supports both command-line usage and environment variables for configuration
  • Requires Jenkins credentials for validation
  • Supports skipping files that are not Jenkins pipelines (e.g., pure Groovy helper classes)

Installation

Using pip

pip install jenkinsfilelint

Using pre-commit

Add this to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/shenxianpeng/jenkinsfilelint
    rev: # or specific version tag
    hooks:
      - id: jenkinsfilelint

Usage

[!IMPORTANT] jenkinsfilelint requires Jenkins credentials to be set via environment variables for validation:

  • JENKINS_URL: Your Jenkins server URL (required)
  • JENKINS_USER: Your Jenkins username (required unless anonymous access is enabled)
  • JENKINS_TOKEN: Your Jenkins API token (required unless anonymous access is enabled)

Command Line

Validation requires Jenkins credentials. Set them using environment variables:

export JENKINS_URL=https://your-jenkins-instance.com
export JENKINS_USER=your-username
export JENKINS_TOKEN=your-api-token

jenkinsfilelint path/to/Jenkinsfile

Or using command-line arguments:

jenkinsfilelint path/to/Jenkinsfile \
  --jenkins-url https://your-jenkins-instance.com \
  --username your-username \
  --token your-api-token

Validate multiple files:

jenkinsfilelint Jenkinsfile Jenkinsfile.prod tests/Jenkinsfile

Skipping Files

In Jenkins shared libraries, some Groovy files are pure Groovy helper classes, not Jenkins pipeline scripts. Use the --skip option to exclude files from validation:

# Skip a specific file pattern
jenkinsfilelint --skip '*/src/*.groovy' Jenkinsfile src/Utils.groovy

# Skip multiple patterns
jenkinsfilelint --skip '*/src/*.groovy' --skip 'vars/*.groovy' Jenkinsfile src/Utils.groovy vars/deploy.groovy

The --skip option accepts glob patterns and can be used multiple times.

Including Only Specific Files

Use the --include option to validate only files that match specified patterns (whitelist approach). Files not matching any include pattern are silently skipped:

# Only validate Jenkinsfiles (files starting with "Jenkinsfile")
jenkinsfilelint --include 'Jenkinsfile*' Jenkinsfile Jenkinsfile.prod src/Utils.groovy

# Only validate pipeline groovy files in a specific folder
jenkinsfilelint --include 'pipelines/*.groovy' pipelines/deploy.groovy src/Utils.groovy

# Use multiple include patterns
jenkinsfilelint --include 'Jenkinsfile*' --include 'pipelines/*.groovy' \
  Jenkinsfile pipelines/deploy.groovy src/Utils.groovy

The --include and --skip options can be combined: --include first narrows the set of files to consider, then --skip further excludes files within that set.

Pre-commit Hook

Create or update .pre-commit-config.yaml in your repository:

repos:
  - repo: https://github.com/shenxianpeng/jenkinsfilelint
    rev: # or specific version tag
    hooks:
      - id: jenkinsfilelint

Or using command-line arguments:

repos:
  - repo: https://github.com/shenxianpeng/jenkinsfilelint
    rev: # or specific version tag
    hooks:
      - id: jenkinsfilelint
        args: ["--jenkins-url=https://your-jenkins-instance.com", "--username=your-username", "--token=your-api-token"]

To skip certain files (e.g., pure Groovy classes in Jenkins shared libraries):

repos:
  - repo: https://github.com/shenxianpeng/jenkinsfilelint
    rev: # or specific version tag
    hooks:
      - id: jenkinsfilelint
        args: ["--skip=*/src/*.groovy", "--skip=vars/*.groovy"]

To validate only specific files:

repos:
  - repo: https://github.com/shenxianpeng/jenkinsfilelint
    rev: # or specific version tag
    hooks:
      - id: jenkinsfilelint
        args: ["--include=Jenkinsfile*", "--include=pipelines/*.groovy"]

[!WARNING] Using args to pass credentials directly in the configuration file is not recommended for security reasons. Consider using environment variables instead. For more details, see pre-commit/pre-commit#758.

Then install the pre-commit hook:

pre-commit install

How It Works

The linter sends the Jenkinsfile to your Jenkins instance's /pipeline-model-converter/validate endpoint for validation. Jenkins credentials (URL, username, and API token) are required.

Requirements

  • Python 3.6+
  • Jenkins server with Pipeline plugin installed

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jenkinsfilelint-1.3.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file jenkinsfilelint-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jenkinsfilelint-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d5d04d2ca4fc33e542aaa8f282eeadf169df0cb93392f7726837e5fb159ffc2
MD5 1c8ed03acfcdef96434e991b98e9422b
BLAKE2b-256 28aa85366805f6640d4f33b422be21b61c72d2076276a172868c0978d255f972

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page