|
| 1 | +# This workflow is an example used during the Agentforce World Tour Paris 2025 |
| 2 | +# Agentforce CLI : Créer un agent depuis votre terminal |
| 3 | +# By @nabondance |
| 4 | + |
| 5 | +name: Salesforce Agent Test |
| 6 | +# This workflow runs Salesforce Agent tests and aggregates the results. |
| 7 | +# It uses a single job to run all tests sequentially, which is simpler but may take longer than a matrix strategy. |
| 8 | +# It is split into several steps for better readability and understanding. |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: [ main ] |
| 13 | + # Uncomment the following lines to enable pull request triggers |
| 14 | + # pull_request: |
| 15 | + # types: [opened, synchronize, ready_for_review, reopened] |
| 16 | + # branches: [ main ] |
| 17 | + workflow_dispatch: |
| 18 | + |
| 19 | +jobs: |
| 20 | + salesforce-agent: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + container: salesforce/cli:latest-slim |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout Code |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: 'Install agent plugin' |
| 29 | + run: sf plugins install @salesforce/plugin-agent |
| 30 | + |
| 31 | + - name: 'Authenticate sf org' |
| 32 | + run: | |
| 33 | + echo "${{ secrets.ORG_SFDX_AUTH_URL }}" > ./authfile |
| 34 | + sf org login sfdx-url --sfdxurlfile=authfile -a sf_org |
| 35 | +
|
| 36 | + - name: 'List Agent tests' |
| 37 | + id: list-tests |
| 38 | + run: | |
| 39 | + echo "TESTS_LIST=$(sf agent test list --target-org=sf_org --json | jq -c '[.result[].fullName]')" >> $GITHUB_OUTPUT |
| 40 | +
|
| 41 | + - name: 'Abort if no tests found' |
| 42 | + if: ${{ steps.list-tests.outputs.TESTS_LIST == '[]' }} |
| 43 | + run: | |
| 44 | + echo "No tests to run in the org." |
| 45 | + exit 0 |
| 46 | +
|
| 47 | + - name: 'Run tests and collect runIds' |
| 48 | + id: run-tests |
| 49 | + shell: bash |
| 50 | + run: | |
| 51 | + TESTS_JSON='${{ steps.list-tests.outputs.TESTS_LIST }}' |
| 52 | + run_ids="" |
| 53 | + while read -r test; do |
| 54 | + if [ ! -z "$test" ]; then |
| 55 | + run_id=$(sf agent test run --target-org=sf_org --api-name="$test" --json | jq -r '.result.runId') |
| 56 | + if [ -z "$run_ids" ]; then |
| 57 | + run_ids="[\"$run_id\"" |
| 58 | + else |
| 59 | + run_ids="$run_ids,\"$run_id\"" |
| 60 | + fi |
| 61 | + fi |
| 62 | + done < <(echo "$TESTS_JSON" | jq -r '.[]') |
| 63 | + run_ids="$run_ids]" |
| 64 | + echo "RUN_IDS=$run_ids" >> $GITHUB_OUTPUT |
| 65 | +
|
| 66 | + - name: 'Display runIds' |
| 67 | + run: | |
| 68 | + echo "${{ steps.run-tests.outputs.RUN_IDS }}" |
| 69 | +
|
| 70 | + - name: 'Wait for tests to finish' |
| 71 | + id: wait-for-tests |
| 72 | + shell: bash |
| 73 | + run: | |
| 74 | + run_ids='${{ steps.run-tests.outputs.RUN_IDS }}' |
| 75 | + test_results=() |
| 76 | + total_passed=0 |
| 77 | + total_failed=0 |
| 78 | + total_tests=0 |
| 79 | + mkdir -p ./test-results |
| 80 | +
|
| 81 | + for run_id in $(echo "$run_ids" | jq -r '.[]'); do |
| 82 | + while true; do |
| 83 | + result=$(sf agent test results --target-org=sf_org --job-id="$run_id" --json) |
| 84 | + status=$(echo "$result" | jq -r '.result.status') |
| 85 | + if [ "$status" == "COMPLETED" ]; then |
| 86 | + # Count test results for this run |
| 87 | + passed=$(echo "$result" | jq -r '[.result.testCases[] | .testResults[] | select(.result == "PASS")] | length') |
| 88 | + failed=$(echo "$result" | jq -r '[.result.testCases[] | .testResults[] | select(.result == "FAILURE")] | length') |
| 89 | +
|
| 90 | + # Add to totals |
| 91 | + total_passed=$((total_passed + passed)) |
| 92 | + total_failed=$((total_failed + failed)) |
| 93 | +
|
| 94 | + # Add result to array |
| 95 | + test_results+=("$(echo "$result" | jq -c '.')") |
| 96 | + echo "$result" > "./test-results/${run_id}.json" |
| 97 | +
|
| 98 | + break |
| 99 | + fi |
| 100 | + sleep 10 |
| 101 | + done |
| 102 | + done |
| 103 | +
|
| 104 | + # Calculate total tests |
| 105 | + total_tests=$((total_passed + total_failed)) |
| 106 | +
|
| 107 | + # Combine all results into a JSON array |
| 108 | + json_array=$(printf '%s,' "${test_results[@]}" | sed 's/,$//') |
| 109 | + echo "TEST_RESULTS=[${json_array}]" >> "$GITHUB_OUTPUT" |
| 110 | +
|
| 111 | + # Also save the statistics as outputs |
| 112 | + echo "TOTAL_TESTS=$total_tests" >> "$GITHUB_OUTPUT" |
| 113 | + echo "TOTAL_PASSED=$total_passed" >> "$GITHUB_OUTPUT" |
| 114 | + echo "TOTAL_FAILED=$total_failed" >> "$GITHUB_OUTPUT" |
| 115 | + echo "PERCENTAGE_PASSED=$((total_passed * 100 / total_tests))" >> "$GITHUB_OUTPUT" |
| 116 | +
|
| 117 | + - name: 'Display test results' |
| 118 | + run: | |
| 119 | + echo "============================================" |
| 120 | + echo " Test Results Summary " |
| 121 | + echo "============================================" |
| 122 | + echo "Total Tests Run: ${{ steps.wait-for-tests.outputs.TOTAL_TESTS }}" |
| 123 | + echo "Tests Passed: ${{ steps.wait-for-tests.outputs.TOTAL_PASSED }}" |
| 124 | + echo "Tests Failed: ${{ steps.wait-for-tests.outputs.TOTAL_FAILED }}" |
| 125 | + echo "Percentage Passed: ${{ steps.wait-for-tests.outputs.PERCENTAGE_PASSED }}%" |
| 126 | + echo "============================================" |
| 127 | +
|
| 128 | + - name: Upload test results |
| 129 | + uses: actions/upload-artifact@v4 |
| 130 | + with: |
| 131 | + name: agent-test-results |
| 132 | + path: ./test-results/ |
| 133 | + |
| 134 | + - name: 'Check for passing threshold' |
| 135 | + if: ${{ steps.wait-for-tests.outputs.PERCENTAGE_PASSED < '75' }} |
| 136 | + run: | |
| 137 | + echo "Test run failed. Percentage passed: ${{ steps.wait-for-tests.outputs.PERCENTAGE_PASSED }}%" |
| 138 | + echo "Failing the job as the percentage passed is below the threshold." |
| 139 | + exit 1 |
0 commit comments