Skip to content

Commit 787555a

Browse files
Implement build and publish Github actions (#49)
* Initial commit for Github actions * Make uploadArchives conditional on the presence of Sonatype credentials * Turn off signing conditionally * Really make signing optional * Add testing output * Test publish step * Attempt to fix path for upload-artifacts action * Use v4 for upload/download-artifacts * Fix signing logic to use in memory keys * Fix push logic in the bump version step * Use checkout@v4 * Add a rebase before committing * Hard-code branch name... * Prepare version 1.0.9-SNAPSHOT for development [skip ci] * Prepare for merge * Remove .vscode folder * Add .vscode to the .gitignore * Split into multiple files * Prepare version 1.0.9-SNAPSHOT for development [skip ci] * Clean up TODOs --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 421d7ba commit 787555a

File tree

5 files changed

+123
-4
lines changed

5 files changed

+123
-4
lines changed

.github/workflows/build.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Build
2+
3+
on:
4+
pull_request:
5+
branches: [ main, master ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
environment: snapshot
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up JDK 8
14+
uses: actions/setup-java@v3
15+
with:
16+
java-version: '8'
17+
distribution: 'temurin'
18+
cache: 'gradle'
19+
- name: Setup Gradle
20+
uses: gradle/gradle-build-action@v2
21+
with:
22+
cache-read-only: false
23+
gradle-home-cache-cleanup: true
24+
- name: Build
25+
run: ./gradlew build

.github/workflows/publish.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Publish
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
environment: production
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up JDK 8
14+
uses: actions/setup-java@v3
15+
with:
16+
java-version: '8'
17+
distribution: 'temurin'
18+
cache: 'gradle'
19+
- name: Setup Gradle
20+
uses: gradle/gradle-build-action@v2
21+
- name: Set release version
22+
run: |
23+
# Remove -SNAPSHOT from version
24+
sed -i 's/\(version "[0-9.]*\)-SNAPSHOT"/\1"/' build.gradle
25+
- name: Publish package
26+
env:
27+
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
28+
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
29+
SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
30+
SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
31+
SIGNING_KEY: ${{ secrets.SIGNING_KEY }}
32+
run: |
33+
./gradlew build uploadArchives
34+
- name: Bump version
35+
id: bump_version
36+
run: |
37+
# Get current version without SNAPSHOT
38+
CURRENT_VERSION=$(grep "version" build.gradle | sed 's/version "\(.*\)"/\1/')
39+
40+
# Split into parts
41+
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
42+
43+
# Increment patch version
44+
NEXT_PATCH=$((VERSION_PARTS[2] + 1))
45+
46+
# Create new version
47+
NEXT_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$NEXT_PATCH-SNAPSHOT"
48+
49+
# Update build.gradle
50+
sed -i "s/version \".*\"/version \"$NEXT_VERSION\"/" build.gradle
51+
52+
# Make the new version available to other steps
53+
echo "next_version=$NEXT_VERSION" >> $GITHUB_OUTPUT
54+
- name: Commit version bump
55+
env:
56+
NEXT_VERSION: ${{ steps.bump_version.outputs.next_version }}
57+
BRANCH_NAME: github_actions
58+
run: |
59+
git config user.name "github-actions[bot]"
60+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
61+
git add --all
62+
git commit -m "Prepare version $NEXT_VERSION for development [skip ci]"
63+
git pull --rebase origin $BRANCH_NAME
64+
git push origin HEAD:$BRANCH_NAME

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
.idea/
33
out/
44
build/
5-
.DS_Store
5+
.DS_Store
6+
secring.gpg
7+
.vscode

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java temurin-8.0.392+8

build.gradle

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,51 @@ task sourcesJar(type: Jar) {
2929
from sourceSets.main.allSource
3030
}
3131

32+
test {
33+
testLogging {
34+
events "passed", "skipped", "failed"
35+
exceptionFormat "full"
36+
showStandardStreams = true
37+
}
38+
}
39+
3240
artifacts {
3341
archives jar, javadocJar, sourcesJar
3442
}
3543

44+
def getSonatypeUsername() {
45+
return System.getenv('SONATYPE_USERNAME')
46+
}
47+
48+
def getSonatypePassword() {
49+
return System.getenv('SONATYPE_PASSWORD')
50+
}
51+
52+
def hasCredentials() {
53+
def result = getSonatypeUsername() != null && getSonatypePassword() != null
54+
if (!result) {
55+
println "Cannot publish package since Sonatype credentials are not set. Please set SONATYPE_USERNAME and SONATYPE_PASSWORD environment variables."
56+
}
57+
return result
58+
}
59+
3660
signing {
61+
required { gradle.taskGraph.hasTask("uploadArchives") }
62+
useInMemoryPgpKeys(System.getenv('SIGNING_KEY'), System.getenv('SIGNING_PASSWORD'))
3763
sign configurations.archives
3864
}
3965

4066
uploadArchives {
4167
repositories {
4268
mavenDeployer {
43-
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
69+
beforeDeployment { deployment -> signing.signPom(deployment) }
4470

4571
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
46-
authentication(userName: sonatypeUsername, password: sonatypePassword)
72+
authentication(userName: getSonatypeUsername(), password: getSonatypePassword())
4773
}
4874

4975
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
50-
authentication(userName: sonatypeUsername, password: sonatypePassword)
76+
authentication(userName: getSonatypeUsername(), password: getSonatypePassword())
5177
}
5278

5379
pom.project {
@@ -80,4 +106,5 @@ uploadArchives {
80106
}
81107
}
82108
}
109+
onlyIf { hasCredentials() }
83110
}

0 commit comments

Comments
 (0)