-
Notifications
You must be signed in to change notification settings - Fork 2
269 lines (219 loc) · 8.53 KB
/
agentforce-script-validate.yml
File metadata and controls
269 lines (219 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# This workflow is an example used during the Irish Dreamin'
# Agentforce DevOps: state of the art
# By @nabondance
name: Agentforce Script Validate
# This workflow runs Salesforce Agent tests and aggregates the results.
# It is split into several steps for better readability and understanding.
on:
pull_request:
types: [opened, synchronize, ready_for_review, reopened]
branches: [ main ]
jobs:
setup-deploy:
runs-on: ubuntu-latest
container: salesforce/cli:latest-slim
steps:
- name: Checkout Code
uses: actions/checkout@v5
- name: 'Scope project to agentforce-script'
run: cp agentforce-script/sfdx-project.json sfdx-project.json
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10
- name: Setup Node.js
id: setup-node
uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: pnpm
cache-dependency-path: 'pnpm-lock.yaml'
- name: Install Dependencies
id: pnpm-install
run: pnpm install
- name: 'Authenticate to Dev Hub'
run: |
echo "${{ secrets.DEVHUB_SFDX_AUTH_URL }}" > ./authfile
sf org login sfdx-url --sfdxurlfile=authfile --alias=devhub
- name: 'Get org from pool'
run: |
pnpm sfp pool fetch --targetdevhubusername=devhub --tag=ci-pool --alias=so-ci
- name: 'Deploy Agentforce Script to org'
run: |
sf project deploy start --target-org=so-ci --manifest=agentforce-script/manifest.xml --wait=30
- name: 'Publish Agentforce authoring bundles'
run: bash agentforce-script/scripts/publishAgents.sh so-ci
- name: 'Activate Agentforce agents'
run: bash agentforce-script/scripts/activateAgents.sh so-ci
- name: 'Patch test definitions with latest agent version'
run: bash agentforce-script/scripts/patchTestVersions.sh so-ci
- name: 'Deploy Agentforce Tests to org'
run: |
sf project deploy start --target-org=so-ci --source-dir=agentforce-script/force-app/main/default/aiEvaluationDefinitions --wait=30
- name: 'Capture org auth URL'
id: capture-org
run: |
AUTH_URL=$(sf org display --target-org=so-ci --verbose --json | tr -d '\000-\037' | jq -r '.result.sfdxAuthUrl')
mkdir -p auth-artifact
echo "$AUTH_URL" > auth-artifact/auth-url.txt
- name: Upload auth URL artifact
uses: actions/upload-artifact@v4
with:
name: org-auth-url
path: auth-artifact/
list-tests:
name: List Agent Tests
needs: setup-deploy
runs-on: ubuntu-latest
container: salesforce/cli:latest-slim
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v5
- name: Download auth URL artifact
uses: actions/download-artifact@v4
with:
name: org-auth-url
path: auth-artifact
- name: Authenticate to org
run: |
AUTH_URL=$(cat auth-artifact/auth-url.txt)
if [ -z "$AUTH_URL" ] || [ "$AUTH_URL" = "null" ]; then
echo "ERROR: Auth URL is empty or null from artifact"
exit 1
fi
echo "$AUTH_URL" > ./authfileci
sf org login sfdx-url --sfdxurlfile=authfileci --alias=so-ci
- name: List agent tests and set matrix
id: set-matrix
run: |
TESTS=$(sf agent test list --target-org=so-ci --json | jq -c '[.result[].fullName]')
if [ "$TESTS" == "[]" ]; then
echo "No tests found. Failing early."
exit 1
fi
echo "matrix={\"test\":$TESTS}" >> "$GITHUB_OUTPUT"
run-agent-test:
name: Run Agent Test - ${{ matrix.test }}
needs: [setup-deploy, list-tests]
runs-on: ubuntu-latest
container: salesforce/cli:latest-slim
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.list-tests.outputs.matrix) }}
steps:
- uses: actions/checkout@v5
- name: Download auth URL artifact
uses: actions/download-artifact@v4
with:
name: org-auth-url
path: auth-artifact
- name: Authenticate to org
run: |
AUTH_URL=$(cat auth-artifact/auth-url.txt)
echo "$AUTH_URL" > ./authfileci
sf org login sfdx-url --sfdxurlfile=authfileci --alias=so-ci
- name: Run test and get result
id: test
run: |
mkdir -p test-results
RUN_ID=$(sf agent test run --target-org=so-ci --api-name="${{ matrix.test }}" --wait 10 --json | jq -r '.result.runId')
RESULT=$(sf agent test results --target-org=so-ci --job-id="$RUN_ID" --json)
echo "$RESULT" | tr -d '\000-\037' > "test-results/${{ matrix.test }}.json"
- name: Upload individual result
uses: actions/upload-artifact@v4
with:
name: agent-test-results-${{ matrix.test }}
path: test-results/
validate-results:
name: Validate Results
needs: run-agent-test
runs-on: ubuntu-latest
steps:
- name: Download all test results
uses: actions/download-artifact@v4
with:
path: all-results
pattern: agent-test-results-*
merge-multiple: true
- name: Summarize test outcomes
id: summary
run: |
total=0
passed=0
failed=0
ls -al all-results/
if ! ls all-results/ 1> /dev/null 2>&1; then
echo "No test result files found"
exit 1
fi
for file in all-results/*; do
echo "Processing file: $file"
if ! jq -e '.result.testCases' "$file" > /dev/null 2>&1; then
echo "Skipping invalid or malformed JSON file: $file"
continue
fi
p=$(jq '[.result.testCases[]? | .testResults[]? | select(.result == "PASS")] | length' "$file" 2>/dev/null || echo "0")
f=$(jq '[.result.testCases[]? | .testResults[]? | select(.result == "FAILURE")] | length' "$file" 2>/dev/null || echo "0")
total=$((total + p + f))
passed=$((passed + p))
failed=$((failed + f))
echo "File $file: $p passed, $f failed"
done
if [ $total -eq 0 ]; then
echo "No test results found in any files"
exit 1
fi
percentage=$((passed * 100 / total))
echo "TOTAL=$total" >> "$GITHUB_OUTPUT"
echo "PASSED=$passed" >> "$GITHUB_OUTPUT"
echo "FAILED=$failed" >> "$GITHUB_OUTPUT"
echo "PERCENT=$percentage" >> "$GITHUB_OUTPUT"
- name: Display summary
run: |
echo "=================================="
echo " Agent Test Summary "
echo "=================================="
echo "Total Tests : ${{ steps.summary.outputs.TOTAL }}"
echo "Tests Passed: ${{ steps.summary.outputs.PASSED }}"
echo "Tests Failed: ${{ steps.summary.outputs.FAILED }}"
echo "Pass : ${{ steps.summary.outputs.PERCENT }}%"
echo "=================================="
- name: Enforce pass threshold
if: ${{ steps.summary.outputs.PERCENT < 75 }}
run: |
echo "Agent tests failed threshold (75%). Only ${{ steps.summary.outputs.PERCENT }}% passed."
exit 1
cleanup:
name: Return Org to Pool
needs: [setup-deploy, validate-results]
if: always()
runs-on: ubuntu-latest
container: salesforce/cli:latest-slim
steps:
- name: Checkout Code
uses: actions/checkout@v5
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10
- name: Install Dependencies
run: pnpm install
- name: Download auth URL artifact
uses: actions/download-artifact@v4
with:
name: org-auth-url
path: auth-artifact
- name: Authenticate to DevHub
run: |
echo "${{ secrets.DEVHUB_SFDX_AUTH_URL }}" > ./authfile
sf org login sfdx-url --sfdxurlfile=authfile --alias=devhub
- name: Authenticate to org
run: |
AUTH_URL=$(cat auth-artifact/auth-url.txt)
echo "$AUTH_URL" > ./authfileci
sf org login sfdx-url --sfdxurlfile=authfileci --alias=so-ci
- name: Return org to pool
run: |
ORG_ID=$(sf org display -o so-ci --json | jq -r '.result.id' | cut -c 1-15)
sf data update record -o devhub --sobject ScratchOrgInfo --where "ScratchOrg='$ORG_ID'" --values "Allocation_status__c='Available'"