`Permission denied` when running shell script in `Exec` task

Hello all! I’m trying to execute a shell script from a Gradle task, here’s all that I have for that:

tasks.register<Exec>("startDocker") {
    commandLine("./start-docker.sh")
        .workingDir("docker")
}

On running that task, I run into the following error:

Caused by: java.io.IOException: error=13, Permission denied

My thinking is that I have to use chmod +x to give executable permissions to that start-docker.sh file, but am struggling to determine how to do that. I’ve taken a shot with the following task:

tasks.register<Exec>("giveExecutablePermissionsForDockerScript") {
    commandLine("chmod", "+x", "start-docker.sh")
        .workingDir("docker")
}

Which I then make the startDocker one depend on in a larger task:

tasks.register<Test>("dockerTest") {
    dependsOn("giveExecutablePermissionsForDockerScript")
    dependsOn("startDocker").mustRunAfter("giveExecutablePermissionsForDockerScript")
    // ...
}

But to no avail! Any thoughts on how I could accomplish this? The greater context is that I’d like to use shell scripts to prepare a Docker environment that some tests rely on.

Is there a reason you do not for example simply use bmuschko/gradle-docker-plugin to do Docker operations?

1 Like

I had not looked into that, thanks for pointing me in that direction! More specifically I was looking to ultimately run docker compose, which that bmuschko/gradle-docker-plugin does not support. However, I did find avast/gradle-docker-compose-plugin from their documentation, which looks to be good for this use case.

However, I’m still curious about the general issue of giving executable permissions to a shell script.

Easiest, just use Java API.

tasks.register<Exec>("startDocker") {
    commandLine("./start-docker.sh")
        .workingDir("docker")
    doFirst {
        file("start-docker.sh").setExecutable(true)
    }
}