Circle.yml environment variables syntax change (json: cannot unmarshal array)

circle.yml

#1

It looks like the syntax changed for environment variables (from a list to a map) but hasn’t been updated in the getting started guide.

Build-agent version 0.0.2333-53afbdf (2017-02-11T12:40:08+0000)
Error: Configuration errors in circle.yml: 2 errors occurred:

* json: cannot unmarshal array into Go value of type map[string]string
* json: cannot unmarshal array into Go value of type map[string]string

Changed from

version: 2
...
stages:
  build:
    environment:
      - FOO: bar
      - HELLO: world
    steps:
      - type: checkout
...

to

version: 2
...
stages:
  build:
    environment:
      FOO: bar
      HELLO: world
    steps:
      - type: checkout
...

#2

A related new change: An environment value of true gets interpreted as a bool and results in this failure

For example

environment:
  DEBUG: true

results in the error below, whereas
environment:
DEBUG: “true”

will succeed.

Build-agent version 0.0.2357-432709b (2017-02-12T16:55:48+0000)
Configuration errors in circle.yml: 2 errors occurred:

  • json: cannot unmarshal bool into Go value of type string
  • json: cannot unmarshal bool into Go value of type string

Builds stuck in Queue?
#3

I just received this, thanks for the pointer. I understand its in beta however some backward support would be good as I now have to adjust over 20 projects :frowning:


#4

Thanks for sharing this Silas. I went and checked the current and 12 historical versions of the getting started guide to get to the bottom of this. The circle.yml schema for the environment: key hasn’t changed in the docs, so that goes back at least as far as the 2.0 alpha.

I think I see the problem, though.

There’s an environment subsection for build, which as you stated, should be a YAML map, not a YAML list:

stages:
  build:
    environment:
      FOO: bar

There’s also an env section that’s passed to Docker images, which should be a YAML list and not a YAML map:

  - image: postgres:9.4.1
    # some containers require setting environment variables
    env:
      - POSTGRES_USER=root

I agree this is confusing. I think it’s the result of the first section getting passed straight to Docker–you’ll also notice the equals/colon difference. The VAR=VALUE is likely passed into bash/sh in the Docker container.

So just to recap, there was no change in syntax around environment variables. There’s an inconsistent syntax between env values passed to Docker and environment values for CircleCI build steps.

Our engineers are aware of this, but I’ll bring this to their attention again. Additionally, I’ll update the documentation so that this is specifically called out.


#5

Actually, Getting Started: Configuration still shows the build environment section as a list. Please do fix it. Also note that validation/usage of build:environment did change just in the last couple of days, as the list in build:environment was working fine up to that point. We’d love to have notice of syntax changes.


#6

Ah, you’re correct. I only looked at the “Putting it all together” section, and missed that the “Environment” section is different.

You’re correct that a syntax change rolled out recently. It should have only accepted more syntaxes as we deprecate one approach in favor of another. If you have some builds that show otherwise, can you Direct Message me on here with some links? That should definitely be passed onto engineering.


#7

@rfay just to follow up–you were correct that there was a regression in the accepted syntax for a couple of days around your last post. This should be resolved now.


#8

Can we please make the information in this post into the documentation section. At least pointing out in the example that add env variable to containers requires the “env” with YAML list. This would help someone who is new to use the 2.0 API at least a few hours…


#9

We do https://circleci.com/docs/2.0/configuration-reference/#full-example


#10

Maybe I misunderstood something. In the link you posted, the environment variable passing to container doesn’t seem to work.

version: 2
jobs:
  build:
    docker:
      - image: postgres:9.4.1
        # some containers require setting environment variables
        environment:
          POSTGRES_USER: root

However, as this thread pointed out, the syntax has changed to:

version: 2
jobs:
  build:
    docker:
      - image: postgres:9.4.1
        # some containers require setting environment variables
        env:
            - POSTGRES_USER=root

And that worked.


#11

That’s backwards. The latter is the older format and the former is the new format. You should use environment not env.


#12

Understood. Thanks.


#13