diff --git a/.github/workflows/DeployAndRunUnitTests.yaml b/.github/workflows/DeployAndRunUnitTests.yaml new file mode 100644 index 00000000000..632f6abb134 --- /dev/null +++ b/.github/workflows/DeployAndRunUnitTests.yaml @@ -0,0 +1,84 @@ +name: Deploy and Run all Unit Tests +on: + push: + branches: + - '**/*' + - main + pull_request: + types: [ opened ] + branches: + - '**/*' + - main +jobs: + create-org-and-deploy: + runs-on: ubuntu-latest + env: + DEVHUB_ORG_ALIAS: OrtooISV + ORG_ALIAS_PREFIX: 'FFLIB' + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + - name: Salesforce SFDX CLI Action + uses: sfdx-actions/setup-sfdx@v1 + + # Update ./config/project-scratch-def.json + + - name: Update project-scratch-def.json orgName + uses: jossef/action-set-json-field@6e6d7e639f24b3955ef682815317b5613ac6ca12 + with: + file: ./config/project-scratch-def.json + field: orgName + value: "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" + + - name: Update project-scratch-def.json description + uses: jossef/action-set-json-field@6e6d7e639f24b3955ef682815317b5613ac6ca12 + with: + file: ./config/project-scratch-def.json + field: description + value: "${{ github.repository }} , Org Alias ${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}. Created by ${{ github.actor }}" + + - name: Show project-scratch-def.json + run: 'cat ./config/project-scratch-def.json' + + # sfdx-project.json file should be ok as is + + - name: Show sfdx-project.json + run: 'cat ./sfdx-project.json' + + # Dev Hub needed to create scratch org + + - name: Create DevHub Auth File + run: echo "${{ secrets.AUTH_SECRET_ISV }}" > devhub_auth_file.txt + + - name: Authorise Dev Hub Org + run: sfdx auth:sfdxurl:store -f devhub_auth_file.txt -d -a ${{ env.DEVHUB_ORG_ALIAS }} + + # Create Scratch Org + + - name: Run script CreateScratchOrgWithNamespaceOnlyHeadless.sh + run: ./scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" + shell: sh + + # Push code to org + + - name: Deploy FFLIB to Org + run: sfdx force:source:deploy -p sfdx-source --targetusername "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" + + # Run Apex Unit Tests + + - name: Run All Unit Tests + run: ./scripts/std_batch/RunApexUnitTests.sh "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" + shell: bash + + # Get Apex Code Coverage Numbers + + - name: Get Apex Code Coverage + run: ./scripts/std_batch/GetApexCodeCoverage.sh "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" + shell: sh + + # Delete Scratch Org + + - name: Delete Scratch Org + if: ${{ always() }} + run: sfdx force:org:delete --noprompt --targetusername "${{env.ORG_ALIAS_PREFIX}}${{github.run_number}}" \ No newline at end of file diff --git a/.github/workflows/deploy.and.test.yml b/.github/workflows/deploy.and.test.yml deleted file mode 100644 index 394bf826646..00000000000 --- a/.github/workflows/deploy.and.test.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Create a Scratch Org, Push Source and Run Apex Tests - -on: - push: - pull_request_target: - workflow_dispatch: - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - with: - ref: ${{github.event.pull_request.head.ref}} - repository: ${{github.event.pull_request.head.repo.full_name}} - - name: Install SFDX CLI and authorize DevHub - uses: apex-enterprise-patterns/setup-sfdx@v1 #We're using a fork of https://github.com/sfdx-actions/setup-sfdx for safety - with: - sfdx-auth-url: ${{ secrets.DEVHUB_SFDXURL }} - - run: sfdx force:config:set defaultdevhubusername=SFDX-ENV -g #Even though the setup-sfdx action uses --setdefaultdevhubusername, it doesn't seem to stick since it uses --setdefaultusername so we brute force it here - - run: echo y | sfdx plugins:install shane-sfdx-plugins - - run: sfdx force:org:create -f config/project-scratch-def.json --setdefaultusername -d 1 - - run: sfdx shane:github:src:install -c -g apex-enterprise-patterns -r fflib-apex-mocks -p sfdx-source/apex-mocks - - run: sfdx force:source:push - - run: sfdx force:apex:test:run -w 5 - - name: Destroy scratch org - run: sfdx force:org:delete -p - if: always() - diff --git a/scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh b/scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh new file mode 100755 index 00000000000..d5d16465e01 --- /dev/null +++ b/scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +# chmod u+x CreateScratchOrgWithNamespaceOnlyHeadless.sh +# run in Terminal window of SFDX project with ./scripts/std_batch/CreateScratchOrgWithNamespaceOnlyHeadless.sh OrtooQassignScratch + +# CREATE SCRATCH ORG WITH NAMESPACE AND GENERATE PASSWORD +# +echo "Scratch Org Creation With Namespace ONLY (no data loaded) started." + +ORG_ALIAS="OrtooQassignScratch" + +if [ $# = 1 ]; then + ORG_ALIAS=$1 +else + echo + echo "Script requires one parameter" + echo "1. Org Alias for created org. e.g. OrtooQassignScratch" + echo + echo "**** Remember to update config/project-scratch-def.json file as appropriate." + echo " Include your public facing IP Address to prevent two-factor authentication." + echo " Update Business Hours if necessary." + echo + echo "**** Remember to update sfdx-project.json file with Namespace to be used." + echo + exit 1 +fi + +echo +echo "Using $ORG_ALIAS for Org alias" +echo + +echo "1. create" +sfdx force:org:create -f config/project-scratch-def.json --setalias "$ORG_ALIAS" --durationdays 3 --setdefaultusername --loglevel fatal +if [ $? = 0 ]; then + echo "Created Scratch Org with alias $ORG_ALIAS" +else + echo + echo "Non zero exit code: $?" + echo "Exiting script" + exit 1 +fi + +echo "2. password generation" +sfdx force:user:password:generate -u "$ORG_ALIAS" +if [ $? = 0 ]; then + echo "Password generated" +else + echo + echo "Non zero exit code: $?" + echo "Exiting script" + echo + exit 1 +fi + +echo "3. display Org details" +sfdx force:org:display -u "$ORG_ALIAS" +if [ $? = 0 ]; then + echo "Org details displayed" +else + echo + echo "Non zero exit code: $?" + echo "Exiting script" + echo + exit 1 +fi + +echo +echo "Scratch Org Creation With Namespace ONLY completed." +echo + +exit 0 \ No newline at end of file diff --git a/scripts/std_batch/GetApexCodeCoverage.sh b/scripts/std_batch/GetApexCodeCoverage.sh new file mode 100755 index 00000000000..f74c25d05c7 --- /dev/null +++ b/scripts/std_batch/GetApexCodeCoverage.sh @@ -0,0 +1,18 @@ +ORG_ALIAS="OrtooQassignScratch" + +if [ $# = 1 ]; then + ORG_ALIAS=$1 +else + echo + echo "Script requires one parameter" + echo "1. Org Alias for created org. e.g. OrtooQassignScratch" + echo + exit 1 +fi + +sfdx force:data:soql:query -u "$ORG_ALIAS" -t -q "SELECT SUM(NumLinesUncovered), SUM(NumLinesCovered) FROM ApexCodeCoverageAggregate" | grep -o -E '[0-9]+' | xargs | while read -r uncovered covered remainder ; do + totallines=$(expr $covered + $uncovered) + coveredtimeshundred=$(expr $covered \* 100) + percentage=$(expr $coveredtimeshundred / $totallines) + echo "Uncovered: $uncovered, Covered: $covered, Percentage: $percentage%" +done \ No newline at end of file diff --git a/scripts/std_batch/RunApexUnitTests.sh b/scripts/std_batch/RunApexUnitTests.sh new file mode 100755 index 00000000000..9791aa3803d --- /dev/null +++ b/scripts/std_batch/RunApexUnitTests.sh @@ -0,0 +1,14 @@ +#!/bin/bash +ORG_ALIAS="OrtooQassignScratch" + +if [ $# = 1 ]; then + ORG_ALIAS=$1 +else + echo + echo "Script requires one parameter" + echo "1. Org Alias for created org. e.g. OrtooQassignScratch" + echo + exit 1 +fi + +( set -o pipefail; SFDX_IMPROVED_CODE_COVERAGE='true' SFDX_MAX_QUERY_LIMIT=200000 sfdx force:apex:test:run -u "$ORG_ALIAS" -r human --codecoverage --wait 30 | grep -v ' Pass ' | grep -v '^[a-zA-Z0-9]\{18\}\s\s' | grep -v 'UNCOVERED' | grep -v 'Apex Code Coverage' | grep -v ' ─────── ───────────────' | grep -v '─────────────────────────────────────────────────────────────────────────────────────────────────────────────' ) \ No newline at end of file