How to use environment variables in the checkout step?

2.0

#1

Hi I’m developing with Go, Dep.
When go commands something like go test it’s necessary to checkout the git revision in the $GOPATH because Dep requires.

So I tried below.

version: 2
jobs:
  test:
    docker:
    - image: golang

    environment:
      CUSTOM_WORK_DIR: /go/src/github.com/foo/bar

    steps:
      - run:
          name: make go source directory
          command: |
            mkdir -p ${CUSTOM_WORK_DIR}

      - checkout:
          path: ${CUSTOM_WORK_DIR}

      - run:
          name: go get dep
          command: |
            go get -u github.com/golang/dep/cmd/dep

      - run:
          name: setting go environments
          command: |
            cd ${CUSTOM_WORK_DIR}
            dep ensure

But error occurred at the build in CircleCI.

setting go environments00:00
Exit code: 1
#!/bin/bash -eo pipefail
cd ${CUSTOM_WORK_DIR}
dep ensure
could not find project Gopkg.toml, use dep init to initiate a manifest
Exited with code 1

It is determined that the environment variable is empty and checked out for an empty directory.

How can i use environment variables in checkout?


#2

As I understand it, some parts of the config can use env vars, some can’t - I suspect the code behind each one was written independently.

You can use a shell script instead of the checkout command though - copy the code echoed in one of your old runs for checkout, and then put that in a run step. You can use env vars there.