Setting environment variables with a command fails

envars
circle.yml

#1

When I try to set an Environment variable wth a shell command, it’s not working. For example:

machine:
  services:
    - docker
  environment:
    AWS_DEFAULT_REGION: us-east-1
    VERSION: $(tail -1 version)

$VERSION should output 0.0 (as it stands right now, the last line of the file ‘version’). However… it’s substituting the text of the command and not the value of the command’s output. What am I doing wrong? Or is this a bug?


#2

Neither. Bash commands are not supported in this part of circle.yml. You can try running that command before the first command/script that needs the variable.

I can also move this into a Feature Request for you if you’d like.


#3

This failed, too:

deployment:
  production:
    branch: tlester/jenkins
    environment:
      VERSION: $(tail -1 version)
    commands:
      - eval $(aws ecr get-login)

#4

If you wanted to do something like that, you could do it like this:

deployment:
  production:
    branch: tlester/jenkins
    commands:
      - |
        VERSION: $(tail -1 version)
        eval $(aws ecr get-login)

I didn’t run that myself but something like that should work. For deployments, anything within commands will run with Bash.

The pipe (|) allows us to run a multiline command in YAML. If you were to do it as two separate commands, it wouldn’t work because each command would get it’s own shell meaning VERSION wouldn’t exists in the second command.


#5

That worked!


#6

I’m happy to hear that. :slight_smile:


#7