I'm using gradle to automate docker publishing and tagging. I currently have the following set of tasks:
task dockerTagLatest(type: Exec) {
description "Build a Docker image of the current version and tag it as 'latest'"
dependsOn 'docker'
group 'Publishing'
commandLine(['docker', 'tag', dockerImageName, dockerImageLatest])
}
task dockerPush(type: Exec, overwrite: true) {
description 'Push the Docker image of the current version to the internal Docker hub'
group 'Publishing'
mustRunAfter 'dockerTagLatest'
commandLine 'docker', 'push', dockerImageName
}
task dockerPushLatestTag(type: Exec) {
description "Push the 'latest' tag to the internal Docker hub"
group 'Publishing'
mustRunAfter 'dockerTagLatest'
commandLine 'docker', 'push', dockerImageLatest
}
task dockerPublish() {
description "Push the Docker image of the current version and the 'latest' tag to the internal Docker hub"
dependsOn 'dockerTagLatest'
dependsOn 'dockerPush'
dependsOn 'dockerPushLatestTag'
group 'Publishing'
}
Would it be better to have something like this?
task dockerPublish(type: Exec) {
commandLine 'bash', '-e', '-c', """
docker tag ...
docker push ...
docker push ...
"""
}
Obviously the second approach is not Windows friendly, but disregarding that for the moment, is it better to have a set of dependent Exec tasks, or to lump all of the command line commands into a single task? I've gotten feedback that the latter is more readable, but I think the first approach is more Gradle-like. Thoughts?
Executing Multiple Tasks You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.
Press ⌃⌃ (macOS), or Ctrl+Ctrl (Windows/Linux), type "gradle" followed by the gradle task name or names. We can, of course, run Gradle commands from the terminal window inside IntelliJ IDEA. Open this with ⌥F12 (macOS), or Alt+F12 (Windows/Linux).
Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.
Is there ever a scenario where you wouldn't want to run all of these tasks in that exact order? Could any of them be parallelized by future Gradle features?
If the answer to either of those is "yes", then you may want to use separate tasks; otherwise one task is just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With