Search code examples
androidgradlebuild.gradlecirclecicircleci-2.0

Response for curl is empty beeing called from gradle on CircleCI


I have a task in gradle which downloads translations for android APP

task('downloadTranslations') {
    ext.downloadPOTranslation = { stringsFile, projectId, lang ->
    def f = new File(stringsFile)
    if (f.exists()) {
        println("Delete file: " + f.absolutePath)
        f.delete()
    }

    println("Get lang: " + lang + " for project: " + projectId)

    def requestArray = ['curl', '-X', 'POST', "\"https://api.poeditor.com/v2/projects/export\"",
                   '-d', 'api_token=\"{API_KEY}\"',
                   '-d', 'id=\"' + projectId + '\"',
                   '-d', 'language=\"' + lang + '\"',
                   '-d', 'type=\"android_strings\"']

    println(requestArray)
    def request = requestArray.execute()

    request.waitFor()
    def response = request.text
    if (response.isEmpty()) {
        def errorResponse = request.err.text
        println("Error response from server:")
        println(errorResponse)
    } else {
        println("Response from server:")
        println(response)
    }

    ...

}

doLast {
    downloadPOTranslation("src/main/res/values/strings.xml", "projectNumber", "en-gb")
}

}

When I call this task from local machine from CLI or Android Studio everything works fine:

Get lang: en-gb for project: 250751
[curl, -X, POST, "https://api.poeditor.com/v2/projects/export", -d, api_token="API_TOKEN", -d, id="250751", -d, language="en-gb", -d, type="android_strings"]
Response from server:
{"response":{"status":"success","code":"200","message":"OK"},"result":{"url":"https:\/\/api.poeditor.com\/v2\/download\/file\/FILENUMBER"}}

But when I try exact the same on CircleCI I got an error:

Get lang: en-gb for project: 250751
[curl, -X, POST, "https://api.poeditor.com/v2/projects/export", -d, api_token="API_TOKEN", -d, id="250751", -d, language="en-gb", -d, type="android_strings"]

FAILURE: Build failed with an exception.

* Where:
Build file '/home/circleci/code/app/build.gradle' line: 204

* What went wrong:
Execution failed for task ':app:downloadTranslations'.
> java.io.IOException: Stream closed

Stream closed happens because response.isEmpty() is true and request.err is closed.

So why it could be so that Gradle fetch an empty response from server while same works locally?

I've also tried SSH to CircleCI and run curl directly from CLI - response is not empty.


Solution

  • The issue was in difference betwee ' and ".

    I don't know why but probably 'curl' was some kind of ignored.

    So

        def requestArray = ["curl",
                            "-X", "POST",
                            "-d","api_token=API_KEY",
                            "-d", "id=" + projectId + "",
                            "-d", "language=" + lang + "",
                            "-d", "type=android_strings",
                            "https://api.poeditor.com/v2/projects/export",]
    

    Works fine.