-
Notifications
You must be signed in to change notification settings - Fork 3
96 lines (79 loc) · 3.09 KB
/
ci.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
name: CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: self-hosted
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install black flake8
- name: Run install.py for database setup
run: python install.py --ci # Run this in CI mode to initialize the database
- name: Run flake8
run: flake8 src/ tests/ | tee flake8-output.log
- name: Run black
run: black --check src/ tests/ | tee black-output.log
- name: Run tests
run: pytest | tee pytest-output.log
- name: Push logs to Loki
env:
LOKI_URL: ${{ secrets.LOKI_URL }}
LOKI_USER: ${{ secrets.LOKI_USER }}
LOKI_PASS: ${{ secrets.LOKI_PASS }}
run: |
TIMESTAMP=$(date +%s%N)
LOG_FILE=flake8-output.log
ERROR_LOG_FILE=black-output.log
TEST_LOG_FILE=pytest-output.log
# Create temporary files to store URL-encoded logs
ENCODED_LOG_FILE=$(mktemp)
ENCODED_ERROR_LOG_FILE=$(mktemp)
ENCODED_TEST_LOG_FILE=$(mktemp)
# URL-encode log content
python -c "import urllib.parse; import sys; print(urllib.parse.quote(open('$LOG_FILE').read()))" > $ENCODED_LOG_FILE
python -c "import urllib.parse; import sys; print(urllib.parse.quote(open('$ERROR_LOG_FILE').read()))" > $ENCODED_ERROR_LOG_FILE
python -c "import urllib.parse; import sys; print(urllib.parse.quote(open('$TEST_LOG_FILE').read()))" > $ENCODED_TEST_LOG_FILE
# Capture CI job log status
CI_STATUS="INFO: CI job completed successfully"
# If any of the logs contain errors, adjust CI_STATUS
if grep -q "ERROR" $LOG_FILE || grep -q "ERROR" $ERROR_LOG_FILE || grep -q "ERROR" $TEST_LOG_FILE; then
CI_STATUS="ERROR: CI job encountered issues"
fi
# Send logs to Loki
curl -u "$LOKI_USER:$LOKI_PASS" -X POST "$LOKI_URL/loki/api/v1/push" \
-H "Content-Type: application/json" \
-d '{
"streams": [
{
"stream": { "job": "ci-cd-pipeline", "level": "INFO" },
"values": [
["'"$TIMESTAMP"'", "'"$CI_STATUS"'"]
]
},
{
"stream": { "job": "ci-cd-pipeline", "level": "ERROR" },
"values": [
["'"$TIMESTAMP"'", "'"$(cat $ENCODED_LOG_FILE) $(cat $ENCODED_ERROR_LOG_FILE) $(cat $ENCODED_TEST_LOG_FILE)"'"]
]
}
]
}'
# Clean up temporary files
rm $ENCODED_LOG_FILE $ENCODED_ERROR_LOG_FILE $ENCODED_TEST_LOG_FILE