14

Trying to push the Gradle project to Github package registry, but not working as expected.

Using io.freefair.github.package-registry-maven-publish plugin for Gradle.

Configure GitHub in build.gradle with data needed to publish - code below. And run the publishing task publishAllPublicationsToGutHub. Getting no error but I can't see my package in GitHub package registry.

github {
    slug
    username = "myGitUserName"
    token = "myTokenWithRightAccess"
    tag = "HEAD"
    travis = true
}

Expecting some examples of how to publish to Github package registry with Gradle or what I'm doing wrong when publishing

Naveen
  • 360
  • 1
  • 8
  • 23
Serhii Zadorozhnyi
  • 576
  • 2
  • 8
  • 25

5 Answers5

10

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.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • Thank you mate, it works for me, have the same error as you, on "3.8.2" version it doesn't work, have a different error. Expected that I need only provide "GitHub" block to make publish but I needed to add the "publishing" block and "maven-publish" plugin to make it works. – Serhii Zadorozhnyi Aug 05 '19 at 07:09
  • Yeah. Let's stay tuned on GitHub issues. – madhead Aug 05 '19 at 08:57
  • 1
    Have another question, how to access to Github package registry as a dependency now by Gradle and thanks again for answer? – Serhii Zadorozhnyi Aug 06 '19 at 09:15
  • 1
    @DOIT, on, SO it's a [good practice](https://meta.stackoverflow.com/a/321823) to ask new questions for... new questions. So, I've asked one for you [here](https://stackoverflow.com/a/57373631/750510) (and replied as well!). Take a look. – madhead Aug 06 '19 at 10:07
  • In your example, I keep getting this error... `madhead` is not a valid gradle task, am i doing something wrong here. – Harsh Aug 23 '19 at 02:53
  • 1
    It just looks like this feature is too beta. I was able to publish with version x.y.z. Then I was not able to publish next version. Then I was able to publish when I removed version at all. Then I'm not able to publish anymore at all :) – ror Oct 19 '19 at 17:26
  • 1
    The gotcha in our case was the `groupId` and `version` in the `publications.gpr` block – Charney Kaye Mar 05 '20 at 01:06
  • The "New answer" answer is not self contained, you can't just link to an article on StackOverflow. Downvoted. – Yngvar Kristiansen Dec 10 '20 at 14:19
3

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.

jjoller
  • 661
  • 9
  • 17
2

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.

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
1

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

Xin Meng
  • 1,982
  • 3
  • 20
  • 32
1

Complete those properties correctly

  1. OWNER
  2. REPOSITORY
  3. USERNAME (or gradle property gpr.user)
  4. PASSWORD (or gradle property gpr.key)

@See demo

  1. https://github.com/youngerier/packagesdemo

  2. https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages

yue mu
  • 74
  • 5