Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aev-mambro2/e080ea2d52126c3b701a1cd43400dd58 to your computer and use it in GitHub Desktop.
Save aev-mambro2/e080ea2d52126c3b701a1cd43400dd58 to your computer and use it in GitHub Desktop.
How to deploy from multiple projects to multiple target servers using Gradle CDCI
["server-1", "server-2", "server-3"].each { server ->
task "deploy-to-$server" (type: Copy) {
description: "Copies the project executables to $server"
enabled: true
group: "distribution"
afterEvaluate { Project project ->
from: "$distDir/$distZip"
into: "\\\\$server\\share\\${project.name}\\"
}
}
}
@aev-mambro2
Copy link
Author

aev-mambro2 commented Mar 10, 2022

A Copy task in Gradle can take input from multiple locations, but it can have only 1 output target. In a multi-project setup, we would hate to set up a separate deployment task for each project to each server. Luckily a Gradle build script is code, too. Code that gets interpreted and executed. Thus we can generate multiple tasks using just 1 declaration. That's what this gist does: it creates a separate deployment task for each of the target servers.

This works for Gradle versions 5.4.1 and up. Maybe earlier, too, but we didn't test that.

@aev-mambro2
Copy link
Author

In the Gradle documentation this is known as "dynamic task creation". It doesn't create dynamic tasks... instead it dynamically creates tasks.

@aev-mambro2
Copy link
Author

We have tried other variations as well. None of these worked:

  • put the target server expansion inside the task, inside the project-specific 'afterEvaluate' scope;
  • put the target server expansion inside the task, enveloping the project-specific 'afterEvaluate' scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment