forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-and-read-benchmarks.sh
More file actions
executable file
·114 lines (93 loc) · 3.83 KB
/
commit-and-read-benchmarks.sh
File metadata and controls
executable file
·114 lines (93 loc) · 3.83 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
#!/bin/bash
set -euo pipefail
# Script to commit benchmark results and read them for GitHub Actions output
# Usage: ./commit-and-read-benchmarks.sh <output_dir> <github_event_name> <github_repository>
OUTPUT_DIR="${1:-benches}"
GITHUB_EVENT_NAME="${2:-pull_request}"
GITHUB_REPOSITORY="${3:-}"
# Global variable for branch name
BRANCH_NAME=""
# Function to commit benchmark results
commit_results() {
echo "Configuring git..."
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# For PR runs, fetch and checkout the PR branch to ensure we're up to date
if [ "$GITHUB_EVENT_NAME" = "pull_request" ] && [ -n "${GITHUB_HEAD_REF:-}" ]; then
echo "Fetching latest changes for PR branch: $GITHUB_HEAD_REF"
git fetch origin "$GITHUB_HEAD_REF"
git checkout -B "$GITHUB_HEAD_REF" "origin/$GITHUB_HEAD_REF"
fi
echo "Adding benchmark file..."
git add "$OUTPUT_DIR/LATEST.md"
if git diff --staged --quiet; then
echo "No changes to commit"
else
echo "Committing benchmark results..."
git commit -m "chore(\`benches\`): update benchmark results
🤖 Generated with [Foundry Benchmarks](https://github.com/${GITHUB_REPOSITORY}/actions)
Co-Authored-By: github-actions <github-actions@github.com>"
echo "Pushing to repository..."
if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]; then
# For manual runs, we're on a new branch
git push origin "$BRANCH_NAME"
elif [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
# For PR runs, push to the PR branch
if [ -n "${GITHUB_HEAD_REF:-}" ]; then
echo "Pushing to PR branch: $GITHUB_HEAD_REF"
git push origin "$GITHUB_HEAD_REF"
else
echo "Error: GITHUB_HEAD_REF not set for pull_request event"
exit 1
fi
else
# This workflow should only run on workflow_dispatch or pull_request
echo "Error: Unexpected event type: $GITHUB_EVENT_NAME"
echo "This workflow only supports 'workflow_dispatch' and 'pull_request' events"
exit 1
fi
echo "Successfully pushed benchmark results"
fi
}
# Function to read benchmark results and output for GitHub Actions
read_results() {
if [ -f "$OUTPUT_DIR/LATEST.md" ]; then
echo "Reading benchmark results..."
# Output full results
{
echo 'results<<EOF'
cat "$OUTPUT_DIR/LATEST.md"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
# Format results for PR comment
echo "Formatting results for PR comment..."
FORMATTED_COMMENT=$("$(dirname "$0")/format-pr-comment.sh" "$OUTPUT_DIR/LATEST.md")
{
echo 'pr_comment<<EOF'
echo "$FORMATTED_COMMENT"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
echo "Successfully read and formatted benchmark results"
else
echo 'results=No benchmark results found.' >> "$GITHUB_OUTPUT"
echo 'pr_comment=No benchmark results found.' >> "$GITHUB_OUTPUT"
echo "Warning: No benchmark results found at $OUTPUT_DIR/LATEST.md"
fi
}
# Main execution
echo "Starting benchmark results processing..."
# Create new branch for manual runs
if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]; then
echo "Manual workflow run detected, creating new branch..."
BRANCH_NAME="benchmarks/results-$(date +%Y%m%d-%H%M%S)"
git checkout -b "$BRANCH_NAME"
echo "Created branch: $BRANCH_NAME"
# Output branch name for later use
echo "branch_name=$BRANCH_NAME" >> "$GITHUB_OUTPUT"
fi
# Always commit benchmark results
echo "Committing benchmark results..."
commit_results
# Always read results for output
read_results
echo "Benchmark results processing complete"