Code quality is a vital aspect of software development, and linting tools like ktlint can help maintain code consistency in Kotlin projects. Integrating ktlint into your GitHub workflow ensures that code style and formatting are automatically checked with every pull request. In this blog, we’ll walk you through the process of setting up ktlint in your GitHub Actions workflow for a Kotlin project.
Prerequisites
Before getting started, make sure you have the following:
- A Kotlin project hosted on GitHub.
- GitHub Actions already set up in your repository.
Create a GitHub Actions workflow file
Now, create a GitHub Actions workflow file that will run ktlint on your Kotlin code. Create a .github/workflows/ktlint.yml
file in your repository with the following content
name: KtLint
on:
pull_request:
jobs:
ktlint:
name: Check Code Quality
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Run Ktlint
uses: ScaCap/action-ktlint@master
with:
github_token: ${{ secrets.github_token }}
reporter: github-pr-check
This action will run on pull request creation and any commit addition to pull request.
PR: https://github.com/cw-bhanunadar/Micronaut-playground/pull/13/files
Review the results
After the workflow runs, you can check the Actions tab in your GitHub repository to see the results. It will show whether the ktlint checks passed or failed.
By following these steps, you’ve integrated ktlint into your GitHub workflow, ensuring that Kotlin code style and formatting are consistently checked on every push to the main
branch. This helps maintain code quality and saves time by catching style issues early in the development process.