Creating GitHub Action to Deploy Projects into a Private Maven Repository
I have been heavily using the other CI/CD platforms like GitLab, BitBucket, Jenkins and Azure DevOps but I can definitely say GitHub nailed it. It has a different and easy to use approach and I am surprised that you can add custom runners for your CI/CD pipeline.
I am gonna create a very simple Kotlin application to show how to deploy a library in a private maven repository. I am gonna use Repsy for maven repository hosting. It gives 1 GB free storage and it should be sufficient for most cases.
Let’s create a gradle project, your IDE will help about this, or you can install gradle CLI from official gradle site. Or you can use SdkMan for this purpose.
sdk i gradle
gradle cli will help you bootstraping your project.
mkdir experiment-actions-kotlin-lib
cd experiment-actions-kotlin-lib
gradle init
Gradle will ask us bunch of questions. I will select library
type for project generation
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 3
Kotlin for programming language selection. Happy to see plenty of options there.
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 4
And Gonna use Kotlin DSL
instead of Groovy
.
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Kotlin) [1..2] 2
Just press enter for default project name or you can enter another one suitable for you.
Project name (default: experiment-actions-kotlin-lib):
For the source package selection, I am gonna use my github TLD.
Source package (default: experiment.actions.kotlin.lib): com.github.firatkucuk.experiment_actions_kotlin_lib
It seems a little bit long but that’s OK. Gradle will create a simple Library.kt
file. Let’s rename it Hello.kt
and change the content.
package com.github.firatkucuk.experiment_actions_kotlin_libclass Hello {
fun sayHello(text: String): String {
return "Hello $text"
}
}
And same for the test file LibraryTest.kt
will be HelloTest.kt
.
package com.github.firatkucuk.experiment_actions_kotlin_libimport kotlin.test.Test
import kotlin.test.assertEqualsclass HelloTest {
@Test fun sayHello() {
val classUnderTest = Hello()
assertEquals(classUnderTest.sayHello("World"), "Hello World")
}
}
Now we can test the build operation:
gradle build
The next phase is publishing our library to our private maven repository in repsy.io. After signup respy create a default repository which can be used with account password. So you only need to sign-up that’s all.
Let’s modify our build.gradle.kts
file for publishing the artifact.
plugins {
id("org.jetbrains.kotlin.jvm") version "1.3.72"
`java-library`
`maven-publish`
}group = "com.github.firatkucuk"
version = "1.0"repositories {
jcenter()
}val repsyUrl: String by project
val repsyUsername: String by project
val repsyPassword: String by projectpublishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
} repositories {
maven {
url = uri(repsyUrl)
credentials {
username = repsyUsername
password = repsyPassword
}
}
}
}dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
Our project implementation is completed. Let’s create git integration of our project. First create an empty git project called experiment-actions-kotlin-lib
on GitHub.
git init
git add .
git commit -m "initial commit"
git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/experiment-actions-kotlin-lib.git
git push origin master
The next step is integrating your project with Github Actions. Go to https://github.com/YOUR_USER_NAME/experiment-actions-kotlin-lib/actions/new
Please Find the Java with Gradle Action. Then you can click the Set up this workflow button. An online editor will be opened up.
Let’s directly commit the changes and we’re gonna see our Action will run immediately. GitHub action will fail so please ignore this. It requires some properties file we’re gonna add properties as GitHub secret.
We need to create a GitHub secret for our gradle.properties file. But because of the limitations. GitHub cannot use file content as secrets. GitLab has the same restriction once upon a time then they’ve introduced File types but we need to create a text file our local computer. The content should be something like this:
repsyUrl=https://repo.repsy.io/mvn/MY_REPSY_ACCOUNT/default
repsyUsername=myrepsyuser
repsyPassword=myrepsypassword
We need to encode this with base64. There’re many online base64 converters. If you’re using Mac/Linux it should be easy like this:
base64 -w0 gradle.properties
Let’s copy the output and create a secret. Go to https://github.com/YOUR_USER_NAME/experiment-actions-kotlin-lib/settings/secrets
And add a github secret called GRADLE_PROPERTIES and paste the encoded string into it.
Now we can open our pipeline configuration file and make the latests changes. Go To https://github.com/YOUR_USER_NAME/experiment-actions-kotlin-lib/blob/master/.github/workflows/gradle.yml and click the edit button. We will add some extra steps for injecting gradle properties.
name: Java CI with Gradleon:
push:
branches: [ master ]
pull_request:
branches: [ master ]jobs:
build:runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
env:
GRADLE_PROPERTIES: ${{ secrets.GRADLE_PROPERTIES }}
run: mkdir -p ~/.gradle && echo $GRADLE_PROPERTIES | base64 -d > ~/.gradle/gradle.properties && cat ~/.gradle/gradle.properties && ./gradlew build
- name: publish
run: ./gradlew publish
That’s all you can find the source code here.