Configure Publishing to GitHub Packages with Gradle Kotlin DSL and GitHub Actions
Goal
Publish the project exclusively to GitHub Packages using the Vanniktech Maven Publish Gradle plugin, authenticated securely via GitHub Actions.
Why Use Vanniktech Plugin Instead of Default Maven Publish Plugin?
The Vanniktech Maven Publish plugin uniquely simplifies publishing for Android and Kotlin Multiplatform projects. It provides streamlined configuration, convenient defaults, automated POM generation, easy artifact signing, and native support for multiplatform modules, significantly reducing complexity compared to the standard maven-publish plugin.
Technical Steps
1. Gradle Kotlin DSL configuration (build.gradle.kts):
plugins {
id("com.vanniktech.maven.publish") version "0.28.0"
}
group = "com.example.mylibrary"
version = "1.0.0"
publishing {
repositories {
maven {
name = "githubPackages"
url = uri("https://maven.pkg.github.com/<GitHubUser>/<Repo>")
credentials(PasswordCredentials::class)
}
}
}
mavenPublishing {
coordinates("com.example.mylibrary", "artifact-id", "1.0.0")
}
2. Credential management via environment variables:
Gradle maps environment variables prefixed with ORG_GRADLE_PROJECT_ directly to Gradle properties:
ORG_GRADLE_PROJECT_githubPackagesUsername → githubPackagesUsername
ORG_GRADLE_PROJECT_githubPackagesPassword → githubPackagesPassword
3. GitHub Actions workflow configuration (.github/workflows/publish.yml):
name: Publish to GitHub Packages
on:
push:
tags:
- '*'
jobs:
publish:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- uses: gradle/actions/setup-gradle@v3
- name: Publish package
env:
ORG_GRADLE_PROJECT_githubPackagesUsername: ${{ github.actor }}
ORG_GRADLE_PROJECT_githubPackagesPassword: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew publishAllPublicationsToGithubPackagesRepository
Result
With this configuration:
- GitHub Actions securely passes credentials.
- Gradle publishes exclusively to GitHub Packages.
- Maven Central credentials are no longer required, eliminating the initial error.
References
Configure Publishing to GitHub Packages with Gradle Kotlin DSL and GitHub Actions
Goal
Publish the project exclusively to GitHub Packages using the Vanniktech Maven Publish Gradle plugin, authenticated securely via GitHub Actions.
Why Use Vanniktech Plugin Instead of Default Maven Publish Plugin?
The Vanniktech Maven Publish plugin uniquely simplifies publishing for Android and Kotlin Multiplatform projects. It provides streamlined configuration, convenient defaults, automated POM generation, easy artifact signing, and native support for multiplatform modules, significantly reducing complexity compared to the standard maven-publish plugin.
Technical Steps
1. Gradle Kotlin DSL configuration (
build.gradle.kts):plugins { id("com.vanniktech.maven.publish") version "0.28.0" } group = "com.example.mylibrary" version = "1.0.0" publishing { repositories { maven { name = "githubPackages" url = uri("https://maven.pkg.github.com/<GitHubUser>/<Repo>") credentials(PasswordCredentials::class) } } } mavenPublishing { coordinates("com.example.mylibrary", "artifact-id", "1.0.0") }2. Credential management via environment variables:
Gradle maps environment variables prefixed with
ORG_GRADLE_PROJECT_directly to Gradle properties:ORG_GRADLE_PROJECT_githubPackagesUsername→githubPackagesUsernameORG_GRADLE_PROJECT_githubPackagesPassword→githubPackagesPassword3. GitHub Actions workflow configuration (
.github/workflows/publish.yml):Result
With this configuration:
References