15

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

Community
  • 1
  • 1
scana
  • 2,785
  • 2
  • 25
  • 49

3 Answers3

13

In YAML, colons are delimiters that separate map keys and values.

What you have now:

curl -H 'Authorization: token someToken' "https://api.github.com/repos/:owner/:repo/releases/tags/$TRAVIS_TAG"

​ is a map with key curl -H 'Authorization and value token someToken' "https://api.github.com/repos/:owner/:repo/releases/tags/$TRAVIS_TAG". You can see how this creeps into the build script.

What you want is a properly quoted string:

after_deploy:
  - "curl -H 'Authorization: token someToken' \"https://api.github.com/repos/:owner/:repo/releases/tags/$TRAVIS_TAG\""
banzaiman
  • 2,631
  • 18
  • 27
  • I see this was suggested earlier by набиячлэвэлиь. How did it fail? – banzaiman Jun 10 '16 at 01:48
  • I have following in my .tarvis file which works fine:`after_success: - echo somejson > buildset.json - "curl -H 'Content-Type: application/json' --request POST --data @buildset.json \"https://hooks.website.com/catch/jhsgdjsag76t76\"" ` But when I use it like following, it fails. `after_success: - echo somejson > buildset.json - if [ "$TRAVIS_PULL_REQUEST" != "false" ] ; then "curl -H 'Content-Type: application/json' --request POST --data @buildset.json \"https://hooks.website.com/catch/jhsgdjsag76t76\"" ; fi` Whats going wrong here? @banzaiman – Gaurav N Jul 20 '18 at 04:04
8

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.

scana
  • 2,785
  • 2
  • 25
  • 49
0

you can just change the syntax and get it running directly.

example slack webhook

 - 'curl args:  -X POST -H ''Content-type: application/json'' --data ''{"text":"Hello, World!"}'' https://hooks.slack.com/services/####/###############'
rMili
  • 104
  • 7