Conditional step based on environment variable

Hi,
company I work for decided to give a try to CircleCI. What I have tried for past 2 days either seems impossible or I am missing something obvious. I downloaded client 0.1.15044+ec2b22a (release) and tried to locally checkout repository using following config.yml:

version: 2.1

jobs:
  build-mylib:
    docker:
      - image: cimg/python:3.7.10
    steps:
      - checkout

workflows:
  main:
    jobs:
      - build-mylib

And run following commands:

circleci config validate
circleci config process .circleci/config.yml > process.yml
circleci local execute --job build-mylib

But got following output:

====>> Preparing environment variables
Using build environment variables:
  BASH_ENV=/tmp/.bash_env-localbuild-1614702436
  CI=true
  CIRCLECI=true
  CIRCLE_BRANCH=
  CIRCLE_BUILD_NUM=
  CIRCLE_JOB=build-mylib
  CIRCLE_NODE_INDEX=0
  CIRCLE_NODE_TOTAL=1
  CIRCLE_REPOSITORY_URL=
  CIRCLE_SHA1=
  CIRCLE_SHELL_ENV=/tmp/.bash_env-localbuild-1614702436
  CIRCLE_WORKING_DIRECTORY=~/project

Using environment variables from project settings and/or contexts:
  REPO_URL=**REDACTED**

The redacted variables listed above will be masked in run step output.====>> Checkout code
Making checkout directory "/home/circleci/project"
Copying files from "/tmp/_circleci_local_build_repo" to "/home/circleci/project"
Error: cp: cannot open '/tmp/_circleci_local_build_repo/.git/smartgit/logcache/nodes2' for reading: Permission denied
cp: cannot open '/tmp/_circleci_local_build_repo/.git/smartgit/logcache/subtree-origins' for reading: Permission denied

Error: 
failed to copy files: exit status 1

Step failed
Error: runner failed (exited with 101)
Task failed
Error: task failed

Tried many things, but did not figure out. Is it possible to have step that depends on passed environment variable? So when running build locally, I can skip checkout and make git clone as part of the run command.

I have found couple of similar issues and starting to wonder is the aim of the CircleCI not to be able to run builds locally on purpose? I can create my own command, pass the private key, add it to the ssh-agent and do clone, but this somehow defeats the purpose of the product and also would failed on server.

Thank you in advance.

Kindest regards

Hi @nikoladsp. I’ve tried to reproduce but have been able to run your example config.yml with no issues. On my machine:

mkdir testing
cd testing
git init
mkdir .circleci
pbpaste > .circleci/config.yml   # I copied the contents of your config.yml before running this
circleci config validate
circleci config process .circleci/config.yml > process.yml  # this command doesn't do anything useful for you
circleci local execute --job build-mylib

The output of the last command indicates success checking out the codebase:

✓ circleci local execute --job build-mylib
Docker image digest: sha256:8c0ed662ac11644f59ffec588750c4f8ab92d006805e1587151217d2145597ad
====>> Spin Up Environment
Build-agent version  ()
Docker Engine Version: 19.03.12
Kernel Version: Linux a1955447d2ae 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 Linux
Starting container cimg/python:3.7.10
  image is cached as cimg/python:3.7.10, but refreshing...
3.7.10: Pulling from cimg/python
Digest: sha256:b0e51c41efcfb8706febd60fbf690913970c448a2e9e0cff9f07016495eba113
Status: Image is up to date for cimg/python:3.7.10
  pull stats: N/A
  time to create container: 50ms
  using image cimg/python@sha256:b0e51c41efcfb8706febd60fbf690913970c448a2e9e0cff9f07016495eba113
Time to upload agent and config: 583.1513ms
Time to start containers: 281.1075ms
====>> Preparing Environment Variables
Using build environment variables:
  BASH_ENV=/tmp/.bash_env-localbuild-1614899208
  CI=true
  CIRCLECI=true
  CIRCLE_BRANCH=
  CIRCLE_BUILD_NUM=
  CIRCLE_JOB=build-mylib
  CIRCLE_NODE_INDEX=0
  CIRCLE_NODE_TOTAL=1
  CIRCLE_REPOSITORY_URL=
  CIRCLE_SHA1=
  CIRCLE_SHELL_ENV=/tmp/.bash_env-localbuild-1614899208
  CIRCLE_WORKING_DIRECTORY=~/project


The redacted variables listed above will be masked in run step output.====>> Checkout code
  #!/bin/bash -eo pipefail
mkdir -p /home/circleci/project && cd /tmp/_circleci_local_build_repo && git ls-files | tar -T - -c | tar -x -C /home/circleci/project && cp -a /tmp/_circleci_local_build_repo/.git /home/circleci/project
Success!

My recommendation is to run the job with debug logging on (circleci local execute --job build-mylib --debug) to see if that shows you anything useful. You can also add a step to your job to show all the shell commands that get run (make this the first step in the job):

      - run:
          command: echo "set -x" | tee -a "$BASH_ENV"

If this isn’t enough to help you figure things out, we will need to see a more complete example to be able to help you.


More broadly: Circle can run some jobs locally, but not all. Things like caches don’t work locally, and a job that requires information from other jobs will similarly fail.

Also, you can do whatever you like with environment variables in run steps just like you would in a shell, but you can’t use job conditionals with them. Your problem seems unrelated to that though (at least with the information given).