Integrating Stylelint into Your GitHub Workflow: Ensuring Consistency and Quality in Your CSS Code

Rajdeep Chandra
3 min readApr 19, 2023

Maintaining a consistent and high-quality coding style across your CSS files is crucial to ensuring your web application’s visual and functional integrity. However, as your codebase grows, keeping track of and enforcing a consistent CSS style can be challenging. That’s where Stylelint comes in. Stylelint is a powerful linter that can help you catch errors and inconsistencies in your CSS code before they cause issues in production.

In this tutorial, we’ll walk you through how to add Stylelint to your GitHub workflow. We’ll start by creating a YAML file in your repository’s .github/workflows folder that defines the workflow logic. Next, we'll add the necessary dependencies to your repository and create a .stylelintrc file that contains the configuration for Stylelint. With these steps in place, Stylelint will automatically check your CSS files for errors and inconsistencies every time you push or pull request to your repository.

So, let’s get started and see how you can easily integrate Stylelint into your GitHub workflow and ensure your CSS code’s consistency and quality.

First, you need to create a .github/workflows folder in your GitHub repository if you haven't already. Then create a new YAML file in the folder and give it a name that describes its purpose. For example, stylelint.yml.

In the newly created YAML file, you need to define the workflow that will run Stylelint. Here is an example of what the YAML file could look like:

This YAML file contains three main sections: name, on, and jobs. The name section is used to give the workflow a descriptive name. In this case, we’re calling it Stylelint.

The on section is used to specify when the workflow should run. In this example, the workflow will run when a push or pull request is made to the repository.

The jobs section is where the workflow logic is defined. In this example, we have one job called stylelint that runs on the ubuntu-latest operating system. This job contains three steps:

  1. Checkout: This step checks out the repository so that it can be used in subsequent steps.
  2. Install dependencies: This step installs any required dependencies for Stylelint, which in this example are installed using npm.
  3. Run Stylelint: This step runs Stylelint on all .css files in the repository.

Once you have created the YAML file and defined the workflow, you need to add the necessary dependencies to your repository. To do this, create a package.json file in the root of your repository (if you don't have one already) and add the following dependencies:

{
"devDependencies": {
"stylelint": "^13.13.1",
"stylelint-config-standard": "^22.0.0"
}
}

This will install Stylelint and the standard configuration for Stylelint. You can modify the configuration as per your requirements.

Finally, you need to create a .stylelintrc file in the root of your repository. This file contains the configuration for Stylelint. Here is an example of what the file could look like:

{
"extends": "stylelint-config-standard",
"rules": {
"indentation": 2,
"string-quotes": "double",
"color-hex-length": "long"
}
}

This configuration extends the standard Stylelint configuration and modifies three rules: indentation, string quotes, and color hex length. You can modify this configuration according to your needs.

Once you have completed these steps, commit the changes to your repository and push them to GitHub. The workflow will now run automatically whenever a push or pull request is made to the repository, and Stylelint will check your CSS files for errors and warnings.

Thanks for reading. For any questions please reach out to me at rajrock38@gmail.com

--

--