Tweak github packages post

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-11-22 18:01:27 +05:30
parent cdabca0c50
commit 948ac1f9a7
3 changed files with 13 additions and 14 deletions

View File

@ -9,11 +9,11 @@ title = "Publishing an Android library to GitHub packages"
+++
GitHub released the Package Registry beta in May of this year, and graduated it to public availability in Universe 2019, rebranded as [GitHub Packages](https://github.com/features/packages "GitHub Packages"). It supports NodeJS, Docker, Maven, Gradle, NuGet, and RubyGems. That's a LOT of ground covered for a service that's about one year old.
Naturally, I was excited to try this out. The [documentation](https://help.github.com/en/github/managing-packages-with-github-packages/about-github-packages) is by no means lacking, but the [official instructions](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages) for using Packages with Gradle does not work for Android libraries. To make it compatible with Android libraries, some small but obscure edits are needed which I've enlisted below so others don't have to struggle as I did.
Naturally, I was excited to try this out. The [documentation](https://help.github.com/en/github/managing-packages-with-github-packages/about-github-packages) is by no means lacking, but the [official instructions](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages) for using Packages with Gradle do not work for Android libraries. To make it compatible with Android libraries, some small but non-obvious edits are needed which I've documented here for everybody's benefit.
I've also created a [sample repository]() with incremental commits following the steps given below, for people who prefer to see the code directly.
**IMPORTANT**: GitHub Packages currently does **NOT** support unauthenticated access to packages, which means you will always require a personal access token with the `read:packages` scope to be able to download packages during build. I emailed GitHub support about this, and their reply is attached at the end of this post.
**DISCLAIMER**: GitHub Packages currently does **NOT** support unauthenticated access to packages, which means you will always require a personal access token with the `read:packages` scope to be able to resolve packages during build. I have an ongoing discussion with GitHub support about it and will update this post whenever the situation changes.
I've also created a [sample repository](https://github.com/msfjarvis/github-packages-deployment-sample/) with incremental commits corresponding to the steps given below, for people who prefer to see the code directly.
NB: Grab a Personal Access Token from GitHub with the `write:packages` scope. You will be using this as authentication to deploy packages. I'll not delve deep into this, a Google search will point you in the right direction if you're not familiar with how to obtain one.
@ -21,7 +21,7 @@ NB: Grab a Personal Access Token from GitHub with the `write:packages` scope. Yo
Copy the official integration step from GitHub's [guide](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages#authenticating-with-a-personal-access-token), into your Android library's `build.gradle`/`build.gradle.kts`. If you try to run `./gradlew publish` now, you'll run into errors. We'll be fixing that shortly. \[Commit [link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/d69235577a1d4345cecb364a3a3d366bf894c5a6)\]
```groovy
```diff
--- library/build.gradle
+++ library/build.gradle
@@ -1,5 +1,6 @@
@ -62,7 +62,7 @@ Copy the official integration step from GitHub's [guide](https://help.github.com
Switch out the `maven-publish` plugin with [this](https://github.com/wupdigital/android-maven-publish) one. It provides us an Android component that's compatible with publications and precisely what we need. \[Commit [link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/1452c4a0c15d394b73dc3384f02834788dfe1bda)\]
```groovy
```diff
--- build.gradle
+++ build.gradle
@@ -14,6 +14,7 @@ buildscript {
@ -102,7 +102,7 @@ Switch out the `maven-publish` plugin with [this](https://github.com/wupdigital/
Switch to using the `android` component provided by `wup.digital.android-maven-publish`. This is the one we require to be able to upload an [AAR](https://developer.android.com/studio/projects/android-library) artifact. \[Commit [link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/7cc6fcd6ffa5774433bce76ac6929435dbbb77cc)\]
```groovy
```diff
--- library/build.gradle
+++ library/build.gradle
@@ -42,7 +42,7 @@ publishing {
@ -120,7 +120,9 @@ Switch to using the `android` component provided by `wup.digital.android-maven-p
Every Gradle/Maven dependency's address has three attributes, a group ID, an artifact ID, and a version.
implementation 'com.example:my-fancy-library:1.0.0'
```groovy
implementation 'com.example:my-fancy-library:1.0.0'
```
Here:
@ -130,7 +132,7 @@ Here:
We'll need to configure these too. I prefer using the `gradle.properties` file for this purpose since it's very easy to access variables from it, but if you have a favorite way of configuring build properties, use that instead! \[Commit [link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/cee74a5e0b3b76d1d7a2d4eb9636d80fb1db49d6)\]
```groovy
```diff
--- gradle.properties
+++ gradle.properties
@@ -19,3 +19,7 @@ android.useAndroidX=true
@ -163,7 +165,7 @@ Now all that's left to do is configure GitHub Actions. Go to the Secrets menu in
Now, let's add the actual configuration that'll get Actions up and running.
```groovy
```diff
--- /dev/null
+++ .github/workflows/publish_snapshot.yml
@@ -0,0 +1,13 @@
@ -182,15 +184,12 @@ Now, let's add the actual configuration that'll get Actions up and running.
+ PASSWORD: ${{ secrets.PACKAGES_TOKEN }}
```
That's it! Once you push to GitHub, you'll see the action running in your repository's Actions tab and a corresponding package once the workflow finishes executing.
![](/uploads/github_packages_actions_run.png)
![](/uploads/github_packages_built_package.png)
That's it! Once you push to GitHub, you'll see the [action running](https://github.com/msfjarvis/github-packages-deployment-sample/commit/42e1f6609bf9f2abe8e181296a57d86df648b4d4/checks?check_suite_id=322323808) in your repository's Actions tab and a [corresponding package](https://github.com/msfjarvis/github-packages-deployment-sample/packages/60429) in the Packages tab once the workflow finishes executing.
### Closing notes
The requirement to authenticate for packages is a significant problem with GitHub Packages' adoption, giving an edge to solutions like [JitPack](https://jitpack.io) which handle the entire process automagically. As mentioned earlier, I did contact GitHub support about it and got this back.
![](/uploads/github_packages_support_response.png)
![GitHub support reply about authentication requirement for packages](/uploads/github_packages_support_response.png)
My interpretation of this is quite simply that **it's gonna take a while**. I hope not :)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB