1515 * along with this program; if not, see https://sonarsource.com/license/ssal/
1616 */
1717import org.gradle.kotlin.dsl.registering
18- import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
1918import org.sonarsource.cloudnative.gradle.GO_BINARY_OUTPUT_DIR
2019import org.sonarsource.cloudnative.gradle.GoBuild
2120import org.sonarsource.cloudnative.gradle.allGoSourcesAndMakeScripts
2221import org.sonarsource.cloudnative.gradle.callMake
2322import org.sonarsource.cloudnative.gradle.getArchitecture
2423import org.sonarsource.cloudnative.gradle.getPlatform
2524import org.sonarsource.cloudnative.gradle.goSources
25+ import org.sonarsource.cloudnative.gradle.goVersion
26+ import org.sonarsource.cloudnative.gradle.isCi
27+ import org.sonarsource.cloudnative.gradle.isCrossCompile
28+
29+ plugins {
30+ id(" org.sonarsource.cloud-native.go-docker-environment" )
31+ }
2632
2733val goBinaries: Configuration by configurations.creating
2834val goBinariesJar by tasks.registering(Jar ::class ) {
2935 group = " build"
30- dependsOn(" compileGo" )
3136 archiveClassifier.set(" binaries" )
3237 from(GO_BINARY_OUTPUT_DIR )
3338}
3439artifacts.add(goBinaries.name, goBinariesJar)
3540
36- val goVersion = providers.environmentVariable(" GO_VERSION" )
37- .orElse(providers.gradleProperty(" goVersion" ))
38- .orNull ? : error(" Either `GO_VERSION` env variable or `goVersion` Gradle property must be set" )
39- val isCrossCompile = providers.environmentVariable(" GO_CROSS_COMPILE" ).orElse(" 0" )
40- val isCi: Boolean = System .getenv(" CI" )?.equals(" true" ) == true
41- val goBuildExtension = extensions.create(" goBuild" , GoBuild ::class )
41+ val goBuildExtension = extensions.findByType<GoBuild >() ? : extensions.create<GoBuild >(" goBuild" )
4242goBuildExtension.dockerWorkDir.convention(" /home/sonarsource/${project.name} " )
4343goBuildExtension.additionalOutputFiles.convention(emptySet())
44+ goBuildExtension.dockerCommands.convention(
45+ mapOf (
46+ " dockerCompileGo" to " ./make.sh clean && ./make.sh build ${getPlatform()} ${getArchitecture()} && ./make.sh test"
47+ )
48+ )
4449
45- if (isCi) {
50+ if (isCi() ) {
4651 val cleanGoCode by tasks.registering(Exec ::class ) {
4752 description = " Clean all compiled version of the go code."
4853 group = " build"
@@ -63,6 +68,7 @@ if (isCi) {
6368
6469 callMake(" build" )
6570 }
71+ goBinariesJar.configure { dependsOn(compileGo) }
6672
6773 val goLangCiLint by tasks.registering(Exec ::class ) {
6874 description = " Run an external Go linter."
@@ -116,94 +122,11 @@ if (isCi) {
116122 dependsOn(goLangCiLint)
117123 }
118124} else {
119- val buildDockerImage by tasks.registering(Exec ::class ) {
120- description = " Build the docker image to build the Go code."
121- group = " build"
122-
123- inputs.file(goBuildExtension.dockerfile)
124- inputs.file(" $projectDir /go.mod" )
125- inputs.file(" $projectDir /go.sum" )
126- // Task outputs are not set, because it is too difficult to check if image is built;
127- // We can ignore Gradle caches here, because Docker takes care of its own caches anyway.
128- errorOutput = System .out
129-
130- val uidProvider = objects.property<Long >()
131- val os = DefaultNativePlatform .getCurrentOperatingSystem()
132- if (os.isLinux || os.isMacOsX) {
133- // UID of the user inside the container should match this of the host user, otherwise files from the host will be not accessible by the container.
134- val uid = com.sun.security.auth.module.UnixSystem ().uid
135- uidProvider.set(uid)
136- }
137-
138- val noTrafficInspection = " false" == System .getProperty(" trafficInspection" )
139-
140- val arguments = buildList {
141- add(" docker" )
142- add(" buildx" )
143- add(" build" )
144- add(" --file" )
145- add(goBuildExtension.dockerfile.asFile.get().absolutePath)
146- if (noTrafficInspection) {
147- add(" --build-arg" )
148- add(" BUILD_ENV=dev" )
149- } else {
150- add(" --network=host" )
151- add(" --build-arg" )
152- add(" BUILD_ENV=dev_custom_cert" )
153- }
154- if (uidProvider.isPresent) {
155- add(" --build-arg" )
156- add(" UID=${uidProvider.get()} " )
157- }
158- add(" --build-arg" )
159- add(" GO_VERSION=$goVersion " )
160- add(" --platform" )
161- add(" linux/amd64" )
162- add(" -t" )
163- add(" ${project.name} -builder" )
164- add(" --progress" )
165- add(" plain" )
166- add(" ${project.projectDir} " )
167- }
168-
169- commandLine(arguments)
170- }
171-
172- val compileGo by tasks.registering(Exec ::class ) {
173- description = " Build the Go executable inside a Docker container."
174- group = " build"
175- dependsOn(buildDockerImage)
176- errorOutput = System .out
177-
178- inputs.files(allGoSourcesAndMakeScripts())
179- inputs.property(" goCrossCompile" , isCrossCompile)
180- outputs.files(goBuildExtension.additionalOutputFiles)
181- outputs.dir(GO_BINARY_OUTPUT_DIR )
182- outputs.cacheIf { true }
183-
184- val platform = getPlatform()
185- val arch = getArchitecture()
186-
187- val workDir = goBuildExtension.dockerWorkDir.get()
188- commandLine(
189- " docker" ,
190- " run" ,
191- " --rm" ,
192- " --network=host" ,
193- " --platform" ,
194- " linux/amd64" ,
195- " --mount" ,
196- " type=bind,source=${project.projectDir} ,target=$workDir " ,
197- " --env" ,
198- " GO_CROSS_COMPILE=${inputs.properties[" goCrossCompile" ]} " ,
199- " ${project.name} -builder" ,
200- " bash" ,
201- " -c" ,
202- " cd $workDir && ./make.sh clean && ./make.sh build $platform $arch && ./make.sh test"
203- )
204- }
205-
125+ val dockerTaskNames = goBuildExtension.dockerCommands.map { it.keys }
206126 tasks.named(" assemble" ) {
207- dependsOn(compileGo)
127+ dependsOn(dockerTaskNames)
128+ }
129+ goBinariesJar.configure {
130+ dependsOn(dockerTaskNames)
208131 }
209132}
0 commit comments