|
1 | 1 | name: publish-pypi |
2 | | -on: |
3 | | - release: |
4 | | - types: [published] |
5 | | - |
6 | | -permissions: |
7 | | - contents: read |
8 | | - id-token: write |
9 | 2 |
|
10 | | -env: |
11 | | - PIP_DISABLE_PIP_VERSION_CHECK: "1" |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["semantic-release"] |
| 6 | + types: [completed] |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + tag: |
| 10 | + description: "Tag to publish (e.g. v0.4.35)" |
| 11 | + required: false |
| 12 | + type: string |
12 | 13 |
|
13 | 14 | jobs: |
14 | | - pypi: |
15 | | - if: startsWith(github.event.release.tag_name, 'v') |
| 15 | + publish: |
| 16 | + if: > |
| 17 | + (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') || |
| 18 | + (github.event_name == 'workflow_dispatch') |
16 | 19 | runs-on: ubuntu-latest |
17 | 20 | environment: pypi |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + packages: write |
| 24 | + id-token: write |
18 | 25 | steps: |
19 | | - - uses: actions/checkout@v4 |
20 | | - - uses: actions/setup-python@v5 |
| 26 | + - name: Checkout with tags |
| 27 | + uses: actions/checkout@v4 |
21 | 28 | with: |
22 | | - python-version: '3.x' |
23 | | - - run: python -m pip install -U pip build |
24 | | - - name: Sync version from tag into pyproject.toml |
25 | | - env: |
26 | | - TAG: ${{ github.event.release.tag_name }} |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + - name: Resolve TAG |
| 32 | + id: tag |
| 33 | + shell: bash |
27 | 34 | run: | |
28 | | - VER="${TAG#v}" |
29 | | - export VER |
30 | | - python - <<'PY' |
31 | | -import os, re, pathlib |
32 | | -ver = os.environ["VER"] |
33 | | -p = pathlib.Path("pyproject.toml") |
34 | | -t = p.read_text(encoding="utf-8") |
35 | | -t = re.sub(r'(?m)^(\s*version\s*=\s*")\d+\.\d+\.\d+(")', rf'\1{ver}\2', t) |
36 | | -p.write_text(t, encoding="utf-8") |
37 | | -print("pyproject.toml version ->", ver) |
38 | | -PY |
39 | | - - run: python -m build |
40 | | - - uses: pypa/gh-action-pypi-publish@release/v1 |
| 35 | + if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" && -n "${{ github.event.inputs.tag }}" ]]; then |
| 36 | + TAG="${{ github.event.inputs.tag }}" |
| 37 | + else |
| 38 | + git fetch --tags --force |
| 39 | + TAG="$(git tag -l 'v*' --sort=-v:refname | head -n1)" |
| 40 | + fi |
| 41 | + echo "TAG=$TAG" | tee -a "$GITHUB_ENV" |
| 42 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 43 | +
|
| 44 | + - name: Setup Python |
| 45 | + uses: actions/setup-python@v5 |
| 46 | + with: |
| 47 | + python-version: '3.12' |
| 48 | + |
| 49 | + - name: Build sdist & wheel |
| 50 | + run: | |
| 51 | + python -m pip install -U pip build |
| 52 | + python -m build |
| 53 | +
|
| 54 | + - name: Publish to PyPI via OIDC |
| 55 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 56 | + with: |
| 57 | + packages-dir: dist |
| 58 | + skip-existing: true |
| 59 | + |
| 60 | + - name: Login to GHCR |
| 61 | + uses: docker/login-action@v3 |
| 62 | + with: |
| 63 | + registry: ghcr.io |
| 64 | + username: ${{ github.actor }} |
| 65 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + |
| 67 | + - name: Setup Buildx |
| 68 | + uses: docker/setup-buildx-action@v3 |
| 69 | + |
| 70 | + - name: Compute lowercase image tag |
| 71 | + id: img |
| 72 | + shell: bash |
| 73 | + run: | |
| 74 | + OWNER_LC="${GITHUB_REPOSITORY_OWNER,,}" |
| 75 | + REPO_LC="$(basename "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]')" |
| 76 | + echo "IMAGE_TAG=ghcr.io/${OWNER_LC}/${REPO_LC}:${TAG}" | tee -a "$GITHUB_ENV" |
| 77 | +
|
| 78 | + - name: Build & Push image |
| 79 | + uses: docker/build-push-action@v5 |
| 80 | + with: |
| 81 | + context: . |
| 82 | + push: true |
| 83 | + tags: ${{ env.IMAGE_TAG }} |
| 84 | + platforms: linux/amd64 |
0 commit comments