Skip to content

Commit 60620e7

Browse files
authored
Enforce Google Maven Mirror on CI environments (#38586)
* Implement Maven Central mirroring in CI environments * Fix subproject buildscript classpath resolution for beam-test-gha using CI mirror fallback * Remove duplicated definition. * Discard the new settings file in buildSrc first. * Fix compilation error.
1 parent b6aaf42 commit 60620e7

5 files changed

Lines changed: 63 additions & 6 deletions

File tree

.github/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818

1919
buildscript {
2020
repositories {
21-
mavenCentral()
21+
def mirrorUrl = project.findProperty("mavenCentralMirrorUrl")
22+
boolean isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
23+
def useMirror = isCi && mirrorUrl
24+
25+
if (!useMirror) {
26+
mavenCentral()
27+
} else {
28+
maven { url mirrorUrl }
29+
}
2230
}
2331
dependencies {
2432
classpath group: 'org.yaml', name: 'snakeyaml', version: '2.2'

buildSrc/build.gradle.kts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18+
import java.util.Properties
19+
20+
val parentProperties = Properties().apply {
21+
val file = file("../gradle.properties")
22+
if (file.exists()) {
23+
file.inputStream().use { load(it) }
24+
}
25+
}
26+
27+
val mavenCentralMirrorUrl = parentProperties.getProperty("mavenCentralMirrorUrl")
28+
val isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
29+
val useMirror = isCi && !mavenCentralMirrorUrl.isNullOrBlank()
1830

1931
// Plugins for configuring _this build_ of the module
2032
plugins {
@@ -25,6 +37,13 @@ plugins {
2537

2638
// Define the set of repositories required to fetch and enable plugins.
2739
repositories {
40+
if (useMirror) {
41+
logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror for buildSrc.")
42+
}
43+
44+
if (useMirror) {
45+
maven { url = uri(mavenCentralMirrorUrl!!) }
46+
}
2847
maven { url = uri("https://plugins.gradle.org/m2/") }
2948
maven {
3049
url = uri("https://repo.spring.io/plugins-release/")

buildSrc/src/main/groovy/org/apache/beam/gradle/Repositories.groovy

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
package org.apache.beam.gradle
2020

2121
import org.gradle.api.Project
22-
2322
class Repositories {
2423

2524
static void register(Project project) {
25+
def mirrorUrl = project.findProperty("mavenCentralMirrorUrl")
26+
boolean isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
27+
def useMirror = isCi && mirrorUrl
28+
29+
if (useMirror) {
30+
project.logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror.")
31+
}
2632

2733
project.repositories {
2834
maven { url project.offlineRepositoryRoot }
@@ -36,7 +42,11 @@ class Repositories {
3642
return
3743
}
3844

39-
mavenCentral()
45+
if (!useMirror) {
46+
mavenCentral()
47+
} else {
48+
maven { url mirrorUrl }
49+
}
4050
mavenLocal()
4151

4252
// For Confluent Kafka dependencies
@@ -60,15 +70,19 @@ class Repositories {
6070
// Apply a plugin which provides the 'updateOfflineRepository' task that creates an offline
6171
// repository. This offline repository satisfies all Gradle build dependencies and Java
6272
// project dependencies. The offline repository is placed within $rootDir/offline-repo
63-
// but can be overridden by specifying '-PofflineRepositoryRoot=/path/to/repo'.
73+
// but can be overridden by specifying '-Pareas/offline-repo'.
6474
// Note that parallel build must be disabled when executing 'updateOfflineRepository'
6575
// by specifying '--no-parallel', see
6676
// https://github.com/mdietrichstein/gradle-offline-dependencies-plugin/issues/3
6777
project.apply plugin: "io.pry.gradle.offline_dependencies"
6878
project.offlineDependencies {
6979
repositories {
7080
mavenLocal()
71-
mavenCentral()
81+
if (!useMirror) {
82+
mavenCentral()
83+
} else {
84+
maven { url mirrorUrl }
85+
}
7286
maven { url "https://plugins.gradle.org/m2/" }
7387
maven { url "https://repo.spring.io/plugins-release" }
7488
maven { url "https://packages.confluent.io/maven/" }

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ flink_versions=1.17,1.18,1.19,1.20,2.0
4444
spark_versions=3,4
4545
# supported python versions
4646
python_versions=3.10,3.11,3.12,3.13,3.14
47+
48+
# Maven Central fallback mirror URL
49+
mavenCentralMirrorUrl=https://maven-central.storage-download.googleapis.com/maven2/

settings.gradle.kts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
import com.gradle.enterprise.gradleplugin.internal.extension.BuildScanExtensionWithHiddenFeatures
1919

2020
pluginManagement {
21+
val mavenCentralMirrorUrl = settings.providers.gradleProperty("mavenCentralMirrorUrl").orNull
22+
val isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
23+
val useMirror = isCi && !mavenCentralMirrorUrl.isNullOrBlank()
24+
25+
if (useMirror) {
26+
logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror.")
27+
}
28+
29+
repositories {
30+
if (useMirror) {
31+
maven { url = uri(mavenCentralMirrorUrl!!) }
32+
}
33+
gradlePluginPortal()
34+
}
2135
plugins {
2236
id("org.javacc.javacc") version "4.0.3" // enable the JavaCC parser generator
2337
}
@@ -28,7 +42,6 @@ plugins {
2842
id("com.gradle.common-custom-user-data-gradle-plugin") version "2.6.0"
2943
}
3044

31-
3245
// JENKINS_HOME and BUILD_ID set automatically during Jenkins execution
3346
val isJenkinsBuild = arrayOf("JENKINS_HOME", "BUILD_ID").all { System.getenv(it) != null }
3447
// GITHUB_REPOSITORY and GITHUB_RUN_ID set automatically during Github Actions run

0 commit comments

Comments
 (0)