One-off tasks in a multi-project build

I have a multi-project build, but everything is encompassed by a single Git repository. As a part of my build, I need to perform some operations (e.g. a commit and push) against the repo, but this needs to be done exactly once at the end of the build, not once per project.

More generally, though, I’d like to know how to attach one-off tasks to the task graph, so that I can perform setup/teardown tasks before and/or after the build. What is the accepted approach to doing this? At the moment the way I’m doing it is with something like this:

project.gradle.taskGraph.addTaskExecutionListener(new TaskExecutionAdapter() {
    @Override
    void afterExecute(Task task, TaskState state) {
        if (task == project.gradle.taskGraph.allTasks.last()) {
            ...
        }
    }
})

Of course, this feels like a ridiculous hack, but I couldn’t find any API for enhancing the task graph itself (presumably it isn’t meant to be manipulated once the graph has been resolved).

Thanks.

There are project.gradle.buildStarted(Closure closure) and project.gradle.buildFinished(Closure closure) methods available.