How to push to Github package registry with Gradle
Also worth setting up a github action to publish to the github package repo:
name: Publish package to GitHub Packages
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Publish package
run: gradle -Pversion=${{ github.event.release.tag_name }} build publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
This publishes a package every time we create a release tag with that tag as the version.
GitHub has published the official document for How to use Gradle with GitHub packager
https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages#authenticating-to-github-packages
I was able to publish to the Github Package Registry using the maven-publish plugin. It seems to work just fine now.
My build.gradle file looks like this:
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id 'java'
id 'maven-publish'
}
group 'com.company.project'
archivesBaseName = 'library-name'
version '0.1.0'
repositories {
mavenCentral()
}
dependencies {
// java dependencies
}
publishing {
repositories {
maven {
name = "Github"
url = uri("https://maven.pkg.github.com/<OWNER>/<REPO>")
credentials {
username = findProperty("github.username")
password = findProperty("github.token")
}
}
}
publications {
register("jar", MavenPublication) {
from(components["java"])
pom {
url.set("https://github.com/<OWNER>/<REPO>.git")
}
}
}
}
Put your github username and token into the gradle.properties file.
New answer:
GitHub has published the official guide: Configuring Gradle for use with GitHub Packages.
Old answer:
It seems like the plugin is not very stable yet. Take a look at the repository I've created that has everything set up. I managed to publish a few packages with that plugin here.
Even the packages are published, Gradle shows task as failed, due to some issues with maven-metadata.xml
:
> Task :publishMainPublicationToGitHub madhead Maven PackagesRepository FAILED
Could not transfer metadata so57323260:test/maven-metadata.xml from/to remote (https://maven.pkg.github.com/madhead): Could not get resource 'so57323260/test/maven-metadata.xml'
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':publishMainPublicationToGitHub madhead Maven PackagesRepository'.
> Failed to publish publication 'main' to repository 'GitHub madhead Maven Packages'
> Could not GET 'https://maven.pkg.github.com/madhead/so57323260/test/maven-metadata.xml'. Received status code 422 from server: Unprocessable Entity
But that's ok, probably will be fixed one day.
I've noticed, that the packages might not be published (see the linked issue) because of the incorrect groupId
of a Maven publication. It seems like right now it should be equal to the Github's project name. So, in my case, I had to use so57323260
as a groupId
for a madhead/so57323260
project. That's not how packages work in Maven generally, so that might be your issue.