Cannot Access Circle's Own Environmental Variables


#1

The $CIRCLE_SHA is nil in the config snippet below, what am I doing wrong?

  deploy-docker-image:
    docker:
      - image: docker:17.05.0-ce-git
    working_directory: ~/stellasourcer
    steps:
      - checkout
      - setup_remote_docker
      - deploy:
          name: Build docker image
          command: |
            docker build -t stellasourcer .
            docker tag stellasourcer ${PRIVATE_REGISTRY}/stellasourcer:${CIRCLE_SHA1}
      - deploy:
          name: Push docker image
          command: |
            docker login -u $NEXUS_USERNAME -p $NEXUS_PASSWORD $PRIVATE_REGISTRY
            docker push ${PRIVATE_REGISTRY}/stellasourcer:${CIRCLE_SHA1}

Thanks


#2

I was not able to reproduce this.

Check out this build: https://circleci.com/gh/levlaz/sandbox/59

I tried all combinations:

          echo $CIRCLE_SHA1
          echo ${CIRCLE_SHA1}
          echo my_registry/stellasourcer:$CIRCLE_SHA1
          echo my_registry/stellasourcer:${CIRCLE_SHA1}

Are you by chance trying to test this locally?


#3

Hi,
Thanks for the reply.
No I am not trying to test it locally. But I got it working another way, I think it is my poor understanding of circle’s config.
here’s the complete configuration. I used to have $CIRCLE_SHA1 where I now have cat .circle-sha :

# Clojure CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-clojure/ for more details
#
version: 2
jobs:
  #----------------------------------------------------------------------
  build:
    docker:
      - image: circleci/clojure:lein-2.8.1

    working_directory: ~/stellasourcer
    environment:
      LEIN_ROOT: "true"
      JVM_OPTS: -Xmx3200m

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-stellasourcer-dependencies-{{ checksum "project.clj" }}
          # fallback to using the latest cache if no exact match is found
          - v1-stellasourcer-dependencies-

      - run: lein deps

      - save_cache:
          paths:
            - ~/.m2
          key: v1-stellasourcer-dependencies-{{ checksum "project.clj" }}
  #----------------------------------------------------------------------
  compile:
    docker:
      - image: circleci/clojure:lein-2.8.1
    environment:
      LEIN_ROOT: "true"
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx3200m
    working_directory: ~/stellasourcer

    steps:
      - checkout
      - run: lein uberjar
  #----------------------------------------------------------------------
  externalized-test:
    docker:
      - image: circleci/clojure:lein-2.8.1
    environment:
      LEIN_ROOT: "true"
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx3200m
    working_directory: ~/stellasourcer

    steps:
      - checkout
      - run:
          name: Install Docker Compose
          command: |
            set -x
            curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o ~/docker-compose
            chmod +x ~/docker-compose
            sudo mv ~/docker-compose /usr/local/bin/docker-compose

      - setup_remote_docker

      - run:
          name: Running Externally Dependent Sources
          command: |
            set -x
            ./compose midje
  #----------------------------------------------------------------------
  deploy-artifact:
    docker:
      - image: circleci/clojure:lein-2.8.1
    environment:
      LEIN_ROOT: "true"
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx3200m
    working_directory: ~/stellasourcer

    steps:
      - checkout
      - deploy:
          name: Build and push artifact to nexus repo
          command: |
            lein deploy snapshots
  #----------------------------------------------------------------------
  deploy-docker-image:
    docker:
      - image: docker:17.05.0-ce-git
    working_directory: ~/stellasourcer
    steps:
      - checkout
      - setup_remote_docker
      - run: echo $CIRCLE_SHA1 > .circle-sha && cat .circle-sha
      - deploy:
          name: Build docker image
          command: |
            export TAG=`date +%s`
            docker build -t stellasourcer .
            docker tag stellasourcer ${PRIVATE_REGISTRY}/stellasourcer:$(cat .circle-sha)
            docker tag stellasourcer ${PRIVATE_REGISTRY}/stellasourcer:latest
      - deploy:
          name: Push docker image
          command: |
            docker login -u $NEXUS_USERNAME -p $NEXUS_PASSWORD $PRIVATE_REGISTRY
            docker push ${PRIVATE_REGISTRY}/stellasourcer:$(cat .circle-sha)
            docker push ${PRIVATE_REGISTRY}/stellasourcer:latest
#------------------------------------------------------------------------
workflows:
  version: 2
  build_test_deploy:
    jobs:
      - build
      - compile:
          requires:
            - build
      - externalized-test:
          requires:
            - compile
              #      - deploy-artifact:
              #          context: org-global
              #          requires:
              #            - externalized-test
              #      - deploy-docker-image:
              #          context: org-global
              #          requires:
              #            - deploy-artifact
            #            - deploy-artifact
#------------------------------------------------------------------------

I still appreciate if you could tell why the previous config didn’t work
Thanks


#4