Unclear how to work with user variables + CircleCI-provided env variables


#1

The docs talk about modifying $PATH:

https://circleci.com/docs/2.0/migrating-from-1-2/#modifying-path

Now I am wondering how to use environment variables provided by circle CI and my own variables. Here’s my broken code where I am trying to echo the 2 variables.

jobs:
  build:
    environment:
      BASH_ENV: ~/.bashrc
    working_directory: ~/my-project
    docker:
      - image: circleci/node:4.8.2
    steps:
      - checkout
      - run: echo 'export PATH=/opt/local/bin:$PATH' >> $CIRCLE_BRANCH
      - run: echo 'export PATH=/opt/local/bin:$PATH' >> $MY_CUSTOM_VAR
      - run:
          name: check-environment
          command: |
              echo $CIRCLE_BRANCH
              echo $MY_CUSTOM_VAR

How do I fix this?


#2

Sorry, I fixed it. Was confused due to my poor bash skills…

  - run: BUCKET_NAME=myvar
  - run:
      name: check-environment
      command: |
          echo $CIRCLE_BRANCH
          echo $BUCKET_NAME

#3

That being saids the example could use some love.

I think steps should be contained within build, indentation is wrong here?

version: 2
jobs:
  build:
    working_directory: /tmp
    docker:
      - image: buildpack-deps:jessie
    environment:
       BASH_ENV: ~/.bashrc
  steps:
    run: echo 'export PATH=/path/to/foo/bin:$PATH' >> $BASH_ENV
    run: some_program_inside_of /path/to/foo/bin

If you add the lines about environment/BASH_ENV to your file you get immediate errors.

I would appreciate an example about variables.


#4

Thank you! You’re right- that indentation is off. I escalated that to our docs team to correct ASAP.

Regarding env vars, I just pushed this build to exemplify using that feature:
https://circleci.com/gh/ryanwohara/test-2.0/298

The idea is to append your line to $BASH_ENV - the file that is automatically sourced on each step. When it’s sourced, your env vars will be accessible.


#5

so it doesn’t really matter where you are sending it to?

thisisntreal:/usr/local/bin could be anything?


#6

Ideally it points to the resources you need, but yes, it can technically be anything. The $PATH is what makes your system utilities available, so breaking $PATH will break your build, and that’s why we’re prepending in this example. But I’d have something executable in /thisisntreal if I were to use that for a project.


#7

So my understanding is that the recommended way of using your own variables now is within a separate bash script, commit that with the repo and then execute it within the build. I will try that :slight_smile:


#8

The solution I arrvied at is very simple actually:

  - run: echo 'export PROJECT_NAME=myprojectname' >> $BASH_ENV
  - run: echo 'export PROJECT_DOMAIN=project.domain' >> $BASH_ENV

What threw me off was the environment in the examples, which generates an error.

And using $PATH as an example is unnecessarily complicated, maybe just use a simple variable?

(might just be me and my skills!)


#9

You can define those in the top of your config, though.


#10

is it possible to provide an example? it would be greatly appreciated.


#11
version: 2
jobs:
  build:
    docker:
      - image: circleci/ruby:2.3.3-node-browsers
    environment:
      PROJECT_NAME: "myprojectname"
      PROJECT_DOMAIN: "project.domain"
    steps:
      - run: echo $PROJECT_NAME
      - run: echo $PROJECT_DOMAIN

How to set ENV VARS for local builds using circleci 2.0
#12

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.