Publishing a Maven artifact to GitHub Packages registry
- November 23, 2021
GitHub Packages is a platform for hosting and managing packages, including containers and other dependencies. GitHub Packages combines your source code and packages in one place to provide integrated permissions management and billing, so you can centralize your software development on GitHub.
Publishing a Maven artifact to GitHub Packages
GitHub Package Repository provides an easy way to share dependencies like Maven artifacts between GitHub projects. Publishing of new artifacts into GitHub Package Repository can be automated with GitHub Actions.
Prerequisites
- Anypoint Platform Account
- GitHub Repository Account
- Git bash Terminal
1. Adding distribution management in pom.xml
<distributionManagement>
<repository>
<id>github</id>
<name>Owner</name>
<url>https://maven.pkg.github.com/Owner/repo-name</url>
</repository>
</distributionManagement>
2. Commit the code to GitHub
Steps to be followed:
Prerequisite
Download git bash on local system
Steps to commit the application
- Copy the GitHub repository URL
- Create an empty folder in local directory and right-click on empty directory to open gitbash terminal.
- Enter the commands
- $ git add * (adds all the project files)
- Downloads the data from the repository to the local directory
- $ cd <github repository name>
- Now, in Anypoint Studio, right-click on the project -> Show In -> System Explorer. It redirects to the project folder in the workspace.
- Copy and paste the project files into the git local directory
- Next, in the terminal, enter commands
- $ git add * (adds all the project files)
- $ git commit -m “Initial Commit” (Commits the files to set into git repository)
- $ git push (Pushes the files from local system to git repository)
- All the files are pushed to the git repository.
Personal Access Token for your GitHub account
To upload artifacts into the GitHub Package Registry, your Personal Access Token must have the scope – write:packages.
Adding Personal Access Token and GitHub repo name in settings.xml
<servers>
<server>
<id>github</id>
<username>Owner</username>
<password>Your Personal Access Token</password>
</server>
</servers>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/Sushma-GitRepo/<repo-name></url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
Note: Locally, you can now publish a new Maven artifact into the GitHub Package Registry with the below command.
$ mvn -Pgithub deploy
Publishing through GitHub Actions workflow
name: Maven Package
on:
push:
branches:
– main
jobs:
build:
runs-on: ubuntu-latest
steps:
– uses: actions/checkout@v2
– name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
server-id: github
server-username: GITHUB_USER_REF # env variable name for username
server-password: GITHUB_TOKEN_REF
– name: Build with Maven.
run: mvn clean package -DattachMuleSources -Pgithub deploy
env:
GITHUB_USER_REF: ${{ secrets.GIT_USERNAME }}
GITHUB_TOKEN_REF: ${{ secrets.GIT_TOKEN }}
Creating secrets for GitHub username and token
For creating secrets, go to Settings -> Secrets -> New Repository Secret button.
With the next GitHub Actions build, your artifacts will be published in the repository’s dashboard.<./p>
We can see the dependency created in packages.
Installing a package
To install an Apache Maven package from GitHub Packages, edit the pom.xml file to include the package as a dependency.
- Authenticate to GitHub Packages.
- Add the package dependencies to the dependencies element of your project pom.xml file, replacing com.mycompany:hello-world-3 with your package.
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>hello-world-3</artifactId>
<version>1.0.7-SNAPSHOT</version>
</dependency>
Install the package.
$ mvn install
Note: Maven dependency downloaded at .m2\repository\com\mycompany\hello-world-3
Deploying the jar file to Runtime Manager
- Download the jar file from GitHub packages.
- Copy the jar file and paste to the local repository.
- Open the Git Bash terminal, clone the repository and follow these commands.
$ git clone <url>
$ git pull
$ add <.jar file>
$git commit -m “<add comments>”
$git push
Provide GitHub token as username and password in the pop up window
We can see in the below screenshot, a jar file is added to the repository.
Deploying jar file to Runtime Manager through GitHub actions
name: Deploying on to Runtime Manager
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
deploy:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
– uses: actions/checkout@v2
– name: Cache
uses: actions/cache@v2.1.5
with:
# A list of files, directories, and wildcard patterns to cache and restore
path: /home/runner/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles(“**/pom.xml”) }}
restore-keys: |
${{ runner.os }}-maven-
– name: Setup Java JDK
uses: actions/setup-java@v1
with:
# The Java version to set up. Takes a whole or semver Java version. See examples of supported syntax in the README file
java-version: 1.8
– name: Deploy to CloudHub
env:
USERNAME: ${{ secrets.ANYPOINT_USERNAME }}
PASSWORD: ${{ secrets.ANYPOINT_PASSWORD }}
run: |
artifactName=$(ls *.jar | head -1)
mvn mule:deploy -Dmule.artifact=$artifactName -Danypoint.userName=$USERNAME -Danypoint.password=$PASSWORD -Danypoint.env=“Sandbox” -Danypoint.appName=“DEMO-WORK-1”
Adding the Maven plugin and CloudHub configuration in pom.xml
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<cloudHubDeployment>
<username>${anypoint.userName}</username>
<password>${anypoint.password}</password>
<environment>${anypoint.env}</environment>
<applicationName>${anypoint.appName}</applicationName>
<workers>1</workers>
<workerType>Micro</workerType>
<muleVersion>4.3.0</muleVersion>
<objectStoreV2>true</objectStoreV2>
</cloudHubDeployment>
</configuration>
</plugin>
Similarly, we can download old version jar files and deploy to Runtime Manager.
References
By- Sushma Kamuni