fix EXAMPLES.md #24
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |