Execute curl on deployment


#1

Hi I set up an environment variable called $DEPLOY_STG which has a value similar to this

curl -H "Content-Type: application/json" --data '{"source_type": "Branch", "source_name": "dev"}' -X POST https://registry.hub.docker.com/.....

And in my circleci.yml I have this

The problem is that in the logs of circleci apparently it doesn’t recognize the curl command parameters, should I escape them somehow? and if so, how? I’ve tried single quotes, double quotes, slashes but couldn’t get it right yet.

thanks in advance


#2

Hi,

This isn’t CircleCI’s behavior, this is bash’s behavior:

$ DEPLOY_STG="curl -H \"Content-Type: application/json\" --data '{\"source_type\": \"Branch\", \"source_name\": \"dev\"}' -X example.com"
$ $DEPLOY_STG
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: "Branch",
curl: (6) Could not resolve host: "source_name"
curl: (3) [globbing] unmatched close brace/bracket in column 6

The short explanation for this behavior is that bash splits arguments on spaces:

$ DEPLOY_STG="cat \"a b\""
$ $DEPLOY_STG
cat: "a: No such file or directory
cat: b": No such file or directory

vs

$ cat "a b"
cat: a b: No such file or directory

A nice way to solve the problem is to use bash -c:

$ DEPLOY_STG="curl -H \"Content-Type: application/json\" --data '{\"source_type\": \"Branch\", \"source_name\": \"dev\"}' -X POST example.com"
$ bash -c "$DEPLOY_STG"
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 50px;
        background-color: #fff;
        border-radius: 1em;
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        body {
            background-color: #fff;
        }
        div {
            width: auto;
            margin: 0 auto;
            border-radius: 0;
            padding: 1em;
        }
    }
    </style>
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is established to be used for illustrative examples in documents. You may use this
    domain in examples without prior coordination or asking for permission.</p>
    <p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

#3

thanks. haven’t known that.


#4

Still not working…

@anon1959517 This is the value I set in circleci

curl -H \"Content-Type: application/json\" --data '{\"source_type\": \"Branch\", \"source_name\": \"dev\"}' -X POST http://example.com

and what I have in my circleci.yml is

deployment
  staging:
    branch: dev
    commands:
      - bash -c "$DEPLOY_STG"

And I get this error now


#5

It works for me. How are you setting it? Are you sure it’s the value you think?


#6

@anon1959517 well the url is not http://example.com but something more like https://registry.hub.docker.com/u/something/whatever/trigger/9b9b3faa-bbcb-44a4-8a4c-05598e243a70/

the value is correct I mean I am copying and pasting into the “Environment Variables” section of circleci dashboard.

It works when i run it in my OSX from my terminal. but it doesn’t work when circleci triggers it.


#7

Try this
DEPLOY_STG=“curl -H ‘Content-Type: application/json’ --data ‘{‘source_type’: ‘Branch’, ‘source_name’: ‘dev’}’ -X example.com


#8