Search code examples
curlyamltravis-ci

How to properly use curl in Travis-CI config file (YAML)?


Ok. This is slowly driving me crazy. I have set up CI on Travis for one of my projects. I'm running some JUnit tests and I would like to upload the tests results to my own server, so it's much easier to browse them.

Basically, all I want do is to call this:

curl -H 'Authorization: Token someToken' -X POST http://my.server.com -F filedata=@file.txt

So this is what I'm trying to do in .travis.yml file.

after_script:
 - curl -H 'Authorization: Token someToken' -X POST http://my.server.com -F filedata=@file.txt

The problem is that for the line above I'm getting an error which looks like this:

$ {:"curl -H '\"Authorization"=>"Token someToken\"' -X POST http://my.server.com -F filedata=@file.txt"}
/home/travis/build.sh: line 45: Token someToken"' -X POST http://my.server.com -F filedata=@file.txt}: No such file or directory

I've learned that in YAML colon represents a pair of key-value and I've found out that one can just use the quotes to escape the colon.

Well - this is the place where I am stuck. I tried to apply those quotes in many different ways but somehow each time I keep getting the same error all over again.

For example:

curl -H '"Authorization: Token someToken"'
curl -H "\"Authorization: Token someToken\""
curl -H "'Authorization: Token someToken'"
curl -H '"Authorization": Token someToken'

I feel like being stupid and I know that the fix for this is probably a simple thing, but I've felt into that "escape quotes while escaping quotes" thing and if anyone could just point me in the right direction, I would be really grateful.

I'm also linking to those questions as I tried to follow them to solve my problem:

Escaping colons in YAML

How to escape indicator characters (i.e. : or - ) in YAML


Solution

  • Ok - I've managed to solve (or hack) this problem, by creating simple bash script:

    #!/bin/bash
    curl -H 'Authorization: Token someToken' -X POST http://my.server.com -F filedata=@file.txt
    

    And then I proceed to call the script in .travis.yml file:

    - ./upload_script.sh
    

    All credits goes to @набиячлэвэлиь for suggesting me that solution in the comments.

    Any other - nicer - solutions are more than welcome.