Skip to content

Commit 680490d

Browse files
authored
Merge pull request #62 from gsinghjay/ci/reusable-workflows
ci: optimize workflow job dependencies
2 parents ec071ed + d57959b commit 680490d

File tree

5 files changed

+183
-140
lines changed

5 files changed

+183
-140
lines changed

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
push:
7+
branches: [ main ]
8+
workflow_call:
9+
secrets:
10+
token:
11+
required: true
12+
description: "GitHub token for authentication"
13+
workflow_dispatch:
14+
15+
jobs:
16+
setup:
17+
name: Setup Environment
18+
uses: ./.github/workflows/setup-python.yml
19+
with:
20+
python-version: "3.11"
21+
22+
lint:
23+
name: Lint Check
24+
needs: [setup]
25+
uses: ./.github/workflows/lint.yml
26+
with:
27+
venv-name: ${{ needs.setup.outputs.venv-name }}
28+
python-version: "3.11"
29+
permissions:
30+
contents: read
31+
pull-requests: read
32+
secrets:
33+
token: ${{ secrets.token }}
34+
35+
test:
36+
name: Run Tests
37+
needs: [setup]
38+
uses: ./.github/workflows/test.yml
39+
with:
40+
venv-name: ${{ needs.setup.outputs.venv-name }}
41+
python-version: "3.11"
42+
permissions:
43+
contents: read
44+
checks: write

.github/workflows/lint.yml

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,69 @@
11
name: Lint
22

33
on:
4-
workflow_call: {} # Allow this workflow to be called by other workflows
5-
pull_request:
6-
branches: [ main ]
7-
push:
8-
branches: [ main ]
9-
workflow_dispatch:
4+
workflow_call:
5+
inputs:
6+
venv-name:
7+
required: true
8+
type: string
9+
description: "Name of the virtual environment artifact"
10+
python-version:
11+
required: false
12+
type: string
13+
default: "3.11"
14+
description: "Python version to use"
15+
secrets:
16+
token:
17+
required: true
18+
description: "GitHub token for authentication"
1019

1120
permissions:
1221
contents: read
1322
pull-requests: read
1423

1524
jobs:
16-
setup:
17-
name: Setup Environment
18-
uses: ./.github/workflows/setup-python.yml
19-
with:
20-
python-version: "3.11"
21-
2225
lint:
2326
name: Run Linters
24-
needs: [setup]
2527
runs-on: ubuntu-latest
26-
# Skip on release commits
27-
if: |
28-
(github.event_name == 'pull_request') ||
29-
(github.actor != 'github-actions[bot]' &&
30-
!contains(github.event.head_commit.message, '[skip ci]') &&
31-
!contains(github.event.head_commit.message, '[ci skip]') &&
32-
!contains(github.event.head_commit.message, '[no ci]') &&
33-
!contains(github.event.head_commit.message, 'chore(release)'))
34-
3528
steps:
3629
- name: Check out code
3730
uses: actions/checkout@v4
31+
with:
32+
fetch-depth: 0
3833

3934
- name: Set up Python
4035
uses: actions/setup-python@v4
4136
with:
42-
python-version: "3.11"
43-
44-
- name: Install Poetry
45-
run: |
46-
pip install poetry==1.8.5
47-
poetry config virtualenvs.in-project true
37+
python-version: ${{ inputs.python-version }}
4838

49-
- name: Cache dependencies
50-
uses: actions/cache@v3
39+
- name: Download virtual environment
40+
uses: actions/download-artifact@v4
5141
with:
52-
path: |
53-
~/.cache/pip
54-
~/.cache/pypoetry
55-
.venv
56-
key: ${{ runner.os }}-python-3.11-${{ hashFiles('**/poetry.lock') }}
57-
restore-keys: |
58-
${{ runner.os }}-python-3.11-
59-
${{ runner.os }}-python-
42+
name: ${{ inputs.venv-name }}
43+
path: artifact
6044

61-
- name: Install dependencies
62-
run: poetry install
45+
- name: Debug downloaded artifact
46+
run: |
47+
echo "Artifact directory contents:"
48+
ls -la artifact/
49+
echo "Current directory contents:"
50+
ls -la
51+
52+
- name: Restore virtual environment
53+
run: |
54+
if [ ! -f "artifact/venv.tar.gz" ]; then
55+
echo "ERROR: venv.tar.gz not found in artifact"
56+
exit 1
57+
fi
58+
tar xzf artifact/venv.tar.gz
59+
cp artifact/poetry.lock .
60+
cp artifact/pyproject.toml .
61+
chmod -R +x .venv/bin/
6362
6463
- name: Run Black
65-
run: poetry run black --check .
64+
run: .venv/bin/black --check .
6665

6766
- name: Check commit messages
6867
uses: wagoid/commitlint-github-action@v5
69-
with:
70-
configFile: commitlint.config.js
71-
failOnWarnings: true
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.token }}

.github/workflows/release.yml

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,19 @@ permissions:
3030
id-token: write
3131

3232
jobs:
33-
lint:
34-
name: Lint Check
35-
uses: ./.github/workflows/lint.yml
33+
ci:
34+
name: Run CI Checks
35+
uses: ./.github/workflows/ci.yml
3636
permissions:
3737
contents: read
3838
pull-requests: read
39-
40-
test:
41-
name: Run Tests
42-
needs: [lint]
43-
uses: ./.github/workflows/test.yml
44-
permissions:
45-
contents: read
4639
checks: write
47-
48-
setup:
49-
name: Setup Environment
50-
needs: [test]
51-
uses: ./.github/workflows/setup-python.yml
52-
with:
53-
python-version: "3.11"
40+
secrets:
41+
token: ${{ secrets.GITHUB_TOKEN }}
5442

5543
release:
5644
name: Create Release
57-
needs: [setup]
45+
needs: [ci]
5846
runs-on: ubuntu-latest
5947
if: |
6048
github.ref == 'refs/heads/main' &&
@@ -82,33 +70,35 @@ jobs:
8270
with:
8371
python-version: "3.11"
8472

85-
- name: Install Poetry
86-
run: |
87-
pip install poetry==1.8.5
88-
poetry config virtualenvs.in-project true
89-
90-
- name: Cache dependencies
91-
uses: actions/cache@v3
73+
- name: Download virtual environment
74+
uses: actions/download-artifact@v4
9275
with:
93-
path: |
94-
~/.cache/pip
95-
~/.cache/pypoetry
96-
.venv
97-
key: ${{ runner.os }}-python-3.11-${{ hashFiles('**/poetry.lock') }}
98-
restore-keys: |
99-
${{ runner.os }}-python-3.11-
100-
${{ runner.os }}-python-
76+
name: ${{ needs.ci.outputs.venv-name }}
77+
path: artifact
78+
79+
- name: Debug downloaded artifact
80+
run: |
81+
echo "Artifact directory contents:"
82+
ls -la artifact/
83+
echo "Current directory contents:"
84+
ls -la
10185
102-
- name: Install dependencies
103-
run: poetry install
86+
- name: Restore virtual environment
87+
run: |
88+
if [ ! -f "artifact/venv.tar.gz" ]; then
89+
echo "ERROR: venv.tar.gz not found in artifact"
90+
exit 1
91+
fi
92+
tar xzf artifact/venv.tar.gz
93+
cp artifact/poetry.lock .
94+
cp artifact/pyproject.toml .
95+
chmod -R +x .venv/bin/
10496
10597
- name: Python Semantic Release
10698
id: release
10799
uses: python-semantic-release/python-semantic-release@v9.15.0
108100
env:
109101
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
110-
VIRTUAL_ENV: ${{ env.VIRTUAL_ENV }}
111-
PATH: ${{ env.PATH }}
112102
with:
113103
github_token: ${{ secrets.GITHUB_TOKEN }}
114104
directory: .

.github/workflows/setup-python.yml

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ on:
99
default: "3.11"
1010
description: "Python version to use"
1111
outputs:
12-
cache-hit:
13-
description: "Whether there was a cache hit for dependencies"
14-
value: ${{ jobs.setup.outputs.cache-hit }}
12+
venv-name:
13+
description: "Name of the virtual environment artifact"
14+
value: ${{ jobs.setup.outputs.venv-name }}
1515

1616
jobs:
1717
setup:
1818
name: Setup Python
1919
runs-on: ubuntu-latest
2020
outputs:
21-
cache-hit: ${{ steps.cache-deps.outputs.cache-hit }}
21+
venv-name: venv-${{ github.run_id }}
2222
steps:
2323
- name: Check out code
2424
uses: actions/checkout@v4
@@ -31,19 +31,6 @@ jobs:
3131
python-version: ${{ inputs.python-version }}
3232
cache: "pip"
3333

34-
- name: Cache dependencies
35-
id: cache-deps
36-
uses: actions/cache@v3
37-
with:
38-
path: |
39-
~/.cache/pip
40-
~/.cache/pypoetry
41-
.venv
42-
key: ${{ runner.os }}-python-${{ inputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
43-
restore-keys: |
44-
${{ runner.os }}-python-${{ inputs.python-version }}-
45-
${{ runner.os }}-python-
46-
4734
- name: Install Poetry
4835
run: |
4936
pip install poetry==1.8.5
@@ -52,7 +39,35 @@ jobs:
5239
- name: Install dependencies
5340
run: poetry install
5441

55-
- name: Set environment variables
42+
- name: Debug before copy
43+
run: |
44+
echo "Current directory contents:"
45+
ls -la
46+
echo "Virtual environment contents:"
47+
ls -la .venv || echo "No .venv directory"
48+
echo "Python executable path:"
49+
which python
50+
echo "Poetry env info:"
51+
poetry env info
52+
53+
- name: Create artifact directory
5654
run: |
57-
echo "VIRTUAL_ENV=$PWD/.venv" >> $GITHUB_ENV
58-
echo "$PWD/.venv/bin" >> $GITHUB_PATH
55+
mkdir -p artifact
56+
cp poetry.lock artifact/
57+
cp pyproject.toml artifact/
58+
if [ ! -d ".venv" ]; then
59+
echo "ERROR: .venv directory not found"
60+
exit 1
61+
fi
62+
tar czf artifact/venv.tar.gz .venv/
63+
echo "Artifact directory contents:"
64+
ls -la artifact/
65+
echo "Artifact sizes:"
66+
du -sh artifact/*
67+
68+
- name: Upload virtual environment
69+
uses: actions/upload-artifact@v4
70+
with:
71+
name: venv-${{ github.run_id }}
72+
path: artifact/
73+
if-no-files-found: error

0 commit comments

Comments
 (0)