-
Notifications
You must be signed in to change notification settings - Fork 1
153 lines (130 loc) · 5.65 KB
/
ci-build-and-test.yml
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
name: CI Build And Test Workflow
on:
pull_request:
types: [opened]
workflow_dispatch:
jobs:
build-and-test:
runs-on: ubuntu-latest
env:
SEED_PHRASE_1: ${{ secrets.SEED_PHRASE_1 }}
SEED_PHRASE_2: ${{ secrets.SEED_PHRASE_2 }}
SEED_PHRASE_3: ${{ secrets.SEED_PHRASE_3 }}
SEED_PHRASE_4: ${{ secrets.SEED_PHRASE_4 }}
SEED_PHRASE_5: ${{ secrets.SEED_PHRASE_5 }}
SEED_PHRASE_6: ${{ secrets.SEED_PHRASE_6 }}
SEED_PHRASE_7: ${{ secrets.SEED_PHRASE_7 }}
SEED_PHRASE_8: ${{ secrets.SEED_PHRASE_8 }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'
- name: Install dependencies
run: npm ci
- name: Run commitlint
run: npx commitlint --from origin/main --to HEAD
- name: Lint
run: npm run lint
- name: Run build
run: npm run build
- name: Run tests & generate test output files
id: run_tests
run: |
mkdir -p reports
set -o pipefail && npm run test 2>&1 | tee reports/test_output.txt
- name: Parse Test Results and Coverage
id: parse_results
if: ${{ !cancelled() }}
run: |
TEST_OUTPUT=$(cat reports/test_output.txt || echo "")
# Initialize variables
PASSED=0
FAILED=0
SKIPPED=0
TOTAL=0
SUITE_FAILED=0
OVERALL_STATUS="failure"
OVERALL_STATUS_EMOJI=":red_circle:"
SLACK_MESSAGE="*Test report was not available to parse.*"
# Initialize coverage variables
STATEMENTS=0
BRANCHES=0
FUNCTIONS=0
LINES=0
COVERAGE_MESSAGE=""
# Check if test report is available
if [ -s reports/test_output.txt ]; then
# Parse suite results
if echo "$TEST_OUTPUT" | grep -q 'Test Suites:'; then
SUITE_FAILED=$(echo "$TEST_OUTPUT" | awk '/Test Suites:/ {for(i=1;i<=NF;i++) if($i=="failed,") print $(i-1)}')
fi
# Parse individual test results
if echo "$TEST_OUTPUT" | grep -q 'Tests:'; then
PASSED=$(echo "$TEST_OUTPUT" | awk '/Tests:/ {for(i=1;i<=NF;i++) if($i=="passed,") print $(i-1)}')
FAILED=$(echo "$TEST_OUTPUT" | awk '/Tests:/ {for(i=1;i<=NF;i++) if($i=="failed,") print $(i-1)}')
SKIPPED=$(echo "$TEST_OUTPUT" | awk '/Tests:/ {for(i=1;i<=NF;i++) if($i=="skipped,") print $(i-1)}')
TOTAL=$(echo "$TEST_OUTPUT" | awk '/Tests:/ {for(i=1;i<=NF;i++) if($i=="total") print $(i-1)}')
PASSED=${PASSED:-0}
FAILED=${FAILED:-0}
SKIPPED=${SKIPPED:-0}
TOTAL=${TOTAL:-0}
SLACK_MESSAGE="*TEST RESULTS*\n===============================\n*PASSED:* $PASSED | *FAILED:* $FAILED | *SKIPPED:* $SKIPPED | *TOTAL:* $TOTAL"
fi
# Parse coverage summary
if echo "$TEST_OUTPUT" | grep -q 'Coverage summary'; then
STATEMENTS=$(echo "$TEST_OUTPUT" | awk '/Statements/ {print $3}' | grep -o '[0-9.]\+')
BRANCHES=$(echo "$TEST_OUTPUT" | awk '/Branches/ {print $3}' | grep -o '[0-9.]\+')
FUNCTIONS=$(echo "$TEST_OUTPUT" | awk '/Functions/ {print $3}' | grep -o '[0-9.]\+')
LINES=$(echo "$TEST_OUTPUT" | awk '/Lines/ {print $3}' | grep -o '[0-9.]\+')
COVERAGE_MESSAGE="*COVERAGE*\n===============================\n*Statements:* ${STATEMENTS}% | *Branches:* ${BRANCHES}% | *Functions:* ${FUNCTIONS}% | *Lines:* ${LINES}%"
fi
# Check if no tests were run
if [ "$TOTAL" -eq 0 ]; then
OVERALL_STATUS="failure"
OVERALL_STATUS_EMOJI=":red_circle:"
SLACK_MESSAGE="*No tests were executed!*"
fi
# If any test suite failed, override the overall status to failure
if [ "$SUITE_FAILED" -gt 0 ]; then
OVERALL_STATUS="failure"
OVERALL_STATUS_EMOJI=":red_circle:"
SLACK_MESSAGE="$SLACK_MESSAGE *(Test Suites Failed: $SUITE_FAILED)*"
elif [ "$FAILED" -eq 0 ] && [ "$TOTAL" -gt 0 ]; then
OVERALL_STATUS="success"
OVERALL_STATUS_EMOJI=":large_green_circle:"
fi
else
echo "test_output.txt is empty or not found."
fi
# Append coverage message to Slack message
if [ -n "$COVERAGE_MESSAGE" ]; then
SLACK_MESSAGE="$SLACK_MESSAGE\n\n$COVERAGE_MESSAGE"
fi
# Set variables required for other workflow steps
echo "OVERALL_STATUS=$OVERALL_STATUS" >> $GITHUB_ENV
echo "OVERALL_STATUS_EMOJI=$OVERALL_STATUS_EMOJI" >> $GITHUB_ENV
echo "SLACK_MESSAGE=$SLACK_MESSAGE" >> $GITHUB_ENV
- name: Send Slack Notification
if: ${{ !cancelled() }}
uses: slackapi/[email protected]
with:
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${{ env.OVERALL_STATUS_EMOJI }} *zkVerifyJS Status:* ${{ env.OVERALL_STATUS }} ${{ env.OVERALL_STATUS_EMOJI }}\n\n${{ env.SLACK_MESSAGE }}\n\n*Build URL:* <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|Click here to view the build>"
}
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.QA_SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK