Skip to content

Commit 281b739

Browse files
committed
adding github CI/CD integration
1 parent 5e82ab3 commit 281b739

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

.github/workflows/build.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Java CI with Gradle
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
tags: [ '*' ] # Trigger on all tags
7+
pull_request:
8+
branches: [ main ]
9+
10+
jobs:
11+
build:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
with:
19+
fetch-depth: 0 # Fetch all history and tags
20+
21+
- name: Set up JDK 17
22+
uses: actions/setup-java@v3
23+
with:
24+
java-version: '17'
25+
distribution: 'temurin'
26+
27+
- name: Grant execute permission for gradlew
28+
run: chmod +x gradlew
29+
30+
- name: Build with Gradle
31+
run: ./gradlew build
32+
33+
- name: Publish to GitHub Packages
34+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
35+
run: ./gradlew publish
36+
env:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

build.gradle.kts

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import java.io.ByteArrayOutputStream
2+
13
plugins {
24
id("java")
5+
id("maven-publish")
36
}
47

5-
group = "de.21no"
6-
version = "1.0.0-alpha"
7-
88
repositories {
99
mavenCentral()
1010
}
@@ -26,9 +26,65 @@ dependencies {
2626
testImplementation("org.junit.jupiter:junit-jupiter")
2727
// https://mvnrepository.com/artifact/org.mockito/mockito-core
2828
testImplementation("org.mockito:mockito-core:5.14.2")
29-
3029
}
31-
3230
tasks.test {
3331
useJUnitPlatform()
34-
}
32+
}
33+
34+
// Dynamically set the version from the latest Git tag
35+
version = run {
36+
// Function to check if Git is available
37+
fun isGitAvailable(): Boolean {
38+
return try {
39+
exec {
40+
commandLine("git", "--version")
41+
standardOutput = ByteArrayOutputStream()
42+
errorOutput = ByteArrayOutputStream()
43+
isIgnoreExitValue = true
44+
}.exitValue == 0
45+
} catch (e: Exception) {
46+
false
47+
}
48+
}
49+
50+
if (isGitAvailable()) {
51+
try {
52+
val stdout = ByteArrayOutputStream()
53+
exec {
54+
commandLine("git", "describe", "--tags", "--abbrev=0")
55+
standardOutput = stdout
56+
errorOutput = ByteArrayOutputStream()
57+
isIgnoreExitValue = true
58+
}
59+
val gitTag = stdout.toString().trim()
60+
gitTag.ifEmpty {
61+
"1.0.0-SNAPSHOT" // Default version if no tag is found
62+
}
63+
} catch (e: Exception) {
64+
"1.0.0-SNAPSHOT" // Default version if Git command fails
65+
}
66+
} else {
67+
"1.0.0-SNAPSHOT" // Default version if Git is not available
68+
}
69+
}
70+
71+
publishing {
72+
publications {
73+
create<MavenPublication>("gpr") {
74+
from(components["java"])
75+
groupId = "de.n21no.realtime.pubsub"
76+
artifactId = "core"
77+
version = version
78+
}
79+
}
80+
repositories {
81+
maven {
82+
name = "GitHubPackages"
83+
url = uri("https://maven.pkg.github.com/BackendStack21/realtime-pubsub-client-java")
84+
credentials {
85+
username = System.getenv("GITHUB_ACTOR")
86+
password = System.getenv("GITHUB_TOKEN")
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)