CircleCI and Salesforce

Main circle.ci file

version: 2
jobs:
  buildonly:
    docker:
      - image: circleci/node:latest
    steps:
      - checkout
      - run:
          name: Install Dependencies
          command: . build/install.sh
      - run:
          name: Create Scratch Org
          command: . build/create-scratch-org.sh
      - run:
          name: Delete Scratch Org
          command: . build/delete-scratch-org.sh
  build:
    docker:
      - image: circleci/node:latest
    steps:
      - checkout
      - run:
          name: Install Dependencies
          command: . build/install.sh
      - run:
          name: Create Scratch Org
          command: . build/create-scratch-org.sh
      - run:
          name: Validate Components & Run Tests
          command: . build/test.sh
      - store_test_results:
          path: test-results
      - run:
          name: Delete Scratch Org
          command: . build/delete-scratch-org.sh
  deploy-prod:
    docker:
    - image: circleci/node:latest
    steps:
      - checkout
      - run:
          name: Install Dependencies
          command: . build/install.sh
      - run:
          name: Login to Production
          command: . build/setup-prod.sh
      - run:
          name: Deploy to Production
          command: . build/deploy-prod.sh
workflows:
  version: 2
  validate:
    jobs:
      - build:
          filters:
            branches:
              ignore:
                - master
      - deploy-prod:
          filters:
            branches:
              only:
                - master
#Install dependencies, SFDX CLI in this case
 echo "Installing Dependencies... "
 sudo npm install -global sfdx-cli
 echo "Installing plugins... "
 echo y |sfdx plugins:install https://github.com/Accenture/sfpowerkit -f
 echo "Installing jq to read json... "
 sudo apt-get install jq
#!/bin/sh

# Get the private key from the environment variable
echo "Setting up DevHub Connection..."
mkdir keys
echo $SFDC_SERVER_KEY | base64 -d > keys/server.key

# Authenticate to salesforce
echo "Authenticating..."
RES=$(sfdx force:auth:jwt:grant --clientid $SFDC_PROD_CLIENTID --jwtkeyfile keys/server.key --username $SFDC_PROD_USER --setdefaultdevhubusername -a DevHub --json)
SFDC_AUTHENTICATE_ID=$(echo ${RES} | jq --raw-output .result.orgId) 
echo "OrgId..."
echo ${SFDC_AUTHENTICATE_ID}

#Create a scratch org
echo "Name of Scratch Org"
echo ${CIRCLE_BRANCH}

echo "Creating the Scratch Org..."
RES=$(sfdx force:org:create -f config/project-scratch-def.json -a ${CIRCLE_BRANCH} -s -d 1 --json) 

#Create org wide email address
echo "Create org email address... "
SFDC_CREATE_EMAIL=$(sfdx  sfpowerkit:org:orgwideemail:create -e test@example.com -n TestEmail -p -u ${CIRCLE_BRANCH} --json)
SFDC_EMAIL_ID=$(echo $SFDC_CREATE_EMAIL| jq --raw-output .result.id) 

echo "reading email id created"
echo ${SFDC_EMAIL_ID}

echo "Valiate org email address... "
RES=$(sfdx sfpowerkit:org:orgwideemail:verify -i ${SFDC_EMAIL_ID} -u ${CIRCLE_BRANCH})
echo ${RES}

The above also sets up a org wide email address so that any email alerts setup will deploy.

Use the following website to load some JSON and then select the right jq syntax e.g. jq –raw-output .result.id

https://jqplay.org/

Leave a Reply

Your email address will not be published.