Skip to content

Commit 179b889

Browse files
authored
Merge pull request #3 from Think2Corp/CI-test
Ci test
2 parents 4508d39 + f03c098 commit 179b889

14 files changed

+4581
-106
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 (Optimized)
6+
# This workflow runs Salesforce Agent tests in parallel and aggregates the results.
7+
# It uses a matrix strategy to run each test in its own job, allowing for faster execution.
8+
9+
on:
10+
push:
11+
branches: [main]
12+
pull_request:
13+
branches: [main]
14+
types: [opened, synchronize, ready_for_review, reopened]
15+
workflow_dispatch:
16+
17+
jobs:
18+
list-tests:
19+
name: List Agent Tests
20+
runs-on: ubuntu-latest
21+
container: salesforce/cli:latest-slim
22+
outputs:
23+
matrix: ${{ steps.set-matrix.outputs.matrix }}
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
28+
- name: Authenticate Salesforce Org
29+
run: |
30+
echo "${{ secrets.ORG_SFDX_AUTH_URL }}" > ./authfile
31+
sf org login sfdx-url --sfdxurlfile=authfile -a sf_org
32+
33+
- name: List agent tests and set matrix
34+
id: set-matrix
35+
run: |
36+
TESTS=$(sf agent test list --target-org=sf_org --json | jq -c '[.result[].fullName]')
37+
if [ "$TESTS" == "[]" ]; then
38+
echo "No tests found. Failing early."
39+
exit 1
40+
fi
41+
echo "matrix={\"test\":$TESTS}" >> "$GITHUB_OUTPUT"
42+
43+
run-agent-test:
44+
name: Run Agent Test - ${{ matrix.test }}
45+
needs: list-tests
46+
runs-on: ubuntu-latest
47+
container: salesforce/cli:latest-slim
48+
strategy:
49+
fail-fast: false
50+
matrix: ${{ fromJson(needs.list-tests.outputs.matrix) }}
51+
52+
steps:
53+
- uses: actions/checkout@v4
54+
55+
- name: Authenticate Salesforce Org
56+
run: |
57+
echo "${{ secrets.ORG_SFDX_AUTH_URL }}" > ./authfile
58+
sf org login sfdx-url --sfdxurlfile=authfile -a sf_org
59+
60+
- name: Run test and get result
61+
id: test
62+
run: |
63+
mkdir -p test-results
64+
RUN_ID=$(sf agent test run --target-org=sf_org --api-name="${{ matrix.test }}" --wait 10 --json | jq -r '.result.runId')
65+
RESULT=$(sf agent test results --target-org=sf_org --job-id="$RUN_ID" --json)
66+
echo "$RESULT" > "test-results/${{ matrix.test }}.json"
67+
68+
- name: Upload individual result
69+
uses: actions/upload-artifact@v4
70+
with:
71+
name: agent-test-results-${{ matrix.test }}
72+
path: test-results/
73+
74+
validate-results:
75+
name: Validate Results
76+
needs: run-agent-test
77+
runs-on: ubuntu-latest
78+
79+
steps:
80+
- name: Download all test results
81+
uses: actions/download-artifact@v4
82+
with:
83+
path: all-results
84+
85+
- name: Summarize test outcomes
86+
id: summary
87+
run: |
88+
total=0
89+
passed=0
90+
failed=0
91+
92+
for file in all-results/**/*.json; do
93+
p=$(jq '[.result.testCases[] | .testResults[] | select(.result == "PASS")] | length' "$file")
94+
f=$(jq '[.result.testCases[] | .testResults[] | select(.result == "FAILURE")] | length' "$file")
95+
total=$((total + p + f))
96+
passed=$((passed + p))
97+
failed=$((failed + f))
98+
done
99+
100+
percentage=$((passed * 100 / total))
101+
echo "TOTAL=$total" >> "$GITHUB_OUTPUT"
102+
echo "PASSED=$passed" >> "$GITHUB_OUTPUT"
103+
echo "FAILED=$failed" >> "$GITHUB_OUTPUT"
104+
echo "PERCENT=$percentage" >> "$GITHUB_OUTPUT"
105+
106+
- name: Display summary
107+
run: |
108+
echo "=================================="
109+
echo " Agent Test Summary "
110+
echo "=================================="
111+
echo "Total Tests : ${{ steps.summary.outputs.TOTAL }}"
112+
echo "Tests Passed: ${{ steps.summary.outputs.PASSED }}"
113+
echo "Tests Failed: ${{ steps.summary.outputs.FAILED }}"
114+
echo "Pass : ${{ steps.summary.outputs.PERCENT }}%"
115+
echo "=================================="
116+
117+
- name: Enforce pass threshold
118+
if: ${{ steps.summary.outputs.PERCENT < 75 }}
119+
run: |
120+
echo "❌ Agent tests failed threshold (75%). Only ${{ steps.summary.outputs.PERCENT }}% passed."
121+
exit 1
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)