|
1 | 1 | name: publish-pypi |
2 | 2 |
|
3 | 3 | on: |
4 | | - workflow_run: |
5 | | - workflows: ["publish-on-semrel"] |
6 | | - types: [completed] |
7 | 4 | workflow_dispatch: |
8 | 5 | inputs: |
9 | 6 | tag: |
10 | | - description: Tag to publish (e.g. v0.4.36) |
| 7 | + description: "Tag a publicar (por defecto: último v*)" |
11 | 8 | required: false |
12 | | - type: string |
| 9 | + workflow_run: |
| 10 | + workflows: ["semantic-release"] |
| 11 | + types: [completed] |
13 | 12 |
|
14 | 13 | permissions: |
15 | | - contents: read |
16 | 14 | id-token: write |
| 15 | + contents: read |
17 | 16 | packages: write |
| 17 | + attestations: write |
18 | 18 |
|
19 | 19 | jobs: |
20 | 20 | 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' |
22 | 22 | environment: pypi |
23 | 23 | runs-on: ubuntu-latest |
24 | 24 | steps: |
25 | | - - name: Checkout with tags |
| 25 | + - name: Checkout con tags |
26 | 26 | uses: actions/checkout@v4 |
27 | | - with: |
28 | | - fetch-depth: 0 |
29 | | - fetch-tags: true |
| 27 | + with: { fetch-depth: 0 } |
30 | 28 |
|
31 | | - - name: Resolve TAG |
| 29 | + - name: Resolver TAG |
32 | 30 | id: tag |
33 | 31 | shell: bash |
34 | 32 | run: | |
35 | 33 | 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/}" |
37 | 37 | 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)" |
41 | 39 | 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}" |
59 | 42 |
|
60 | 43 | - name: Setup Python |
61 | 44 | uses: actions/setup-python@v5 |
62 | | - with: |
63 | | - python-version: '3.x' |
| 45 | + with: { python-version: '3.x' } |
64 | 46 |
|
65 | 47 | - name: Build sdist & wheel |
66 | 48 | run: | |
67 | 49 | python -m pip install -U pip build |
68 | 50 | python -m build |
69 | 51 |
|
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) |
71 | 66 | uses: pypa/gh-action-pypi-publish@release/v1 |
72 | 67 | 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