Skip to content

Commit 0d97d26

Browse files
Kirill Andreevaugi
authored andcommitted
Fix exposing env variables and system properties under service name having dashes replaced with underscores
1 parent 7647f71 commit 0d97d26

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/main/groovy/com/avast/gradle/dockercompose/ComposeSettings.groovy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ abstract class ComposeSettings {
291291
}
292292

293293
protected Map<String, Object> createEnvironmentVariables(String variableName, ContainerInfo ci) {
294-
def serviceName = variableName.replaceAll('-', '_')
294+
def serviceName = replaceV2Separator(variableName)
295295
Map<String, Object> environmentVariables = [:]
296296
environmentVariables.put("${serviceName}_HOST".toString(), ci.host)
297297
environmentVariables.put("${serviceName}_CONTAINER_HOSTNAME".toString(), ci.containerHostname)
@@ -301,7 +301,7 @@ abstract class ComposeSettings {
301301
}
302302

303303
protected Map<String, Object> createSystemProperties(String variableName, ContainerInfo ci) {
304-
def serviceName = variableName.replaceAll('-', '_')
304+
def serviceName = replaceV2Separator(variableName)
305305
Map<String, Object> systemProperties = [:]
306306
systemProperties.put("${serviceName}.host".toString(), ci.host)
307307
systemProperties.put("${serviceName}.containerHostname".toString(), ci.containerHostname)
@@ -310,6 +310,10 @@ abstract class ComposeSettings {
310310
systemProperties
311311
}
312312

313+
static String replaceV2Separator(String serviceName) {
314+
serviceName.replaceAll('-(\\d+)$', '_$1')
315+
}
316+
313317
boolean removeOrphans() {
314318
composeExecutor.version >= VersionNumber.parse('1.7.0') && this.removeOrphans.get()
315319
}

src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,38 @@ class DockerComposePluginTest extends Specification {
287287
''']
288288
}
289289

290+
def "exposes environment variables and system properties for services having dash in service name"() {
291+
def f = Fixture.custom(composeFileContent)
292+
f.project.plugins.apply 'java'
293+
f.project.tasks.composeUp.up()
294+
Test test = f.project.tasks.test as Test
295+
when:
296+
f.project.dockerCompose.exposeAsEnvironment(test)
297+
f.project.dockerCompose.exposeAsSystemProperties(test)
298+
then:
299+
test.environment.containsKey('WEB-SERVICE_HOST')
300+
test.environment.containsKey('WEB-SERVICE_CONTAINER_HOSTNAME')
301+
test.environment.containsKey('WEB-SERVICE_TCP_80')
302+
test.environment.containsKey('WEB-SERVICE_UDP_81')
303+
test.systemProperties.containsKey('web-service.host')
304+
test.systemProperties.containsKey('web-service.containerHostname')
305+
test.systemProperties.containsKey('web-service.tcp.80')
306+
test.systemProperties.containsKey('web-service.udp.81')
307+
cleanup:
308+
f.project.tasks.composeDown.down()
309+
f.close()
310+
where:
311+
composeFileContent << ['''
312+
version: '2'
313+
services:
314+
web-service:
315+
image: nginx:stable
316+
ports:
317+
- 80
318+
- 81/udp
319+
''']
320+
}
321+
290322
private static boolean isRunningOnWindows() { System.properties['os.name'].toString().toLowerCase().startsWith('windows') }
291323
private static boolean isRunningOnMac() { System.properties['os.name'].toString().toLowerCase().startsWith('macos') || System.properties['os.name'].toString().toLowerCase().startsWith('mac os') }
292324

0 commit comments

Comments
 (0)