|
| 1 | +name: publish-on-semrel |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ["semantic-release"] |
| 5 | + types: [completed] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + tag: |
| 9 | + description: "Tag a publicar (p.ej. v0.4.35). Vacío = último release" |
| 10 | + required: false |
| 11 | + default: "" |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + packages: write |
| 15 | + id-token: write |
| 16 | +concurrency: |
| 17 | + group: publish-${{ github.event_name }}-${{ github.run_id }} |
| 18 | + cancel-in-progress: false |
| 19 | +jobs: |
| 20 | + publish: |
| 21 | + if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success') }} |
| 22 | + runs-on: ubuntu-latest |
| 23 | + environment: pypi |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + # Obtener tag del último release |
| 30 | + - uses: actions/github-script@v7 |
| 31 | + id: latest |
| 32 | + with: |
| 33 | + script: | |
| 34 | + const { data } = await github.repos.getLatestRelease({ owner: context.repo.owner, repo: context.repo.repo }); |
| 35 | + core.setOutput('tag', data.tag_name); |
| 36 | +
|
| 37 | + - name: Resolver TAG de publicación |
| 38 | + id: tag |
| 39 | + shell: bash |
| 40 | + run: | |
| 41 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]] && [[ -n "${{ github.event.inputs.tag }}" ]]; then |
| 42 | + TAG="${{ github.event.inputs.tag }}" |
| 43 | + else |
| 44 | + TAG="${{ steps.latest.outputs.tag }}" |
| 45 | + fi |
| 46 | + echo "TAG=$TAG" >> "$GITHUB_ENV" |
| 47 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 48 | + echo "Publicando tag: $TAG" |
| 49 | +
|
| 50 | + - name: Sincronizar version de pyproject.toml con TAG |
| 51 | + shell: bash |
| 52 | + run: | |
| 53 | + python - <<'PY' |
| 54 | + import os, re, pathlib |
| 55 | + tag = os.environ["TAG"].lstrip('v') |
| 56 | + p = pathlib.Path("pyproject.toml") |
| 57 | + t = p.read_text() |
| 58 | + t = re.sub(r'(?m)^(\s*version\s*=\s*")\d+\.\d+\.\d+(")', rf"\1{tag}\2", t) |
| 59 | + p.write_text(t) |
| 60 | + print("Set version ->", tag) |
| 61 | + PY |
| 62 | +
|
| 63 | + # Build & push GHCR |
| 64 | + - name: Set IMAGE |
| 65 | + run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" |
| 66 | + |
| 67 | + - uses: docker/setup-qemu-action@v3 |
| 68 | + - uses: docker/setup-buildx-action@v3 |
| 69 | + - uses: docker/login-action@v3 |
| 70 | + with: |
| 71 | + registry: ghcr.io |
| 72 | + username: ${{ github.actor }} |
| 73 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 74 | + |
| 75 | + - name: Build & Push container |
| 76 | + uses: docker/build-push-action@v6 |
| 77 | + with: |
| 78 | + context: . |
| 79 | + file: ./Dockerfile |
| 80 | + platforms: linux/amd64 |
| 81 | + push: true |
| 82 | + provenance: false |
| 83 | + tags: ${{ env.IMAGE }}:${{ steps.tag.outputs.tag }},${{ env.IMAGE }}:latest |
| 84 | + |
| 85 | + # Build & publish PyPI (OIDC) |
| 86 | + - uses: actions/setup-python@v5 |
| 87 | + with: |
| 88 | + python-version: "3.12" |
| 89 | + |
| 90 | + - name: Build sdist/wheel |
| 91 | + run: | |
| 92 | + python -m pip install -U pip build |
| 93 | + python -m build |
| 94 | +
|
| 95 | + - name: Publish to PyPI (Trusted Publisher) |
| 96 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 97 | + with: |
| 98 | + skip-existing: true |
| 99 | + verbose: true |
| 100 | + env: |
| 101 | + PYTHON_KEYRING_BACKEND: keyring.backends.null.Keyring |
0 commit comments