Get previous commit sha in circleCli

Viewed 362

I am using NX and CircleCI to deploy my project to firebase.

I create a command nx:affected:deploy which should deploy only affected build based on previous commit to develop branch.

my config.yml from CircleCI looks like this

version: 2
jobs:
  test_affected:
    docker:
      - image: circleci/node:12-browsers
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            - v1-dependencies-
      - run: npm install
      - save_cache:
          key: v1-npm-deps-{{ checksum "package-lock.json" }}
          paths:
            - ./node_modules
      - run: npm run affected:dev:stylelint
      - run: npm run affected:dev:lint
      - run: npm run affected:dev:test
      - persist_to_workspace:
          root: .
          paths:
            - .

  deploy_affected:
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - run: npm i
      - run: npm run affected:dev:deploy

workflows:
  version: 2
  build_and_deploy:
    jobs:
      - test_affected
      - deploy_affected:
          requires:
            - test_affected
          filters:
            branches:
              only: develop

now, the problem is, when deploy_affected run, the commit is already pushed to develop and nothing trigger (since develop and affected look for the same commit), so I need to set the target to the previous commit SHA on develop.

I know in github actions we can do :

env:
  BEFORE_SHA: ${{ github.event.before }}

but I can't find a simple way on CircleCli config.

EDIT :

I tried to use :

    environment:
      LASTEST_SHA1: echo -e $(git rev-list -n 1 origin/develop^1)
      LASTEST_SHA2: << pipeline.git.base_revision >>
      LASTEST_SHA3: $(git rev-list -n 1 origin/develop^1)
    steps:
      - run:
          command: |
            echo $LASTEST_SHA1
            echo $LASTEST_SHA2
            echo $LASTEST_SHA3

but the result are the same value I set...

echo -e $(git rev-list -n 1 origin/develop^1)
<< pipeline.git.base_revision >>
1 Answers

The environment blocks doesn't allow referring to other variables, but you can use them in commands. So try e.g.

echo "<< pipeline.git.base_revision >>"

If you have a command that needs this value as an environment variables (instead of a command line parameter), you could do e.g.

MY_ENV="<< pipeline.git.base_revision>>" ./tools/mycommand

or use BASH_ENV approach from CircleCI documentation: https://circleci.com/docs/2.0/env-vars/#example-configuration-of-environment-variables

Related