Publish to github releases and Github Package Registry with GitHub Actions

Publish to Github Package Registry with Github Actions

We have already seen how to create a maven package with CI pipeline created using github actions in an earlier post – GitHub Actions workflow to enable a simple CI pipeline for a java web app with maven and junit
In this post we will deploy the maven package to github package registry.


What is Github Package Registry

This is a registry within Github where you can publish your artifacts from both public and private repos. The package published in a public repo can be accessed by anyone. However, a package published in a private repo will remain private.

One big advantage of publishing the package in Github Package registry for code stored in Github is that you can maintain a single login for your code and packages. It also makes it easier to share your work with others on Github or within your organization. 

Publishing packages to Github Package registry is not just restricted to work with Github actions CI workflow but it has support for other CI platforms as well and has support for webhooks.

From here, let us now dive into in the code changes we need to do in order to deploy the maven package to github package registry.

Update pom.xml


Step 1. In pom.xml we need to ensure that artifact id and version are mentioned correctly. Artifact id should be in lowercase. We also need to change the version every time a package has to be published if we have not setup auto-versioning.

<artifactid>sampleartifact</artifactid>
<packaging>war</packaging>
<version>v1</version>

Step 2. Update <distributionmanagement> section in pom.xml. Create a repository tag and mention the details as shown in the code snippet below. The value of id should be github and the url for github packages will contain the org name and the repo name.

For example if your org name (github account name) is org1 and your repo name is repo1, then the distribution management section will be as follows

<distributionManagement>
  <repository>
    <id>github</id>       
    <name>Releases</name>           <url>https://maven.pkg.github.com/org1/repo1</url> 
  </repository>
</distributionManagement>   

Update the Github Actions workflow

We have an existing workflow for the build job. Let us add another job to it to publish the package. This new job called Publish will be triggered once the Build job has completed.

Below we have mentioned existing workflow for the build job. This workflow was created and explained step by step in the post – GitHub Actions workflow to enable a simple CI pipeline for a java web app with maven and junit.

# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11
    - name: Build with Maven
      run: mvn -B package --file pom.xml

We will now create another workflow and append the code below the above code in the workflow. We want the below job to run only if the above job is successful. This is achieved using the needs directive as shown below.

needs: Build

This new job which we will name as “Publish” will create a release and will publish the maven package to the github package registry.

Publish:
needs: Build
runs-on: ubuntu-latest
 steps:
 - uses: actions/checkout@v2
 - name: Set up JDK 11
   uses: actions/setup-java@v1
   with:
     java-version: '11'
 - name: Create GitHub Release
   uses: actions/create-release@v1
   env:
     GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 
   with: 
     tag_name: v${{github.run_number}}
     release_name: Release V${{github.run_number}} 
 - name: Update version
   run: mvn -B versions:set -DnewVersion=v${{github.run_number}} -DgenerateBackupPoms=false
 - name: Publish to GitHub Packages Apache Maven
   run: mvn -B deploy -DskipTests --file pom.xml
   env:
     GITHUB_TOKEN: ${{ github.token }}

We will set up the environment similar to Build job. Now let us go through the step created for our job.

Create GitHub Release

Here we will use create-release@v1 action from the actions org. We will tag this release with the action run number. We need to authenticate ourselves before we are able to create a release. This can be achieved by using the github token already provided by github actions.

Update version

In the above step we have tagged the release with the github actions run number for this repo. This will be a unique number. In this step we will update the artifact version with the sane version number so that the package that will be create in the next step will be tagged with the same number. This will be helpful to identify the package against a particular release. Moreover, this will also save us the need to manually update the version number everytime we create a new package.

Publish to GitHub Packages Apache Maven

Next step is to update the package in the github package registry for maven. This will have the same version as that of the release created in an earlier step. We will once again use the github token provided by github actions to authenticate the repo. We may not run the tests again as the code has been tested in the previous job, hence we will skip the tests using – Dskiptests

Conclusion

In this post we have used github actions to create a release within github releases and publish the package to github package registry for maven. We have used the sane version for both for easy identification of release against the package.

Let us know if you found the post useful.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *