Created
February 24, 2020 21:12
-
-
Save kirklewis/1b5ceaa24adfeac1f233770a8540d85a to your computer and use it in GitHub Desktop.
Groovy 3.0.0 YamlBuilder Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.yaml.YamlBuilder | |
class DockerService { List ports; List volumes; Boolean tty; } | |
def ordersApiService = new DockerService( | |
ports: ['3040:3040'], | |
volumes: ['./:/opt'], | |
tty: true, | |
) | |
def dockerCompose = new YamlBuilder() | |
dockerCompose { | |
version '3' | |
services { | |
// build YAML from another object. Hyphenated properties must be quoted | |
'orders-api'(ordersApiService) | |
// build YAML in place | |
cypress { | |
image 'cypress/included:3.6.0' | |
// lists using array literals [] | |
depends_on([ | |
'orders-api' | |
]) | |
environment([ | |
'CYPRESS_baseUrl=http://orders-api:3040', | |
'CYPRESS_host=api.server.qa' | |
]) | |
// lists using Closures | |
volumes(['./:/opt']).each { it } | |
working_dir '/opt' | |
} | |
} | |
} | |
println dockerCompose.toString() | |
assert dockerCompose.toString() == '''\ | |
--- | |
version: "3" | |
services: | |
orders-api: | |
tty: true | |
volumes: | |
- "./:/opt" | |
ports: | |
- "3040:3040" | |
cypress: | |
image: "cypress/included:3.6.0" | |
depends_on: | |
- "orders-api" | |
environment: | |
- "CYPRESS_baseUrl=http://orders-api:3040" | |
- "CYPRESS_host=api.server.qa" | |
volumes: | |
- "./:/opt" | |
working_dir: "/opt" | |
''' | |
// written in Groovy 3.0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment