forked from SiEPIC/openEBL-2025-10
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow-run-postlayout-sim.yml
More file actions
346 lines (275 loc) · 13.2 KB
/
workflow-run-postlayout-sim.yml
File metadata and controls
346 lines (275 loc) · 13.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
name: Post-layout simulation verification
# Runs if the layout functional verification passes
# Runs a circuit simulation, and returns the results
on:
workflow_run:
workflows: ["Run Layout Verification"]
types:
- completed
workflow_dispatch:
jobs:
secure-sim:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' && github.repository_owner == 'SiEPIC' }}
steps:
- name: Checkout triggering commit
uses: actions/checkout@v4
with:
repository: ${{ github.repository }}
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0
- name: Fetch base branch
run: |
git fetch origin main --no-tags --prune
- name: Debug Git info after checkout
run: |
echo "Checked out commit:"
git rev-parse HEAD
echo "Commit message:"
git log -1 --pretty=%B
echo "Changed files compared to main:"
git fetch origin main
git diff --name-only origin/main...HEAD > files_changed.txt
cat files_changed.txt
echo "Commits in this range:"
git log --pretty=format:'%h %an <%ae>' origin/main..HEAD || true
- name: Get PR number, username, and fork repo
id: getpr
run: |
echo "Finding PR for commit: ${{ github.event.workflow_run.head_sha }}"
echo "Branch: ${{ github.event.workflow_run.head_branch }}"
# Get all open PRs with their head SHA and branch info
PR_JSON=$(gh pr list --state open --json number,author,headRepositoryOwner,headRefName,headRefOid)
# Find the PR that matches both the commit SHA and branch name
# First try to match by SHA (most reliable)
MATCHING_PR=$(echo "$PR_JSON" | jq --arg sha "${{ github.event.workflow_run.head_sha }}" --arg branch "${{ github.event.workflow_run.head_branch }}" '
.[] | select(.headRefOid == $sha and .headRefName == $branch)
')
# If no exact SHA match, fall back to branch name with most recent update
if [ -z "$MATCHING_PR" ] || [ "$MATCHING_PR" = "null" ]; then
echo "No exact SHA match found, searching by branch name..."
MATCHING_PR=$(gh pr list --state open --json number,author,headRepositoryOwner,headRefName,updatedAt --search "head:${{ github.event.workflow_run.head_branch }}" --limit 100 | jq --arg branch "${{ github.event.workflow_run.head_branch }}" '
[.[] | select(.headRefName == $branch)] | sort_by(.updatedAt) | reverse | .[0]
')
fi
PR_NUMBER=$(echo "$MATCHING_PR" | jq -r '.number')
USERNAME=$(echo "$MATCHING_PR" | jq -r '.author.login')
FORK_OWNER=$(echo "$MATCHING_PR" | jq -r '.headRepositoryOwner.login')
REPO_NAME=$(basename "${{ github.repository }}")
FORK_REPO="$FORK_OWNER/$REPO_NAME"
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
echo "username=$USERNAME" >> $GITHUB_OUTPUT
echo "fork_repo=$FORK_REPO" >> $GITHUB_OUTPUT
echo "Found username: $USERNAME"
echo "Found PR #: $PR_NUMBER"
echo "Fork repo: $FORK_REPO"
env:
GH_TOKEN: ${{ secrets.PAT }}
- name: Detect layout files
id: check_files
run: |
USERNAME="${{ steps.getpr.outputs.username }}"
echo "Detected GitHub username: $USERNAME"
FILES=$(git diff --diff-filter=AM --name-only origin/main...HEAD | grep -i -E '\.(gds|oas)$' | sed 's|^submissions/||' || true)
if [ -z "$FILES" ]; then
echo "No .gds or .oas files in submissions/"
echo "found=false" >> $GITHUB_OUTPUT
FILES_ALL=$(git diff --diff-filter=AM --name-only origin/main...HEAD || true)
if [ -z "$FILES_ALL" ]; then
echo "No files in pull request"
else
echo "All files in pull request: $FILES_ALL"
fi
else
echo "$FILES"
echo "$FILES" > files_to_copy.txt
echo "found=true" >> $GITHUB_OUTPUT
fi
- name: Setup SSH
if: steps.check_files.outputs.found == 'true'
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keygen -l -f ~/.ssh/id_ed25519 || echo "Invalid SSH key"
# Write SSH config with ProxyJump
cat <<EOF > ~/.ssh/config
Host turing
HostName turing.phas.ubc.ca
User lukasc
Port 7743
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
ForwardAgent yes
Host baker
User lukasc
ProxyJump turing
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
ForwardAgent yes
EOF
chmod 600 ~/.ssh/config
- name: Copy .py files to Baker
if: steps.check_files.outputs.found == 'true'
run: |
PR_NUMBER="${{ steps.getpr.outputs.pr_number }}"
USERNAME="${{ steps.getpr.outputs.username }}"
echo "Simulating for PR $PR_NUMBER"
if [ -z "$USERNAME" ]; then
echo "❌ USERNAME is blank. Refusing to proceed."
exit 1
fi
echo "simulation files:"
cat files_to_copy.txt
ssh baker "mkdir -p ~/simulations/$USERNAME; rm -rf ~/simulations/$USERNAME/*"
# Create space-separated list of existing files
FILES=$(awk '{print "submissions/" $0}' files_to_copy.txt | xargs -r ls 2>/dev/null)
if [ -z "$FILES" ]; then
echo "⚠️ No existing files to copy. Exiting."
exit 0
fi
echo "Copying files to baker..."
scp $FILES baker:~/simulations/$USERNAME/
# - name: Copy .py files to Baker
# if: steps.check_files.outputs.found == 'true'
# run: |
# PR_NUMBER="${{ steps.getpr.outputs.pr_number }}"
# echo "Simulating for PR $PR_NUMBER"
# echo "simulation files:"
# cat files_to_copy.txt
# USERNAME="${{ steps.getpr.outputs.username }}"
# ssh baker "mkdir -p ~/simulations/$USERNAME; rm -rf ~/simulations/$USERNAME/*"
# while IFS= read -r file; do
# if [ -f "submissions/$file" ]; then
# echo "Copying $file to baker:~/simulations/$USERNAME/"
# scp "submissions/$file" baker:~/simulations/$USERNAME/
# if [ $? -ne 0 ]; then
# echo "❌ Failed to copy $file"
# exit 1
# fi
# else
# echo "⚠️ File submissions/$file not found (possibly deleted), skipping."
# fi
# done < files_to_copy.txt
- name: Run simulation and fetch results
if: steps.check_files.outputs.found == 'true'
run: |
USERNAME="${{ steps.getpr.outputs.username }}"
PR_NUMBER="${{ steps.getpr.outputs.pr_number }}"
FILE_LIST=$(<files_to_copy.txt)
echo "Running remote simulation for $USERNAME"
echo "Formatting comment..."
echo "This is an experimental feature. The goal is to run a circuit simulation on the design, and return the results for human verification." > comment.txt
echo "" >> comment.txt
for file in $FILE_LIST; do
FILE_BASENAME=$(basename "$file")
FILE_REMOTE="~/simulations/$USERNAME/$FILE_BASENAME"
REMOTE_DIR="~/simulations/$USERNAME"
echo "Starting simulation on Baker for $FILE_BASENAME"
# 5 min time limit
timeout 300s ssh baker "cd simulations && python3 run_circuit_simulation.py $USERNAME/$FILE_BASENAME" \
| tee sim_stdout.txt
RET_CODE=${PIPESTATUS[0]} # capture ssh return code with pipe
# RET_CODE=$?
echo "SSH return code: $RET_CODE"
if [ $RET_CODE -eq 124 ]; then
echo "❌ SSH command timed out."
fi
echo "Fetching new output files from Baker..."
timeout 60s rsync -avz --ignore-existing --update baker:simulations/$USERNAME/ ./output/
RSYNC_RET=$?
if [ $RSYNC_RET -eq 124 ]; then
echo "❌ rsync command timed out."
fi
if [ $RET_CODE -eq 0 ]; then
echo "✅ Simulation results for \`$FILE_BASENAME\`" >> comment.txt
else
echo "❌ Simulation failed for \`$FILE_BASENAME\` (exit code $RET_CODE)" >> comment.txt
fi
echo "" >> comment.txt
if [ -s sim_stdout.txt ]; then
echo "**stdout:**" >> comment.txt
echo '```' >> comment.txt
cat sim_stdout.txt >> comment.txt
echo '```' >> comment.txt
echo "" >> comment.txt
fi
#if [ -s sim_stderr.txt ]; then
# echo "**stderr:**" >> comment.txt
# echo '```' >> comment.txt
# cat sim_stderr.txt >> comment.txt
# echo '```' >> comment.txt
# echo "" >> comment.txt
#fi
done
- name: Commit PNGs to PR branch submissions folder
if: steps.check_files.outputs.found == 'true'
run: |
PR_NUMBER="${{ steps.getpr.outputs.pr_number }}"
USERNAME="${{ steps.getpr.outputs.username }}"
BRANCH_NAME="${{ github.event.workflow_run.head_branch }}"
echo "Resolving fork repository for PR #$PR_NUMBER..."
FORK_REPO=${{ steps.getpr.outputs.fork_repo }}
echo "Committing PNGs to forked PR branch: $FORK_REPO $BRANCH_NAME"
# Clone forked repo + checkout PR branch
echo "git clone"
git clone --depth=1 --branch "$BRANCH_NAME" "https://x-access-token:${{ secrets.PAT }}@github.com/$FORK_REPO.git" fork-pr
echo "cd fork-pr"
cd fork-pr
echo "git config"
git config user.name "github-actions"
git config user.email "github-actions@github.com"
mkdir -p submissions
mkdir -p submissions/circuit_simulations
cp ../output/*.png submissions/circuit_simulations/ || echo "No PNGs found"
echo "git add"
git add submissions/circuit_simulations/*.png || true
echo "git commit"
git commit -m "Add simulation result PNGs for PR #$PR_NUMBER" || echo "No changes"
echo "git push origin"
git push origin "$BRANCH_NAME"
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
- name: Append PNG links to comment
if: steps.check_files.outputs.found == 'true'
run: |
echo "📸 Simulation result image(s):" >> comment.txt
BRANCH_NAME="${{ github.event.workflow_run.head_branch }}"
FORK_REPO="${{ steps.getpr.outputs.fork_repo }}"
for img in output/*.png; do
[ -f "$img" ] || continue
FILENAME=$(basename "$img")
echo "" >> comment.txt
done
echo "" >> comment.txt
# echo "_To re-run this simulation, comment **please resimulate** below._" >> comment.txt # not working
echo "_To re-run this simulation, please make a change to the layout file (simply running V verification will change the date stamp in the layout), save it, and upload it to your GitHub fork. This will trigger this simulation to run again._" >> comment.txt
echo "" >> comment.txt
echo " I hereby grant you permission to use these plots for inclusion in a course report." >> comment.txt
echo "" >> comment.txt
#echo "_We have implemented automated computation of the ebeam-dc-halfring-straight s-parameters, for TE 1550. If the parameters chosen in the design were missing, a simulation job was submitted, and the CML on the server will be automatically updated, and available on for future circuit simulations._" >> comment.txt
#echo "" >> comment.txt
- name: Find existing simulation comment
uses: peter-evans/find-comment@v3
if: steps.check_files.outputs.found == 'true'
id: fc
with:
issue-number: ${{ steps.getpr.outputs.pr_number }}
body-includes: "This is an experimental feature."
env:
GH_TOKEN: ${{ secrets.PAT }}
- name: Update comment
if: steps.fc.outputs.comment-id != ''
run: |
dt=$(TZ='America/Los_Angeles' date '+%Y-%m-%d %H:%M:%S %Z')
echo " - Simulation date: $dt " >> comment.txt
- name: Post simulation results as PR comment
if: steps.check_files.outputs.found == 'true'
uses: peter-evans/create-or-update-comment@v3
with:
token: ${{ secrets.PAT }}
repository: ${{ github.repository }}
issue-number: ${{ steps.getpr.outputs.pr_number }}
body-path: comment.txt
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace