CI/CD for Kotlin Projects: GitHub Actions from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why CI/CD is Critically Important for Kotlin Projects

\n

In modern Kotlin development, whether it's a Ktor backend, an Android application, or a multiplatform project, manually performing repetitive operations (building, running tests, code style checks) is not only a waste of time but also a source of errors. CI/CD (Continuous Integration / Continuous Deployment) solves this problem by automating the entire pipeline from commit to production.

\n

GitHub Actions is a built-in automation tool that is ideal for Kotlin projects. It allows you to create pipelines directly in the repository without requiring third-party services. In this article, we will cover how to set up a full CI/CD for Kotlin using GitHub Actions, from basic builds to deployment.

\n\n

1. GitHub Actions and Kotlin Basics

\n

GitHub Actions works based on workflows — YAML files that define a sequence of steps. Each workflow is triggered by an event (push, pull request, schedule) and runs in a virtual runner environment.

\n

For Kotlin projects, the standard stack includes:

\n
    \n
  • Gradle or Maven — build system;
  • \n
  • JUnit / Kotest — testing frameworks;
  • \n
  • Detekt / ktlint — linters for Kotlin.
  • \n
\n

The first step is to create a file .github/workflows/ci.yml in the root of the repository. Let's look at a minimal configuration for building and testing.

\n\n

1.1 Basic Build and Testing

\n

Let's start with a simple pipeline that triggers on every push and pull request to the main branch:

\n
name: CI for Kotlin\n\non:\n  push:\n    branches: [ main ]\n  pull_request:\n    branches: [ main ]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions/checkout@v3\n      - name: Set up JDK 17\n        uses: actions/setup-java@v3\n        with:\n          java-version: '17'\n          distribution: 'temurin'\n      - name: Grant execute permission for gradlew\n        run: chmod +x gradlew\n      - name: Build with Gradle\n        run: ./gradlew build\n      - name: Run tests\n        run: ./gradlew test
\n

This code does the following: clones the repository, sets up JDK 17 (the recommended version for Kotlin 1.9+), makes gradlew executable, runs the build, and executes tests. If tests fail, the pipeline stops with an error.

\n\n

2. Advanced Configuration: Linting, Caching, and Matrix Builds

\n

A basic setup is good, but a real project needs more. Let's add code style checking with Detekt and Gradle dependency caching to speed up the workflow.

\n\n

2.1 Gradle Caching

\n

Without caching, each run downloads dependencies from scratch, which can take 2-5 minutes. Use built-in caching:

\n
- name: Cache Gradle packages\n  uses: actions/cache@v3\n  with:\n    path: |\n      ~/.gradle/caches\n      ~/.gradle/wrapper\n    key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}\n    restore-keys: |\n      ${{ runner.os }}-gradle-
\n\n

2.2 Linting with Detekt

\n

Add a task for code checking. Detekt can be run via the Gradle plugin:

\n
- name: Run Detekt\n  run: ./gradlew detekt
\n

If Detekt finds violations, the pipeline will fail with an error. This ensures that "dirty" code doesn't get into the repository.

\n\n

2.3 Matrix Builds

\n

If your project supports multiple versions of Kotlin or JDK, use a matrix. This allows you to check compatibility without duplicating code:

\n
jobs:\n  build:\n    strategy:\n      matrix:\n        java: [11, 17, 21]\n        kotlin: [1.9.0, 2.0.0]\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v3\n      - name: Set up JD                

Blogs

Book Recommendations