19

I have been using GitHub packages for a while, in an Android project, without having any issue. Now when I try to publish a new package I get the error:

Could not PUT Received status code 422 from server: Unprocessable Entity

To be sure that I hadn't change anything I went back to a git-tag from which I successfully managed to publish a package a few days ago, I changed only the version to generate a different package. I get the same error.

I added logs and I can see that the token is read correctly, all the value (GROUP, VERSION, etc) seem correct and that the file that I'm trying to publish is there in the correct folder. I have also tried to create and use a new token in case something was wrong with the old one but it didn't help.

Would GitHub reject the publishing with that error in case I published too many files? I did not find any documentation about the error that you get in that case.

EDIT

I have also tried to create a new project and post to that one, in case something had got messed up in the initial one, but it did not work either.

I have tried to PUT a file directly using CURL and this worked that means that the token is correct and that the problem is not the limit in the total size of the published packages:

curl -X PUT \
"https://maven.pkg.github.com/companyname/repositoryname/com/companyname/artifactid/v2.1.520/artifactid-v2.1.520.aar" \
-H "Authorization: token mytoken” \
--upload-file “/full/path/to/file.aar" -vvv

Of course, this is not the solutions since I need to post the maven repo with the pom etc.

END EDIT

Here my configuration that had been working for a long time and that is just following the documentation + the logs that I added to investigate the issue.

In the build.gradle:

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/companyname/companyname-android-sdk")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("GitHubPackagesUsername")
                password = project.findProperty("gpr.key") ?: System.getenv("GitHubPackagesToken")
                println "GitHubPackages build.gradle\n\tusername=$username\n\ttoken=$password"
            }
        }
    }
}

in the publish-artifacts.gradle:

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/companyname/companyname-android-sdk")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("GitHubPackagesUsername")
                password = project.findProperty("gpr.key") ?: System.getenv("GitHubPackagesToken")
                println "GitHubPackages Publish Artifact:\n\tusername=$username\n\ttoken=$password"
            }
        }
    }

    publications {
        gpr(MavenPublication) {
            println "\tskSdkVersion=$SK_SDK_VERSION\n\tarchivesBaseName=$archivesBaseName\n\tGROUP=$GROUP\n\tdesciption=$POM_DESCRIPTION"
            println "artifact from $buildDir/outputs/aar/$archivesBaseName-${VARIANT_SUFFIX}.aar"
            groupId SK_GROUP
            version SK_SDK_VERSION
            artifactId archivesBaseName
            artifact "$buildDir/outputs/aar/$archivesBaseName-${VARIANT_SUFFIX}.aar"
            description POM_DESCRIPTION
            pom.packaging POM_PACKAGING
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.implementation.allDependencies.each {
                    println "dependency=$it"
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }
}

in the gradle.properties file:

POM_NAME=PackageName
POM_PACKAGING=aar
GROUP=com.companyname
POM_DESCRIPTION=CompanyName SDK Core library

VARIANT_SUFFIX is set from an env variable.

archivesBaseName is set in the module's build.gradle

kingston
  • 11,053
  • 14
  • 62
  • 116
  • What is the `gpr` block for? – IgorGanapolsky Jun 15 '21 at 20:28
  • 1
    @IgorGanapolsky it tells Gradle the artifacts that need to be published, with their pom file declaring the dependencies etc. `gpr` is just a name. I have two: `gpr` and `gprStaging` – kingston Jun 16 '21 at 10:33
  • Where do you declare the `gpr` and `gprStaging` names? How does gradle know to interpret them. – IgorGanapolsky Jun 16 '21 at 13:54
  • 1
    @IgorGanapolsky you are declaring it with that line. It's the (horrible?) groovy way of hiding what you are actually doing. In kotlin it would be `create("gpr") {`. Please check: https://docs.gradle.org/current/userguide/publishing_maven.html – kingston Jun 16 '21 at 14:27

3 Answers3

36

Could you try lower casing your artifact ID? I was facing the same issue and lowercasing it made it work.

Reference: https://github.community/t/gradle-maven-deploy-failing-with-422-unprocessable-entity/137299/3

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Jayshil Dave
  • 1,106
  • 10
  • 22
  • Github seems to have changed something here. Ether they allowed uppercase chars or they re-wrote them to lower case. Anyway - this fixed my release build which worked 4 months ago, without any code changes on my side :) – hb0 Nov 02 '20 at 10:22
  • 3
    This is no longer valid. Github has changed it. Most of the 422 errors when publishing to GitHub Packages seems to be related to the fact the artifact is already associated with a different repo (see the answer by @gridcell-coder). If you are using gradle or mvn to publish you only see a generic "422 Unprocessable Entity", but if you do the PUT request directly (e.g. with Postman) you see the full error "Package xxx is already associated with another repository". – Nicus Jul 23 '21 at 12:15
  • 2
    lol, this solved it for me! Thanks +1 – Dubstepzedd Dec 13 '21 at 11:19
17

In my case when the artifact had the name api or service or serde it was being blocked. eg. this worked <artifactId>database-ervice</artifactId> this did not database-service

Edit: When using Postman to do the put request I got

Package "io.XXX.mydomain.xyz-service" is already associated with another repository. so realised that the package was previously deplloyed in another repo.

The maven-deployer-plugin is extremely poor at reporting the error message of the PUT response so had to use mintm to lookup the url and Postman to figure out the message.

Gridcell Coder
  • 693
  • 9
  • 18
  • 2
    Seems to be no other way then changing the artifact/group name, or to delete the artifacts from the previous repo. A user/organization can only publish artifacts with the same group-artifact combination from one repository. – hb0 May 10 '21 at 23:49
0

If you run into this issue 422: Unprocessable Entity it also might be b/c you pushed it to another repo in the same org.

Source: https://github.com/orgs/community/discussions/23474

GEverding
  • 3,131
  • 2
  • 21
  • 23