Skip to content

Commit 9843d83

Browse files
authored
Merge pull request #21 from shiningflash/workflows-improvement
Workflows improvement
2 parents 56d2bf0 + 8cdf400 commit 9843d83

File tree

1 file changed

+103
-21
lines changed

1 file changed

+103
-21
lines changed

.github/workflows/ci.yml

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: CI - Code Testing Workflow
22

33
on:
44
pull_request:
5+
types: [opened, synchronize, reopened, ready_for_review]
56
branches:
67
- develop
78
- main
@@ -10,52 +11,133 @@ on:
1011
paths:
1112
- "src/**"
1213
- "tests/**"
13-
14-
permissions:
15-
contents: write
16-
pull-requests: write
17-
checks: write
14+
- ".github/workflows/**"
15+
push:
16+
branches:
17+
- develop
18+
- main
19+
paths:
20+
- "src/**"
21+
- "tests/**"
22+
- ".github/workflows/**"
1823

1924
jobs:
2025
test:
2126
name: Run Tests and Post Coverage
27+
concurrency:
28+
group: allure-${{ github.event.pull_request.number || github.ref }}
29+
cancel-in-progress: true
2230
runs-on: ubuntu-latest
23-
24-
env: # Load environment variables from repository secrets
31+
permissions:
32+
contents: write
33+
pull-requests: write
34+
env:
2535
BASE_URL: ${{ secrets.BASE_URL }}
2636
API_KEY: ${{ secrets.API_KEY }}
2737
WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }}
2838

2939
steps:
30-
# Step 1: Checkout the repository code
3140
- name: Checkout code
3241
uses: actions/checkout@v3
3342

34-
# Step 2: Set up Python environment
3543
- name: Set up Python
3644
uses: actions/setup-python@v4
3745
with:
3846
python-version: "3.9"
3947

40-
# Step 3: Install dependencies
48+
- name: Set up Node (for Allure CLI)
49+
uses: actions/setup-node@v4
50+
with:
51+
node-version: "18"
52+
4153
- name: Install dependencies
4254
run: |
43-
python -m venv venv # Create a virtual environment
44-
source venv/bin/activate # Activate the virtual environment
45-
pip install --upgrade pip # Upgrade pip
46-
pip install pytest pytest-cov # Install coverage tools
47-
pip install -r requirements.txt # Install project dependencies
48-
49-
# Step 4: Run tests with coverage
50-
- name: Run tests with coverage
55+
python -m venv venv
56+
source venv/bin/activate
57+
pip install --upgrade pip
58+
pip install pytest pytest-cov allure-pytest
59+
pip install -r requirements.txt
60+
npm install -g allure-commandline
61+
62+
- name: Run tests (coverage + allure results)
63+
id: run_tests
5164
run: |
5265
source venv/bin/activate
53-
pytest --cov=src --cov-report=xml --cov-report=term > coverage.txt
54-
pytest --junitxml=pytest.xml
66+
pytest --cov=src --cov-report=xml --cov-report=term \
67+
--alluredir=allure-results --junitxml=pytest.xml --tb=short > coverage.txt 2>&1 || echo "TESTS_FAILED=true" >> $GITHUB_ENV
68+
continue-on-error: true
5569

56-
# Step 5: Post coverage summary to the pull request
5770
- name: Pytest coverage comment
71+
if: github.event_name == 'pull_request' && always()
5872
uses: MishaKav/pytest-coverage-comment@main
5973
with:
6074
pytest-coverage-path: ./coverage.txt
6175
junitxml-path: ./pytest.xml
76+
77+
- name: Pull previous Allure history (if exists)
78+
if: always()
79+
run: |
80+
git fetch origin allure-report || true
81+
if git ls-remote --exit-code origin allure-report; then
82+
git checkout origin/allure-report -- history || true
83+
if [ -d history ]; then
84+
mkdir -p allure-results/history
85+
cp -r history/* allure-results/history/ || true
86+
fi
87+
fi
88+
89+
- name: Generate Allure Report
90+
if: always()
91+
run: |
92+
allure generate allure-results -o allure-report --clean || echo "No allure-results to generate"
93+
echo "<!-- build: run-${{ github.run_number }} -->" >> allure-report/index.html
94+
touch allure-report/.nojekyll
95+
96+
- name: Deploy Allure Report to GitHub Pages branch
97+
if: always()
98+
uses: peaceiris/actions-gh-pages@v3
99+
with:
100+
github_token: ${{ secrets.GITHUB_TOKEN }}
101+
publish_branch: allure-report
102+
publish_dir: ./allure-report
103+
keep_files: true
104+
force_orphan: false
105+
commit_message: "docs(allure): update report for ${{ github.sha }} (run ${{ github.run_number }})"
106+
107+
- name: Comment PR with Allure Report URL
108+
if: github.event_name == 'pull_request' && always()
109+
uses: actions/github-script@v7
110+
with:
111+
script: |
112+
const owner = context.repo.owner;
113+
const repo = context.repo.repo;
114+
const bust = `${context.runNumber}`;
115+
const reportUrl = `https://${owner}.github.io/${repo}/?run=${bust}`;
116+
117+
// Determine test result color and icon
118+
const testsFailed = process.env.TESTS_FAILED === 'true';
119+
const badgeColor = testsFailed ? 'E63946' : '2EA44F'; // red : green
120+
const testStatus = testsFailed ? '⚠️ (Some tests failed)' : '✅ All tests passed';
121+
122+
const body = `## 📊 Test Report ${testStatus}
123+
124+
Coverage trends • Module breakdown • Failure analysis • Execution timeline
125+
126+
[![View Allure Dashboard](https://img.shields.io/badge/View%20Dashboard-${badgeColor}?style=for-the-badge&logo=google-chrome&logoColor=white)](${reportUrl})
127+
128+
⚠️ NB: If the dashboard doesn't update immediately, try a hard reload (Cmd + Shift + R on Mac) or open in incognito mode.`;
129+
130+
const { data: comments } = await github.rest.issues.listComments({
131+
owner, repo, issue_number: context.issue.number
132+
});
133+
const marker = '📊 Test Report';
134+
const existing = comments.find(c => c.user.type === 'Bot' && c.body.includes(marker));
135+
if (existing) {
136+
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
137+
} else {
138+
await github.rest.issues.createComment({ owner, repo, issue_number: context.issue.number, body });
139+
}
140+
141+
- name: Fail job if tests failed
142+
if: env.TESTS_FAILED == 'true'
143+
run: exit 1

0 commit comments

Comments
 (0)