Skip to content

Commit

Permalink
Merge pull request #142 from ai-cfia/141-add-optional-checks-in-pytho…
Browse files Browse the repository at this point in the history
…n-lint-if-the-version-in-pyprojecttoml-file-has-been-bumped-fails-pipeline-if-not-bumped

Issue #141: adds jobs for validating version bump
  • Loading branch information
SonOfLope authored Oct 3, 2024
2 parents dab5959 + eb94acd commit 841c548
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
19 changes: 11 additions & 8 deletions .github/workflows/workflow-lint-test-python.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
name: Reusable lint and test workflow for Python projects

Check warning on line 1 in .github/workflows/workflow-lint-test-python.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

1:1 [document-start] missing document start "---"

on:
workflow_call:
inputs:
skip-coverage:
required: false
type: string
default: 'false'

permissions:
actions: read
Expand Down Expand Up @@ -31,21 +35,20 @@ jobs:
- name: Lint with ruff
run: |
# stop the build if there are Python syntax errors or undefined
# names
ruff --output-format=github --select=E9,F63,F7,F82 \
--target-version=py311 .
# stop the build if there are Python syntax errors or undefined names
ruff --output-format=github --select=E9,F63,F7,F82 --target-version=py311 .
# default set of ruff rules with GitHub Annotations
ruff --output-format=github --target-version=py311 .
- uses: oNaiPs/secrets-to-env-action@v1
with:
secrets: ${{ toJSON(secrets) }}

- name: Test with unittest
- name: Test with pytest
run: |
python -m unittest discover -s tests
python -m pytest
- name: Run tests with coverage (coverage must be at least 80% to pass)
if: inputs.skip-coverage != 'true'
run: |
pytest --cov=. --cov-fail-under=80 tests/ # default value, coverage must be at least 80%
python -m pytest --cov=. --cov-fail-under=80
14 changes: 14 additions & 0 deletions .github/workflows/workflow-version-bump-python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Reusable version bump check validation workflow for Python projects

- **Purpose:** Validate the version bump in a Python project.
- **Usage:** Call this workflow in your Python projects.

```yaml
lint-test:
uses:
ai-cfia/github-workflows/.github/workflows/workflow-version-bump-python.yml@main
secrets: inherit
with:
pyproject-path: 'pyproject.toml'
package-name: 'my-package'
```
49 changes: 49 additions & 0 deletions .github/workflows/workflow-version-bump-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Reusable version bump check validation workflow for Python projects

Check warning on line 1 in .github/workflows/workflow-version-bump-python.yml

View workflow job for this annotation

GitHub Actions / yaml-lint-check

1:1 [document-start] missing document start "---"


on:
workflow_call:
inputs:
pyproject-path:
required: false
type: string
package-name:
required: false
type: string

jobs:
version-bump-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-tags: true
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Parse new version from pyproject.toml
id: parse_new_version
run: |
new_version=$(grep -Po '(?<=^version = ")[^"]*' ${{ inputs.pyproject-path }})
echo "new_version=$new_version" >> $GITHUB_ENV
- name: Get latest version
id: get_latest_version
run: |
package_name="${{ inputs.package-name }}"
latest_tag=$(git tag --list "v*-*${package_name}" --sort=-creatordate | head -n 1)
latest_version=$(echo $latest_tag | grep -oP '(?<=v)[0-9]+\.[0-9]+\.[0-9]+')
echo "latest_version=$latest_version" >> $GITHUB_ENV
- name: Check version bump
run: |
if [ "${{ env.new_version }}" == "${{ env.latest_version }}" ]; then
echo "Version has not been bumped!"
exit 1
else
echo "Version has been bumped."
fi

0 comments on commit 841c548

Please sign in to comment.