|
| 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. Push is enabled |
| 7 | + * only when credentials are present, so fork PRs (which have no secrets) are |
| 8 | + * pull-only and cannot poison the cache. |
| 9 | + */ |
| 10 | + |
| 11 | +def getMeshProperty(String key) { |
| 12 | + def env = System.getenv(key) |
| 13 | + if (env) return env |
| 14 | + def currentDir = settingsDir |
| 15 | + while (currentDir != null) { |
| 16 | + for (name in ["local.properties", "config.properties"]) { |
| 17 | + def f = new File(currentDir, name) |
| 18 | + if (f.exists()) { |
| 19 | + def props = new Properties() |
| 20 | + f.withInputStream { props.load(it) } |
| 21 | + if (props.containsKey(key)) return props.getProperty(key) |
| 22 | + } |
| 23 | + } |
| 24 | + currentDir = currentDir.parentFile |
| 25 | + } |
| 26 | + return null |
| 27 | +} |
| 28 | + |
| 29 | +buildCache { |
| 30 | + local { |
| 31 | + enabled = true |
| 32 | + } |
| 33 | + remote(HttpBuildCache) { |
| 34 | + // Some cache servers return 403 on "Expect: 100-continue". |
| 35 | + useExpectContinue = false |
| 36 | + def cacheUrl = getMeshProperty("GRADLE_CACHE_URL")?.trim() |
| 37 | + def cacheUsername = getMeshProperty("GRADLE_CACHE_USERNAME")?.trim() |
| 38 | + def cachePassword = getMeshProperty("GRADLE_CACHE_PASSWORD")?.trim() |
| 39 | + if (cacheUrl) { |
| 40 | + url = cacheUrl.endsWith("/") ? cacheUrl : "${cacheUrl}/" |
| 41 | + if (cacheUsername && cachePassword) { |
| 42 | + credentials { |
| 43 | + username = cacheUsername |
| 44 | + password = cachePassword |
| 45 | + } |
| 46 | + } |
| 47 | + allowInsecureProtocol = true |
| 48 | + allowUntrustedServer = true |
| 49 | + // Push only when credentials exist -> fork PRs are pull-only. |
| 50 | + push = (cacheUsername && cachePassword) |
| 51 | + enabled = true |
| 52 | + } else { |
| 53 | + enabled = false |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments