Skip to content

Commit

Permalink
Remove release on test PyPI, OIDC auth, fix release tag (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
multani authored Sep 3, 2023
1 parent 782a094 commit 74915ea
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
49 changes: 49 additions & 0 deletions .github/set-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3


import argparse
import io
import os

GIT_REF_PREFIX = "refs/tags/"


def github_ref_type(value: str) -> str:
if not value.startswith(GIT_REF_PREFIX):
raise ValueError("Invalid GITHUB_REF format, expects `refs/tags/<tag name>`")

return value


def write_about_file(file: io.TextIOWrapper, git_ref: str) -> None:
tag = git_ref[len(GIT_REF_PREFIX) :]
value = f'__version__ = "{tag}"'
print(value, file=file)
return tag


def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"GITHUB_REF",
default=os.environ.get("GITHUB_REF"),
type=github_ref_type,
help=(
'The Git reference, like "refs/tags/<tag name>". '
"Read automatically the GITHUB_REF environment variable, if possible."
),
)
parser.add_argument(
"-f",
"--filename",
default="structlog_gcp/__about__.py",
type=argparse.FileType("w"),
help="The file to write the version into.",
)
args = parser.parse_args()

write_about_file(args.filename, args.GITHUB_REF)


if __name__ == "__main__":
main()
46 changes: 12 additions & 34 deletions .github/workflows/build-test-release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build & maybe upload PyPI package
name: Build & test

on:
push:
Expand All @@ -9,15 +9,8 @@ on:
branches:
- main

release:
types:
- published

workflow_dispatch:

permissions:
contents: read

jobs:
build:
name: Build & verify package
Expand All @@ -35,6 +28,10 @@ jobs:
- name: Setup Hatch
run: pipx install hatch

- name: Configure version
if: github.event.action == 'published'
run: ./.github/set-version

- name: Test
run: hatch run pytest --color=yes

Expand All @@ -59,32 +56,15 @@ jobs:
name: Packages
path: dist/*

# Upload to Test PyPI on every commit on main.
release-test-pypi:
name: Publish in-dev package to test.pypi.org
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
release:
name: Publish
runs-on: ubuntu-latest
needs: build

steps:
- name: Download package
uses: actions/download-artifact@v3
with:
name: Packages
path: dist

- name: Upload to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository-url: https://test.pypi.org/legacy/

# Upload to real PyPI on GitHub Releases.
release-pypi:
name: Publish released package to pypi.org
if: github.event.action == 'published'
runs-on: ubuntu-latest
needs: build
environment: pypi-release

permissions:
id-token: write

steps:
- name: Download package
Expand All @@ -93,7 +73,5 @@ jobs:
name: Packages
path: dist

- name: Upload package to PyPI
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
6 changes: 6 additions & 0 deletions DEVELOPERS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# For Developers

## How to release?

* Create a new GitHub Release
* Publish the release
2 changes: 1 addition & 1 deletion structlog_gcp/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.1"
__version__ = "0.0.1.dev" # Will be overwritten during deployment

0 comments on commit 74915ea

Please sign in to comment.