diff --git a/.github/actions/test/action.yml b/.github/actions/test/action.yml new file mode 100644 index 0000000..2fb922f --- /dev/null +++ b/.github/actions/test/action.yml @@ -0,0 +1,24 @@ +name: Test +description: Lint, typecheck, test and check formatting + +runs: + using: composite + steps: + - uses: astral-sh/setup-uv@v6 + with: + enable-cache: true + + - name: Lint, typecheck, test + run: make check + shell: bash + + - name: Test summary + if: ${{ !cancelled() && hashFiles('pytest.xml') != '' }} + uses: test-summary/action@v2 + with: + paths: pytest.xml + show: fail, skip + + - name: Check format + run: make check-format + shell: bash diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 63d1d76..1b0dbbb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,73 +1,50 @@ name: CI on: - push: - branches: [main] pull_request: - workflow_dispatch: + workflow_call: + inputs: + ref: + description: Git ref to check out, defaults to the triggering ref + type: string + default: "" + outputs: + coverage: + value: ${{ jobs.test.outputs.coverage }} concurrency: - group: ci-${{ github.ref }} + # When called from another workflow, `github.workflow` is the caller's name, + # keeping runs called from different workflows on the same ref from cancelling each other. + group: ci-${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true +permissions: + contents: read + jobs: - check: - name: Check + test: + name: Test and coverage runs-on: ubuntu-latest - permissions: - contents: write # publishes the coverage badge to the `badges` branch + outputs: + coverage: ${{ steps.coverage.outputs.coverage }} steps: - uses: actions/checkout@v5 - - - uses: astral-sh/setup-uv@v6 with: - enable-cache: true + ref: ${{ inputs.ref }} - - name: Lint, typecheck, test - run: make check - - - name: Test summary - if: ${{ !cancelled() && hashFiles('pytest.xml') != '' }} - uses: test-summary/action@v2 - with: - paths: pytest.xml - show: fail, skip + - uses: ./.github/actions/test - name: Coverage summary + id: coverage if: ${{ !cancelled() && hashFiles('.coverage') != '' }} run: | uv run coverage report --format=markdown >> "$GITHUB_STEP_SUMMARY" + echo "coverage=$(uv run coverage report --format=total)" >> "$GITHUB_OUTPUT" uv run coverage html - - name: Upload coverage HTML report - if: ${{ !cancelled() && hashFiles('.coverage') != '' }} + - name: Upload coverage + if: ${{ !cancelled() && hashFiles('htmlcov/**') != '' }} uses: actions/upload-artifact@v4 with: name: coverage-html path: htmlcov/ - - - name: Coverage badge JSON - if: ${{ !cancelled() && github.event_name == 'push' && github.ref == 'refs/heads/main' && hashFiles('.coverage') != '' }} - run: | - pct=$(uv run coverage report --format=total) - if [ "$pct" -ge 90 ]; then color=brightgreen - elif [ "$pct" -ge 75 ]; then color=green - elif [ "$pct" -ge 60 ]; then color=yellow - else color=red - fi - mkdir -p badge - printf '{"schemaVersion":1,"label":"coverage","message":"%s%%","color":"%s"}\n' \ - "$pct" "$color" > badge/coverage.json - - - name: Publish coverage badge - if: ${{ !cancelled() && github.event_name == 'push' && github.ref == 'refs/heads/main' && hashFiles('badge/coverage.json') != '' }} - uses: peaceiris/actions-gh-pages@v4 - with: - github_token: ${{ github.token }} - publish_branch: badges - publish_dir: badge - force_orphan: true - - - name: Format - if: ${{ !cancelled() }} - run: make check-format diff --git a/.github/workflows/ci_main.yml b/.github/workflows/ci_main.yml new file mode 100644 index 0000000..e49ed5e --- /dev/null +++ b/.github/workflows/ci_main.yml @@ -0,0 +1,44 @@ +name: CI [main branch] + +on: + push: + branches: [main] + +concurrency: + group: ci-main-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + ci: + uses: ./.github/workflows/ci.yml + + publish-coverage: + name: Publish coverage + runs-on: ubuntu-latest + needs: ci + permissions: + contents: write # publishes the coverage badge to the `badges` branch + steps: + - name: Coverage badge JSON + run: | + pct="${{ needs.ci.outputs.coverage }}" + if [ "$pct" -ge 90 ]; then color=brightgreen + elif [ "$pct" -ge 75 ]; then color=green + elif [ "$pct" -ge 60 ]; then color=yellow + else color=red + fi + mkdir -p badge + printf '{"schemaVersion":1,"label":"coverage","message":"%s%%","color":"%s"}\n' \ + "$pct" "$color" > badge/coverage.json + + - name: Publish coverage badge + uses: peaceiris/actions-gh-pages@84c30a85c19949d7eee79c4ff27748b70285e453 # v4.1.0 + with: + github_token: ${{ github.token }} + publish_branch: badges + publish_dir: badge + force_orphan: true + diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..37be628 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,103 @@ +name: Publish the new version + +on: + workflow_call: + inputs: + version: + description: Which version to release + type: string + required: true + skip_tests: + description: Skip the tests, for callers that already ran them on this version + type: boolean + default: false + + workflow_dispatch: + inputs: + version: + description: Which version to release + type: string + required: true + +concurrency: + group: publish + cancel-in-progress: false + +permissions: + contents: read + +env: + VERSION_TAG: v${{ inputs.version }} + VERSION_REF: refs/tags/v${{ inputs.version }} + +jobs: + check: + name: Check the version release + runs-on: ubuntu-latest + steps: + # Fails unless the version exists as a tag. + - uses: actions/checkout@v5 + with: + ref: ${{ env.VERSION_REF }} + + # TODO: Make sure the tag is on main + + - if: ${{ !inputs.skip_tests }} + uses: ./.github/actions/test + + build: + name: Build the release + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ env.VERSION_REF }} + + - uses: astral-sh/setup-uv@v6 + + - run: uv build + + - uses: actions/upload-artifact@v4 + with: + name: dist + path: dist/ + + pypi: + name: PyPI + needs: build + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write # PyPI trusted publishing + steps: + - uses: actions/download-artifact@v4 + with: + name: dist + path: dist/ + + - uses: astral-sh/setup-uv@v6 + + - run: uv publish + + github-release: + name: GitHub release + needs: build + runs-on: ubuntu-latest + permissions: + contents: write # create the release + steps: + - uses: actions/download-artifact@v4 + with: + name: dist + path: dist/ + + - name: Create release + env: + GH_TOKEN: ${{ github.token }} + run: | + gh release create "${{ env.VERSION_TAG }}" \ + --repo "${{ github.repository }}" \ + --verify-tag \ + --generate-notes \ + dist/* diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..ff1ac8f --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,78 @@ +name: Release a new version + +on: + workflow_dispatch: + inputs: + bump: + description: Version component to bump + type: choice + options: [patch, minor, major] + publish: + description: Also publish the new version + type: boolean + default: false + +concurrency: + group: release + cancel-in-progress: false + +permissions: + contents: read + +jobs: + check: + name: Check the branch and tests + runs-on: ubuntu-latest + steps: + - if: github.ref != 'refs/heads/main' + run: | + echo "::error::This workflow must be run from main, not ${{ github.ref_name }}" + exit 1 + + - uses: actions/checkout@v5 + + - uses: ./.github/actions/test + + bump: + name: Bump version + needs: check + runs-on: ubuntu-latest + permissions: + contents: write # push the release commit and tag + outputs: + version: ${{ steps.version.outputs.version }} + tag: ${{ steps.version.outputs.tag }} + steps: + - uses: actions/checkout@v5 + + - uses: astral-sh/setup-uv@v6 + + - name: Bump version + id: version + run: | + uv version --bump ${{ inputs.bump }} + version="$(uv version --short)" + echo "version=$version" >> "$GITHUB_OUTPUT" + echo "tag=v$version" >> "$GITHUB_OUTPUT" + + - name: Commit and tag + run: | + git config user.name "github-actions[bot]" + git config user.email "41898283+github-actions[bot]@users.noreply.github.com" + git commit -am "Bump version to ${{ steps.version.outputs.tag }}" + git tag "${{ steps.version.outputs.tag }}" + + - name: Push + run: git push --atomic origin HEAD:main "${{ steps.version.outputs.tag }}" + + publish: + needs: bump + if: inputs.publish + uses: ./.github/workflows/publish.yml + permissions: + contents: write + id-token: write + with: + version: ${{ needs.bump.outputs.version }} + skip_tests: true + diff --git a/foobar b/foobar new file mode 100644 index 0000000..323fae0 --- /dev/null +++ b/foobar @@ -0,0 +1 @@ +foobar diff --git a/pyproject.toml b/pyproject.toml index 2890b05..0fae2ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,15 +1,25 @@ [project] name = "bench" version = "0.1.0" -description = "A lightweight Python benchmarking framework" -requires-python = ">=3.12" +description = "A Python benchmarking framework and command-line tool" +maintainers = [ + { name = "Filip Říha", email = "riha.fina@gmail.com" }, + { name = "Filip Křikava", email = "krikava@gmail.com" }, +] + +readme = "README.md" license = "MIT" license-files = ["LICENSE"] + +requires-python = ">=3.12" dependencies = [ "cattrs>=26.1.0", "rich", ] +[project.urls] +Homepage = "https://github.com/PRL-PRG/bench" + # ---------- # Build # ---------- @@ -52,6 +62,7 @@ addopts = ["--cov", "--cov-report=term-missing:skip-covered", "--junitxml=pytest [tool.coverage.run] source = ["bench"] branch = true +relative_files = true # CLI and example tests spawn `python -m bench`; without this they contribute nothing. patch = ["subprocess"]