How to add a path to PATH in Circle 2.0?

config

#1

I used to do this in Circle v1:

  environment:
    PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"

But when I do the same thing on Circle v2, it doesn’t seem to interpolate the ${PATH} stuff. So when I echo $PATH it actually starts with the string “${PATH}”.


How to change node version in CircleCI 2.0?
#2

You currently cannot evaluate a variable when setting another one, so the fix is to set it to the string that $PATH evals to at that point. You can just docker run -it imagename:version echo $PATH.


#3

I’d love a bit more insight into your answer. I’m not sure how to write my config file based on your answer.

I’m starting with:

jobs:
  build:
    environment:
      PATH: /usr/lib/postgresql/9.6/bin/:$PATH

How do I user your answer in my config? Thanks!


#4

If you want to modify PATH in the 2.0 config you will have to do the following:

    environment:
       BASH_ENV: ~/.bashrc
    steps:
      run: echo 'export PATH=/path/to/foo/bin:$PATH' >> $BASH_ENV 
      run: some_program_inside_bin

We don’t yet support interpolating environment variables in the config, but this workaround should do it.

Please let us know :bow:


#5

Not a good result - perhaps I didn’t incorporate it correctly into my config? I’m not sure I’m doing it right. How would I work your suggestion into my config file? Since my steps has a list, I can’t add these items as a map. If I add the echo statement as a list item, it gives me this error.

 #!/bin/bash -eo pipefail
echo 'export PATH=/usr/lib/postgresql/9.6/bin/:$PATH' >> $BASH_ENV
/bin/bash: ~/.bashrc: No such file or directory
Exited with code 1

The config file:

version: 2
jobs:
  build:
    working_directory: ~/classtag-web
    environment:
      BASH_ENV: ~/.bashrc
    docker:
      - image: circleci/ruby:2.4.1-node
        environment:
          RAILS_ENV: test
      - image: circleci/postgres:9.6-alpine
    steps:
      - run: echo 'export PATH=/usr/lib/postgresql/9.6/bin/:$PATH' >> $BASH_ENV

      - checkout

      # Restore bundle cache
      - type: cache-restore
        key: rails-demo-{{ checksum "Gemfile.lock" }}

      # Bundle install dependencies
      - run: bundle install --path vendor/bundle

      # Store bundle cache
      - type: cache-save
        key: rails-demo-{{ checksum "Gemfile.lock" }}
        paths:
          - vendor/bundle
      # Database setup
      - run: bundle exec rake db:create
      - run: bundle exec rake db:migrate

      # Run rspec in parallel
      - type: shell
        command: |
          bundle exec rspec --profile 10 \
                            --format RspecJunitFormatter \
                            --out /tmp/test-results/rspec.xml \
                            --format progress \
                            $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)

      # Save artifacts
      - type: store_test_results
        path: /tmp/test-results

#6

Hi Jolim,

The pre-existing $BASH_ENV file actually exists as a temporary file. You can remove these line:

    environment:
      BASH_ENV: ~/.bashrc

#7

I’m actually observing the opposite - the temporary file does not exist and has to be created before it can be used:

#!/bin/bash -eo pipefail

echo $BASH_ENV
ls -la $BASH_ENV || true
touch $BASH_ENV || true
ls -la $BASH_ENV || true

/tmp/.bash_env-5952daa5c9e77c00011dfe39-0-build
ls: cannot access /tmp/.bash_env-5952daa5c9e77c00011dfe39-0-build: No such file or directory
-rw-r--r-- 1 circleci circleci 0 Jun 27 22:23 /tmp/.bash_env-5952daa5c9e77c00011dfe39-0-build

Another problem I’m having is that variables are not interpolated, so setting BASH_ENV to anything other then an absolute path won’t work (when using the variable in commands/scripts):

    environment:
      BASH_ENV: ~/.bashrc
#!/bin/bash -eo pipefail

echo $BASH_ENV
ls -la $BASH_ENV || true
touch $BASH_ENV || true
ls -la $BASH_ENV || true

~/.bashrc
ls: cannot access ~/.bashrc: No such file or directory
touch: cannot touch ‘~/.bashrc’: No such file or directory
ls: cannot access ~/.bashrc: No such file or directory

This will work, as there are no variable to interpolate here:

    environment:
      BASH_ENV: /home/circleci/.bashrc

The file still has to be touched before it can be used (touch $BASH_ENV)


#8

@lmakarov did you ever resolve this? I cannot get the PATH to be set right… tried a few different ways.

I did what you suggested - using the path in the BASH_ENV. but after adding the export statement. its still not using the right path. I tried

- run: echo 'export PATH=$HOME/.meteor:$PATH' >> $BASH_ENV
- run: source /home/circleci/.bashrc

#9

@port80labs Using source as its own step is not going to do anything since every step runs in its own somewhat isolated shell. The only way that I could get it to load the $BASH_ENV is by NOT defining it under environment since circle already exports that environment variable, and simply adding to it. E.g. in your case, assuming that you don’t define $BASH_ENV yourself, this one step:

- run: echo 'export PATH=$HOME/.meteor:$PATH' >> $BASH_ENV

should be enough for later steps to pick up that path. At least it does for me :slight_smile:


Sourcing bashrc before run on machine executor
#10

Yo for me this one worked as suggested by @teeberg:

  - run:
      name: Make Composer executables available globally
      command: |
        echo 'export PATH=$HOME/my-repo-name/bin:$PATH' >> $BASH_ENV
        source /home/circleci/.bashrc

#11

I’m struggling to understand this $BASH_ENV thing. I just want to set an env var such as

TAG=‘v1.$CIRCLE_BUILD_NUM’

How could I set this within a case like this?


#12