Search code examples
bashgradleenvironment-variables

Gradle Environment variables. Load from file


I am new to Gradle.

Currently I have this task:

task fooTask {
    doLast {
        exec {
            environment 'FOO_KEY', '1234567' // Load from file here!
            commandLine 'fooScript.sh'
        }
    }
}

fooScript.sh

#!/bin/bash
echo $FOO_KEY

Everything works great. But I have env.file with all needed environment variables. This file is used in Docker builder.

env.file

FOO_KEY=1234567

Question: how can I use env.file together with Gradle environment to load all needed env. params?


Solution

  • What about this :

    task fooTask {
        doLast {
            exec {
                file('env.file').readLines().each() {
                    def (key, value) = it.tokenize('=')
                    environment key, value
                }
                commandLine 'fooScript.sh'
            }
        }
    }