|
| 1 | +name: Build, test n' release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + version: |
| 12 | + description: "Version to release (e.g. 0.5.4 or 0.6.0-beta). Leave empty to use rites/_version.py" |
| 13 | + required: false |
| 14 | + type: string |
| 15 | + |
| 16 | +jobs: |
| 17 | + test: |
| 18 | + runs-on: ${{ matrix.os }} |
| 19 | + strategy: |
| 20 | + fail-fast: false |
| 21 | + matrix: |
| 22 | + os: [ubuntu-latest, windows-latest, macos-latest] |
| 23 | + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] |
| 24 | + |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Set up Python ${{ matrix.python-version }} |
| 29 | + uses: actions/setup-python@v5 |
| 30 | + with: |
| 31 | + python-version: ${{ matrix.python-version }} |
| 32 | + |
| 33 | + - name: Install package |
| 34 | + run: | |
| 35 | + python -m pip install --upgrade pip |
| 36 | + pip install . |
| 37 | +
|
| 38 | + - name: Smoke test (import + run examples) |
| 39 | + run: python test.py |
| 40 | + |
| 41 | + build-and-release: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + needs: test |
| 44 | + if: github.ref == 'refs/heads/main' |
| 45 | + |
| 46 | + permissions: |
| 47 | + contents: write |
| 48 | + id-token: write |
| 49 | + |
| 50 | + steps: |
| 51 | + - name: Checkout repository |
| 52 | + uses: actions/checkout@v4 |
| 53 | + with: |
| 54 | + fetch-depth: 0 |
| 55 | + fetch-tags: true |
| 56 | + |
| 57 | + - name: Determine version |
| 58 | + id: check_version |
| 59 | + run: | |
| 60 | + SHOULD_RELEASE=false |
| 61 | + VERSION="" |
| 62 | +
|
| 63 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 64 | + INPUT_VERSION="${{ github.event.inputs.version }}" |
| 65 | + if [[ -n "$INPUT_VERSION" ]]; then |
| 66 | + VERSION="${INPUT_VERSION#v}" |
| 67 | + else |
| 68 | + VERSION=$(grep -E '^__version__' rites/_version.py | sed -E 's/.*"([^"]+)".*/\1/') |
| 69 | + fi |
| 70 | + SHOULD_RELEASE=true |
| 71 | + else |
| 72 | + COMMIT_MSG=$(git log -1 --pretty=%B) |
| 73 | + echo "Commit message: $COMMIT_MSG" |
| 74 | + if [[ $COMMIT_MSG =~ v([0-9]+\.[0-9]+\.[0-9]+([-+][a-zA-Z0-9.-]+)?) ]]; then |
| 75 | + VERSION="${BASH_REMATCH[1]}" |
| 76 | + SHOULD_RELEASE=true |
| 77 | + fi |
| 78 | + fi |
| 79 | +
|
| 80 | + if [[ "$SHOULD_RELEASE" == "true" ]]; then |
| 81 | + TAG="v${VERSION}" |
| 82 | + echo "Releasing version: $VERSION (tag: $TAG)" |
| 83 | + echo "SHOULD_RELEASE=true" >> $GITHUB_OUTPUT |
| 84 | + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT |
| 85 | + echo "TAG=$TAG" >> $GITHUB_OUTPUT |
| 86 | + if [[ "$VERSION" == *"-"* || "$VERSION" == *"+"* ]]; then |
| 87 | + echo "PRERELEASE=true" >> $GITHUB_OUTPUT |
| 88 | + else |
| 89 | + echo "PRERELEASE=false" >> $GITHUB_OUTPUT |
| 90 | + fi |
| 91 | + else |
| 92 | + echo "No release version found; skipping release." |
| 93 | + echo "SHOULD_RELEASE=false" >> $GITHUB_OUTPUT |
| 94 | + fi |
| 95 | +
|
| 96 | + - name: Set up Python |
| 97 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 98 | + uses: actions/setup-python@v5 |
| 99 | + with: |
| 100 | + python-version: "3.x" |
| 101 | + |
| 102 | + - name: Install build tooling |
| 103 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 104 | + run: | |
| 105 | + python -m pip install --upgrade pip |
| 106 | + python -m pip install build twine |
| 107 | +
|
| 108 | + - name: Sync version into rites/_version.py |
| 109 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 110 | + run: | |
| 111 | + python - <<'PY' |
| 112 | + import os, re |
| 113 | + v = os.environ["VERSION"] |
| 114 | + path = "rites/_version.py" |
| 115 | + with open(path, "r", encoding="utf-8") as f: |
| 116 | + content = f.read() |
| 117 | + new = re.sub(r'__version__\s*=\s*".*"', f'__version__ = "{v}"', content) |
| 118 | + with open(path, "w", encoding="utf-8") as f: |
| 119 | + f.write(new) |
| 120 | + print(f"Set version to {v}") |
| 121 | + PY |
| 122 | + env: |
| 123 | + VERSION: ${{ steps.check_version.outputs.VERSION }} |
| 124 | + |
| 125 | + - name: Build sdist and wheel |
| 126 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 127 | + run: python -m build |
| 128 | + env: |
| 129 | + RITES_PKG_VERSION: ${{ steps.check_version.outputs.VERSION }} |
| 130 | + |
| 131 | + - name: Get previous tag |
| 132 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 133 | + id: prev_tag |
| 134 | + run: | |
| 135 | + tag=$(git tag --sort=-creatordate | head -1 || true) |
| 136 | + echo "Most recent tag: $tag" |
| 137 | + echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 138 | +
|
| 139 | + - name: Get commit log |
| 140 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 141 | + id: commit_log |
| 142 | + run: | |
| 143 | + if [ -z "${{ steps.prev_tag.outputs.tag }}" ]; then |
| 144 | + echo "log=- First release" >> "$GITHUB_OUTPUT" |
| 145 | + else |
| 146 | + TEMP_LOG=$(mktemp) |
| 147 | + git log ${{ steps.prev_tag.outputs.tag }}..HEAD --pretty=format:"- %s (%an)" > "$TEMP_LOG" |
| 148 | + DELIMITER="COMMIT_LOG_DELIMITER_$(date +%s)" |
| 149 | + echo "log<<$DELIMITER" >> "$GITHUB_OUTPUT" |
| 150 | + cat "$TEMP_LOG" >> "$GITHUB_OUTPUT" |
| 151 | + echo "" >> "$GITHUB_OUTPUT" |
| 152 | + echo "$DELIMITER" >> "$GITHUB_OUTPUT" |
| 153 | + rm "$TEMP_LOG" |
| 154 | + fi |
| 155 | +
|
| 156 | + - name: Create tag |
| 157 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 158 | + run: | |
| 159 | + git config --local user.email "action@github.com" |
| 160 | + git config --local user.name "GitHub Action" |
| 161 | + if git rev-parse "${{ steps.check_version.outputs.TAG }}" >/dev/null 2>&1; then |
| 162 | + echo "Tag ${{ steps.check_version.outputs.TAG }} already exists; skipping tag creation." |
| 163 | + else |
| 164 | + git tag -a ${{ steps.check_version.outputs.TAG }} -m "Release ${{ steps.check_version.outputs.TAG }}" |
| 165 | + git push origin ${{ steps.check_version.outputs.TAG }} |
| 166 | + fi |
| 167 | +
|
| 168 | + - name: Create GitHub Release |
| 169 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 170 | + uses: softprops/action-gh-release@v2 |
| 171 | + with: |
| 172 | + tag_name: ${{ steps.check_version.outputs.TAG }} |
| 173 | + name: "rites ${{ steps.check_version.outputs.TAG }}" |
| 174 | + body: | |
| 175 | + ## rites ${{ steps.check_version.outputs.TAG }} |
| 176 | +
|
| 177 | + ### Changes |
| 178 | + ${{ steps.commit_log.outputs.log }} |
| 179 | +
|
| 180 | + ### Install |
| 181 | + ``` |
| 182 | + pip install rites==${{ steps.check_version.outputs.VERSION }} |
| 183 | + ``` |
| 184 | + draft: false |
| 185 | + prerelease: ${{ steps.check_version.outputs.PRERELEASE }} |
| 186 | + files: dist/* |
| 187 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 188 | + |
| 189 | + - name: Publish to PyPI |
| 190 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 191 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 192 | + |
| 193 | + - name: Upload build artifacts |
| 194 | + if: steps.check_version.outputs.SHOULD_RELEASE == 'true' |
| 195 | + uses: actions/upload-artifact@v4 |
| 196 | + with: |
| 197 | + name: dist |
| 198 | + path: dist/* |
0 commit comments