Serhii Zadorozhnyi
Serhii Zadorozhnyi

Reputation: 626

How to push to Github package registry with Gradle

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

Upvotes: 17

Views: 14458

Answers (6)

Anand Rockzz
Anand Rockzz

Reputation: 6668

In my case, the token I had setup in repo secrets had permissions only for repo. Beware, it should have write:packages authorized to it also in https://github.com/settings/tokens/new

Upvotes: 0

Jilles van Gurp
Jilles van Gurp

Reputation: 8334

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.

Upvotes: 3

yue mu
yue mu

Reputation: 74

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

Upvotes: 1

madhead
madhead

Reputation: 33511

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.

Upvotes: 10

Xin Meng
Xin Meng

Reputation: 2110

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

Upvotes: 1

jjoller
jjoller

Reputation: 681

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.

Upvotes: 3

Related Questions