Skip to content

Commit 68d1afa

Browse files
ci(release): enable manual/semrel publish; verify pyproject==tag; PyPI OIDC + GHCR (#82)
1 parent e6b5f9d commit 68d1afa

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

.github/workflows/publish-pypi.yml

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,88 @@
11
name: publish-pypi
22

33
on:
4-
workflow_run:
5-
workflows: ["publish-on-semrel"]
6-
types: [completed]
74
workflow_dispatch:
85
inputs:
96
tag:
10-
description: Tag to publish (e.g. v0.4.36)
7+
description: "Tag a publicar (por defecto: último v*)"
118
required: false
12-
type: string
9+
workflow_run:
10+
workflows: ["semantic-release"]
11+
types: [completed]
1312

1413
permissions:
15-
contents: read
1614
id-token: write
15+
contents: read
1716
packages: write
17+
attestations: write
1818

1919
jobs:
2020
publish:
21-
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success') }}
21+
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
2222
environment: pypi
2323
runs-on: ubuntu-latest
2424
steps:
25-
- name: Checkout with tags
25+
- name: Checkout con tags
2626
uses: actions/checkout@v4
27-
with:
28-
fetch-depth: 0
29-
fetch-tags: true
27+
with: { fetch-depth: 0 }
3028

31-
- name: Resolve TAG
29+
- name: Resolver TAG
3230
id: tag
3331
shell: bash
3432
run: |
3533
if [[ -n "${{ github.event.inputs.tag }}" ]]; then
36-
echo "TAG=${{ github.event.inputs.tag }}" >> $GITHUB_ENV
34+
TAG="${{ github.event.inputs.tag }}"
35+
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
36+
TAG="${GITHUB_REF#refs/tags/}"
3737
else
38-
git fetch --tags --force
39-
TAG=$(git describe --tags --abbrev=0)
40-
echo "TAG=$TAG" >> $GITHUB_ENV
38+
TAG="$(git tag -l 'v*' --sort=-v:refname | head -n1)"
4139
fi
42-
echo "Resolved TAG=$TAG"
43-
44-
- name: Sync version from TAG into pyproject.toml
45-
shell: bash
46-
env:
47-
TAG: ${{ env.TAG }}
48-
run: |
49-
VER="${TAG#v}"
50-
python - <<'PY'
51-
import os,re,pathlib
52-
ver=os.environ["VER"]
53-
p=pathlib.Path("pyproject.toml")
54-
t=p.read_text()
55-
t=re.sub(r'(?m)^(\s*version\s*=\s*")\d+\.\d+\.\d+(")', rf'\1{ver}\2', t)
56-
p.write_text(t)
57-
print("synced version:", ver)
58-
PY
40+
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
41+
echo "TAG=${TAG}"
5942
6043
- name: Setup Python
6144
uses: actions/setup-python@v5
62-
with:
63-
python-version: '3.x'
45+
with: { python-version: '3.x' }
6446

6547
- name: Build sdist & wheel
6648
run: |
6749
python -m pip install -U pip build
6850
python -m build
6951
70-
- name: Publish to PyPI via OIDC
52+
- name: Verificar version == tag
53+
shell: bash
54+
run: |
55+
WANT="${{ steps.tag.outputs.tag#v }}"
56+
ACTUAL=$(python - <<'PY'
57+
import re, pathlib
58+
t=pathlib.Path("pyproject.toml").read_text()
59+
print(re.search(r'(?m)^\s*version\s*=\s*"([0-9.]+)"', t).group(1))
60+
PY
61+
)
62+
echo "want=$WANT actual=$ACTUAL"
63+
test "$WANT" = "$ACTUAL"
64+
65+
- name: Publicar en PyPI (OIDC)
7166
uses: pypa/gh-action-pypi-publish@release/v1
7267
with:
73-
packages-dir: dist
74-
verbose: true
68+
skip-existing: true
69+
70+
- name: Login GHCR
71+
uses: docker/login-action@v3
72+
with:
73+
registry: ghcr.io
74+
username: ${{ github.actor }}
75+
password: ${{ secrets.GITHUB_TOKEN }}
76+
77+
- name: Setup Buildx
78+
uses: docker/setup-buildx-action@v3
79+
80+
- name: Build & Push image
81+
shell: bash
82+
run: |
83+
REPO="${GITHUB_REPOSITORY,,}"
84+
TAG="${{ steps.tag.outputs.tag }}"
85+
docker buildx build --platform linux/amd64,linux/arm64 \
86+
-t ghcr.io/$REPO:$TAG \
87+
-t ghcr.io/$REPO:latest \
88+
--push .

0 commit comments

Comments
 (0)