Skip to content

Commit

Permalink
feat: add common task for template
Browse files Browse the repository at this point in the history
  • Loading branch information
grv87 committed Apr 2, 2017
1 parent 59c9f40 commit a026409
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# gradle-packer-plugin
This plugin allows to run [Packer](http://www.packer.io/) builds from Gradle.

1. It parses Packer's templates and creates Gradle tasks to run Packer,
separate task for each builder in each template.
1. It parses Packer's templates and creates Gradle tasks to run Packer.
For each template it creates single task to run all builders at once
and separate task for each builder.

2. It could pass used-defined variables to Packer.

Expand Down Expand Up @@ -34,12 +35,11 @@ Plugin creates series of tasks for cleaning and running Packer builds.
If template has a `name` variable, it is used instead of template
filename. High-level task names are:

* `clean-<name variable or template filename>`
* `build-<name variable or template filename>`
* `clean-<name variable or template filename>-<build name>`
* `build-<name variable or template filename>-<build name>`

Note that there is no out-of-the-box single task to run all builds
defined in one template.

## Supported Packer configurations:
* Builders:
* `virtualbox-iso` and `virtualbox-ovf`.
Expand Down
48 changes: 45 additions & 3 deletions src/main/groovy/org/fidata/gradle/packer/GradlePackerPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class GradlePackerPlugin implements Plugin<Project> {
owners = (builder['source_ami_filter']['owners'] ?: []).collect { parseString(it, t.contextTemplateData) }
mostRecent = builder['source_ami_filter']['most_recent'] ?: false
}
t.inputs.property('sourceAMI', {
t.inputProperties['sourceAMI'] = {
List res = findAMI(
project, t.awsEnvironment,
parseString(builder['region'], t.contextTemplateData),
Expand All @@ -244,7 +244,7 @@ class GradlePackerPlugin implements Plugin<Project> {
res = [res[0]]
project.logger.info(sprintf('gradle-packer-plugin: sourceAMI value %s', [JsonOutput.toJson(res)]))
JsonOutput.toJson(res)
})
}

t.ext.AMIName = parseString(builder['ami_name'], t.contextTemplateData)
Map awsContextTemplateData = new HashMap(t.contextTemplateData)
Expand All @@ -256,7 +256,7 @@ class GradlePackerPlugin implements Plugin<Project> {
builder['ami_regions'] = [builder['region']] + builder['ami_regions']
for (region in builder['ami_regions']) {
region = parseString(region, t.contextTemplateData)
t.outputs.upToDateWhen {
t.upToDateWhen.push {
t.ext.outputAMI = findAMI(
project, t.awsEnvironment,
region,
Expand Down Expand Up @@ -430,7 +430,49 @@ class GradlePackerPlugin implements Plugin<Project> {
else processPostProcessors(p)
}
}
Task commonT = project.task("build-$imageName") {
group 'Build'
ext.cleanTask = project.task("clean-$imageName") {
group 'Clean'
shouldRunAfter validate
}
shouldRunAfter validate
mustRunAfter cleanTask
doLast {
project.exec {
commandLine(
[
'packer',
'build',
] +
customVariablesCmdLine +
(project.gradle.startParameter.logLevel <= LogLevel.DEBUG ? ['-debug'] : []) +
[
fileName
]
)
}
}
inputs.property 'customVariablesCmdLine', customVariablesCmdLine
}
for (t in ts.values()) {
commonT.cleanTask.dependsOn t.cleanTask
for (i in t.inputs.files)
commonT.inputs.file i
for (i in t.inputProperties) {
t.inputs.property i.key, i.value
commonT.inputs.property "${t.buildName}-${i.key}", i.value
}
for (o in t.outputs.files)
commonT.outputs.file o
Closure u = {
if (t.upToDateWhen.size() > 0)
t.upToDateWhen.every { it() }
else
true
}
t.outputs.upToDateWhen u
commonT.outputs.upToDateWhen u
if (t.ext.has('outputFileName')) {
project.logger.info(sprintf('gradle-packer-plugin: task %s has outputFileName %s', [t.name, t.outputFileName]))
t.outputs.file(t.outputFileName)
Expand Down

0 comments on commit a026409

Please sign in to comment.