We all have the need to release our own components to developers, but we have to buy a server to build a Nexus. After watching the release process of MavenCentral, it is very painful. Now the JitPack can do just fine. However, you have to go to the website to manually click Get It, and recently I encountered a problem that plug-ins cannot be published in JitPack. All these problems make me feel that component publishing is very painful now.

Our goal this time is to do the best with limited resources!!

Github’s existing solution

Github can be used as a Maven mirror source from the repository by simply uploading the AAR to the repository. For example, our warehouse address is github.com/MRwangqi/Ma… , and aar is placed in the main branch, then the Maven mirror address is

Raw.githubusercontent.com/MRwangqi/Ma…

We just need to import the image source in our project build.gradle as follows:

repositories {
   google()
   // Import the github repository mirror source
   maven{
        url "https://raw.githubusercontent.com/MRwangqi/Maven/main"}}Copy the code

Then introduce their own dependence can be happy to play, my checkPlugin is so play

Second, the plan

Look at the above Github existing scheme, do you feel very troublesome? He has the following questions:

  1. You need to publish the components locally first
  2. The components are then copied and copied to the Maven repository
  3. Git then commits the components to the repository

Before addressing these issues, we need to take a look at Github Actions:

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment

We can build an AAR and publish it to maven’s repository by automating the build. The idea is set, so let’s do it.

1. Create Actions

Github Actions provides a lot of ready-made templates. You can create an Android CI template to see what you need to do to build an APK

The clickConfigureEnter the YML configuration interface:

I’m not going to go into workflow here because it would be too much of a distraction. If you want to learn about syntax, go to the Workflow document instead

/gradlew assembleDebug./gradlew assembleDebug./gradlew assembleDebug./gradlew assembleDebug Indeed, building projects is that simple.

2. Build components

Build./gradlew publish./gradlew publish Indeed, we just need to make a small change to the component, open the build.gradle component and configure it as follows:

plugins {
    ...
    id 'maven-publish'
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                // Configure the component's GAV
                groupId = 'com.codelang.jitpack'
                artifactId = 'hook-test'
                version = '1.0'
            }
        }
        repositories {
            maven {
                // Specifies the component to publish to the project project's /build/repo directory
                url = ".. /build/repo"}}}}Copy the code

This is actually a very basic component publishing configuration, but I specify the location of the component publishing so that after the component is published, it can be retrieved through a relative path for subsequent component uploading.

So, our Workflow can be changed to:

Next, we’re looking at, how do you publish built components to another repository

3. Component release

Knock up my keyboard in Google a while disorderly search Actions, don’t say, also really search to a, github.com/s0/git-publ… , the library introduces:

This GitHub Action will take any subdirectory in your repository, and push it as the contents of a git branch to a repository and branch of your choosing, either over SSH or to the current repo.

This Action uploads files in a directory specified by the current repository to another repository

It is a coincidence that the path is specified when the component is builtbuild/repo, and based on the sample demo provided with the library, our Workflow can be written as:

Git SSH is a private key that can be used to generate git SSHwww.jianshu.com/p/0a4190325…Configure your public key on Github and check the contents of the private key id_rsa.

It’s important to note that,---BESIN RSAThese are also copied, and then we open the component repository and configure SSH_PRIVATE_KEY as follows:

Next, we just need Run Workflow:

We can view the entire build process, and during the Deploy publish process we can see that we have published successfully:

And then we goGithub.com/MRwangqi/Ma…Take a look at the warehouse, aar has been successfully released:

Note that:

  • Git-publish-subdir-action must be configuredCLEAR_GLOBS_FILE: ".clear-target-files"Otherwise, by default, it will delete all files in your Maven repository that are not in the git directory. The.clear-target-files file contains the directory that needs to be deleted

Third, summary

Github Actions can be configured with trigger rules, but the default Android CI configuration is that the workflow is executed as soon as the code is pushed to the repository, the component is published to the specified directory, and all files in the directory are submitted to the remote repository. That is, the automatic publishing of components is realized.

You can copy my Workflow section, just change the repO address to your own repository address, configure the SSH_PRIVATE_KEY private key, and don’t forget to configure.clear-target-files in the component repository

My Workflow file address: github.com/MRwangqi/Me…