Allow Setting 'environment' later than 'machine'

envars
git
circle.yml

#1

I am trying to detect some environment variables that are derived from data in my repository:

machine:
  environment:
    METEOR_VERSION: $(cut -d@ -f2 app/.meteor/release)
    REVISION: $(git describe --tags)
test:
  override:
    - echo $REVISION $METEOR_VERSION

the command substitution works but the environment is not ready as the variables are one of the first thing to be set up in the container. (I am aware of the git-describe caveats with shallow repositories and added git fetch --tags in checkout.post, but the environment is set up even before the git repo is fetched).

To solve this I suggest to enable the same functionality that runs machine.environment to apply to checkout.environment, dependencies.environment etc:

machine:
  environment:
    ONE: two

checkout:
  post:
    - git fetch --tags

test:
  environment:
    METEOR_VERSION: $(cut -d@ -f2 app/.meteor/release)
    REVISION: $(git describe --tags)
  override:
    - echo $REVISION $METEOR_VERSION

The environment keys would be set just for that block of commands (pre, override, post) and run before pre (but it wouldn’t be an issue if they exist for longer than that, as long as that is documented behaviour).

It is very unlikely that there are cases where the environment variable depends on a command that needs to be executed in the very same block as the command as the first one using the variable, and it can still be solved by inconveniently substituting the variable definition at the few references of the variable that cannot be fulfilled.


#2

btw, if anyone is having the same problem, here is my current workaround that is still more convenient than copying code-golfed snippets of weird syntax all around your commands:

machine:
  environment:
    METEOR_VER: /home/ubuntu/.meteor_version.ci
    REVISION: /home/ubuntu/.revision.ci

checkout:
  post:
    - git fetch --tags
    - cut -d@ -f2 app/.meteor/release > $METEOR_VER
    - git describe --tags > $REVISION

test:
  override:
     - tar -zcvf $CIRCLE_ARTIFACTS/rise_`cat $REVISION`.tar.gz bundle

it’s still a bit weird but at least it’s readable.


#3