Skip to content

Commit 642aae4

Browse files
authored
Merge pull request #1016 from meshtastic/ci/kmp-build-cache
ci(kmp): add remote HTTP build cache and Konan toolchain caching
2 parents b73e5ad + f92ce54 commit 642aae4

5 files changed

Lines changed: 111 additions & 0 deletions

File tree

.github/workflows/kmp-pull-request.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ on:
1010
permissions:
1111
contents: read
1212

13+
env:
14+
GRADLE_CACHE_URL: ${{ secrets.GRADLE_CACHE_URL }}
15+
GRADLE_CACHE_USERNAME: ${{ secrets.GRADLE_CACHE_USERNAME }}
16+
GRADLE_CACHE_PASSWORD: ${{ secrets.GRADLE_CACHE_PASSWORD }}
17+
1318
jobs:
1419
build-kmp:
1520
runs-on: macos-latest
@@ -29,5 +34,13 @@ jobs:
2934
- name: Setup Gradle
3035
uses: gradle/actions/setup-gradle@v6
3136

37+
- name: Cache Konan (Kotlin/Native toolchain)
38+
uses: actions/cache@v6
39+
with:
40+
path: ~/.konan
41+
key: konan-${{ runner.os }}-${{ hashFiles('packages/kmp/build.gradle.kts', 'packages/kmp/gradle/wrapper/gradle-wrapper.properties') }}
42+
restore-keys: |
43+
konan-${{ runner.os }}-
44+
3245
- name: Build KMP package
3346
run: packages/kmp/gradlew --no-daemon -p packages/kmp build -PVERSION_NAME=0.0.0-pr

.github/workflows/publish-kmp.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ on:
1919
permissions:
2020
contents: read
2121

22+
env:
23+
GRADLE_CACHE_URL: ${{ secrets.GRADLE_CACHE_URL }}
24+
GRADLE_CACHE_USERNAME: ${{ secrets.GRADLE_CACHE_USERNAME }}
25+
GRADLE_CACHE_PASSWORD: ${{ secrets.GRADLE_CACHE_PASSWORD }}
26+
2227
jobs:
2328
build-kmp:
2429
runs-on: macos-latest
@@ -59,6 +64,14 @@ jobs:
5964
- name: Setup Gradle
6065
uses: gradle/actions/setup-gradle@v6
6166

67+
- name: Cache Konan (Kotlin/Native toolchain)
68+
uses: actions/cache@v6
69+
with:
70+
path: ~/.konan
71+
key: konan-${{ runner.os }}-${{ hashFiles('packages/kmp/build.gradle.kts', 'packages/kmp/gradle/wrapper/gradle-wrapper.properties') }}
72+
restore-keys: |
73+
konan-${{ runner.os }}-
74+
6275
- name: Build KMP package
6376
run: packages/kmp/gradlew --no-daemon -p packages/kmp clean build -PVERSION_NAME=${{ steps.version.outputs.VERSION }}
6477

@@ -87,6 +100,14 @@ jobs:
87100
- name: Setup Gradle
88101
uses: gradle/actions/setup-gradle@v6
89102

103+
- name: Cache Konan (Kotlin/Native toolchain)
104+
uses: actions/cache@v6
105+
with:
106+
path: ~/.konan
107+
key: konan-${{ runner.os }}-${{ hashFiles('packages/kmp/build.gradle.kts', 'packages/kmp/gradle/wrapper/gradle-wrapper.properties') }}
108+
restore-keys: |
109+
konan-${{ runner.os }}-
110+
90111
- name: Publish to Maven Central
91112
env:
92113
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }}

.github/workflows/snapshot-kmp.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ on:
1515
permissions:
1616
contents: read
1717

18+
env:
19+
GRADLE_CACHE_URL: ${{ secrets.GRADLE_CACHE_URL }}
20+
GRADLE_CACHE_USERNAME: ${{ secrets.GRADLE_CACHE_USERNAME }}
21+
GRADLE_CACHE_PASSWORD: ${{ secrets.GRADLE_CACHE_PASSWORD }}
22+
1823
jobs:
1924
publish-snapshot:
2025
runs-on: macos-latest
@@ -36,6 +41,14 @@ jobs:
3641
- name: Setup Gradle
3742
uses: gradle/actions/setup-gradle@v6
3843

44+
- name: Cache Konan (Kotlin/Native toolchain)
45+
uses: actions/cache@v6
46+
with:
47+
path: ~/.konan
48+
key: konan-${{ runner.os }}-${{ hashFiles('packages/kmp/build.gradle.kts', 'packages/kmp/gradle/wrapper/gradle-wrapper.properties') }}
49+
restore-keys: |
50+
konan-${{ runner.os }}-
51+
3952
- name: Set version name
4053
run: |
4154
# Name the snapshot after the latest tag (v-stripped), the commit count
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Shared remote HTTP Gradle build cache for Meshtastic KMP libraries.
3+
*
4+
* Credentials come from the GRADLE_CACHE_URL / GRADLE_CACHE_USERNAME /
5+
* GRADLE_CACHE_PASSWORD environment variables (CI secrets), or a
6+
* local.properties / config.properties entry for local use. Writes to the
7+
* cache happen only from trusted events (local dev, push, merge_group) with
8+
* credentials present, so pull-request runs (and credential-less fork PRs)
9+
* stay pull-only and cannot poison the cache.
10+
*/
11+
12+
def getMeshProperty(String key) {
13+
def env = System.getenv(key)
14+
if (env) return env
15+
def currentDir = settingsDir
16+
while (currentDir != null) {
17+
for (name in ["local.properties", "config.properties"]) {
18+
def f = new File(currentDir, name)
19+
if (f.exists()) {
20+
def props = new Properties()
21+
f.withInputStream { props.load(it) }
22+
if (props.containsKey(key)) return props.getProperty(key)
23+
}
24+
}
25+
currentDir = currentDir.parentFile
26+
}
27+
return null
28+
}
29+
30+
buildCache {
31+
local {
32+
enabled = true
33+
}
34+
remote(HttpBuildCache) {
35+
// Some cache servers return 403 on "Expect: 100-continue".
36+
useExpectContinue = false
37+
def cacheUrl = getMeshProperty("GRADLE_CACHE_URL")?.trim()
38+
def cacheUsername = getMeshProperty("GRADLE_CACHE_USERNAME")?.trim()
39+
def cachePassword = getMeshProperty("GRADLE_CACHE_PASSWORD")?.trim()
40+
if (cacheUrl) {
41+
// HTTPS + valid TLS enforced (no allowInsecureProtocol / no
42+
// allowUntrustedServer): the cache server must present a trusted
43+
// certificate over TLS.
44+
url = cacheUrl.endsWith("/") ? cacheUrl : "${cacheUrl}/"
45+
if (cacheUsername && cachePassword) {
46+
credentials {
47+
username = cacheUsername
48+
password = cachePassword
49+
}
50+
}
51+
// Write only from trusted events (local dev, push to a protected
52+
// branch, or the merge queue) with credentials — never from
53+
// pull_request runs, so unmerged code can't poison the cache.
54+
def eventName = System.getenv("GITHUB_EVENT_NAME")
55+
def trustedForPush = eventName == null || eventName == "push" || eventName == "merge_group"
56+
push = (cacheUsername && cachePassword && trustedForPush)
57+
enabled = true
58+
} else {
59+
enabled = false
60+
}
61+
}
62+
}

packages/kmp/settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ dependencyResolutionManagement {
1313
}
1414
}
1515

16+
apply(from = "gradle/build-cache.settings.gradle")
17+
1618
rootProject.name = "protobufs"

0 commit comments

Comments
 (0)