Search code examples
javamavengithubgradlegithub-packages

Debugging Github Packages repository connection in Gradle


I have set up a private project in Github that has some Maven packages. I can browse to them on the Github web site.

By following the directions here I have set up my build.gradle to declare the repository as follows:

repositories {
    mavenLocal()
    maven {
        url = "https://maven.pkg.github.com/myAccountName/myRepo"
        credentials {
            username = System.getenv("GITHUB_USERNAME")
            password = System.getenv("GITHUB_PACKAGES_TOKEN")
        }
    }
}

I've set up the system environment variables with the username and access token that I created in Github settings with read:packages permission.

I declare the dependency the same way I was doing previously when I had it installed to mavenLocal(). But I deleted it from the local Maven repo so I could test this.

The dependency cannot be resolved. But all the warning from Gradle says is:

Unable to resolve dependency for ':android@debug/compileClasspath': Could not resolve myGroup:myArtifact:1.0

There are no logged warnings about one of the Maven repos not being valid, but I think that is the likely problem, since this was working fine when the package was in the local Maven repo. Likely either I'm specifying it incorrectly or the credentials aren't working.

I also tried directly typing the string username and token instead of using environment variables. The token is brand new and not expired.

How can I determine what is going wrong with my connection to the Github Packages Maven repository? Is there a way I can get more useful logs? Or do you see something I'm doing wrong with how I'm authenticating?


Solution

  • From what I can tell from your question, your setup looks correct.

    It seems that you’re only looking at the error provided by Android Studio. Gradle’s own error messages are usually more helpful, so I’d recommend to look at those. In other words, I’d recommend to try building the project from the command line, e.g. with:

    ./gradlew build
    

    Depending on what’s wrong, you’ll get different error messages. For example, if your password/token is wrong, then you’ll see something like this:

    > Could not resolve all files for configuration ':compileClasspath'.
       > Could not resolve com.example:my-lib:1.0.0.
         Required by:
             project :
          > Could not resolve com.example:my-lib:1.0.0.
             > Could not get resource 'https://maven.pkg.github.com/myAccountName/myRepo/com/example/my-lib/1.0.0/my-lib-1.0.0.pom'.
                > Could not GET 'https://maven.pkg.github.com/myAccountName/myRepo/com/example/my-lib/1.0.0/my-lib-1.0.0.pom'. Received status code 401 from server: Unauthorized
    

    Note the Received status code 401 from server: Unauthorized at the end.

    If, however, your dependency declaration is wrong (any of group, module name or version), then you’ll get an error like this:

    > Could not resolve all files for configuration ':compileClasspath'.
       > Could not find com.example:oops-wrong:1.0.0.
         Searched in the following locations:
           - file:/home/chriki/.m2/repository/com/example/oops-wrong/1.0.0/oops-wrong-1.0.0.pom
           - https://maven.pkg.github.com/myAccountName/myRepo/com/example/oops-wrong/1.0.0/oops-wrong-1.0.0.pom
         Required by:
             project :
    

    Of course, you’ll get the same error if you haven’t (correctly) uploaded/published the required package before. You can manually check if it exists at https://maven.pkg.github.com/myAccountName/myRepo/packages.

    There may be other error messages for other cases. I remember having seen a 403, for example. Unfortunately, the GitHub Packages registry doesn’t seem to provide enough details to Gradle so that it can provide other errors than the two above. Even if I use a wrong repository, I still get the second kind of error only:

    > Could not resolve all files for configuration ':compileClasspath'.
       > Could not find com.example:my-lib:1.0.0.
         Searched in the following locations:
           - file:/home/chriki/.m2/repository/com/example/my-lib/1.0.0/my-lib-1.0.0.pom
           - https://maven.pkg.github.com/oops-wrong/com/example/my-lib/1.0.0/my-lib-1.0.0.pom
         Required by:
             project :
    

    Some more findings:

    • For the credentials, it seems that GitHub only looks at the token. The user name doesn’t seem to matter.
    • The concrete name of the repository in the URL seems to be irrelevant as long as it’s not empty.

    Getting back to your concrete issue: part of the Android Studio error message seems to still come from Gradle directly. It says “Could not resolve” rather than “Could not find”. So it’s likely a problem with the repository connection. If you haven’t used your token for anything else, then you can check at https://github.com/settings/tokens whether GitHub considers it to having been used, yet (e.g., “Last used within the last week”). If it says “Never used”, then you’ll know that Gradle has never succeeded to connect to GitHub, and you should double check the used token or try a new one. Alternatively, you could try your credentials with wget first:

    wget --user myAccountName --password "$GITHUB_PACKAGES_TOKEN" \
        https://maven.pkg.github.com/myAccountName/myRepo/com/example/my-lib/1.0.0/my-lib-1.0.0.pom