Skip to content

Commit

Permalink
Rearrange CI methodology
Browse files Browse the repository at this point in the history
  • Loading branch information
bswck committed Nov 1, 2023
1 parent 26445ab commit 3f1c16f
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 20 deletions.
20 changes: 0 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,3 @@ repos:
- id: poetry-check
stages: [push]
name: Check Poetry environment
- id: poetry-lock
name: Lock Dependencies with Poetry
stages: [push]
- id: poetry-export
name: Export Dependencies with Poetry
args:
["--without=dev", "-f", "requirements.txt", "-o", "requirements.txt"]
stages: [push]
- id: poetry-export
name: Export Dev Dependencies with Poetry
args:
[
"--with=dev",
"--extras=all",
"-f",
"requirements.txt",
"-o",
"requirements-dev.txt",
]
stages: [push]
30 changes: 30 additions & 0 deletions install
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# (C) bswck
#
# Usage:
# $ ./install && poetry shell
poetry install --all-extras

if [ $? -eq 0 ]; then
echo "Successfully installed dependencies."
else
echo "Failed to install dependencies."
exit 1
fi

PYTHON_VERSION="$(cat .python-version)"

poetry env use $PYTHON_VERSION -q

if [ $? -eq 0 ]; then
echo "Successfully set Python version to $PYTHON_VERSION."
else
echo "Failed to set Python version to $PYTHON_VERSION."
exit 1
fi

DIR="$(dirname "$(readlink -f "$0")")"
CMD="export PATH=\$PATH:$DIR/scripts"
APPEND="\n# Add repo scripts to PATH for convenience.\n$CMD\n"
ACTIVATE="$(poetry env info -p)/bin/activate"

echo $APPEND >> $ACTIVATE
14 changes: 14 additions & 0 deletions scripts/lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# (C) bswck
# A manual alias for long poetry commands ✨
#
# Run this before pushing to ensure that package locks are up-to-date.
# We update requirements-dev.txt in order to make sure that the CI environment
# can install the dev dependencies without Poetry.
#
# Usage:
# $ lock
echo "✨ Doing the magic ✨"
poetry lock
poetry export --without=dev -f requirements.txt -o requirements.txt
poetry export --with=dev --extras=all -f requirements.txt -o requirements-dev.txt
113 changes: 113 additions & 0 deletions scripts/release
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python
# (C) bswck
#
# Run this script to release a new version of the package.
#
# Usage:
# $ release [major|minor|patch|<major>.<minor>.<patch>]
"""
Automate the release process by updating local files, creating and pushing a new tag.
The actual release is made via GitHub Actions.
"""

import argparse
import functools
import logging
import subprocess
import sys

_LOGGER = logging.getLogger("release")


if __name__ == "__main__":
_LOGGER.setLevel(logging.INFO)
(_LOGGER_HANDLER := logging.StreamHandler()).setFormatter(
logging.Formatter("%(levelname)s: %(message)s"),
)
_LOGGER.addHandler(_LOGGER_HANDLER)

cmd, shell = str.split, functools.partial(subprocess.run, check=True)

def abort(msg: str, /) -> None:
"""Display an error message and exit the script process with status code -1."""
_LOGGER.critical(msg)
sys.exit(-1)

parser = argparse.ArgumentParser(description="Release a semver version.")
parser.add_argument(
"version",
type=str,
nargs=1,
)
args: argparse.Namespace = parser.parse_args()
version: str = args.version.pop()

files_changed = shell(
cmd("git diff --name-only HEAD"),
capture_output=True,
).stdout.decode()

if files_changed:
msg = (
"There are uncommitted changes in the working tree in these files:\n"
f"{files_changed}\n"
"Continue? They will be included in the release commit. (y/n) [n]: "
)
continue_confirm = (input(msg).casefold().strip() or "n")[0]
if continue_confirm != "y":
abort("Uncommitted changes in the working tree.")

# If we get here, we should be good to go
# Let's do a final check for safety
msg = f"You are about to release {version!r} version. Are you sure? (y/n) [y]: "

release_confirm = ((input(msg).casefold().strip()) or "y")[0]

if release_confirm != "y":
abort(f"You said no when prompted to bump the {version!r} version.")

shell(cmd("poetry self add poetry-bumpversion@latest"))

_LOGGER.info("Bumping the %r version", version)

shell([*cmd("poetry version"), version])

new_version = "v" + (
shell(cmd("poetry version --short"), capture_output=True)
.stdout.decode()
.strip()
)

files_changed_for_release = shell(
cmd("git diff --name-only HEAD"),
capture_output=True,
).stdout.decode()

if files_changed_for_release:
shell(cmd("git diff"))
msg = (
"You are about to commit and push auto-changed files due "
"to version upgrade, see the diff view above. "
"Are you sure? (y/n) [y]: "
)
commit_confirm = ((input(msg).casefold().strip()) or "y")[0]

if commit_confirm == "y":
shell([*cmd("git commit -am"), f"Release {new_version}"])
shell(cmd("git push"))
else:
abort(
"Changes made uncommitted. "
"Commit your unrelated changes and try again.",
)

_LOGGER.info("Creating %s tag...", new_version)

try:
shell([*cmd("git tag -a"), new_version, "-m", f"Release {new_version}"])
except subprocess.CalledProcessError:
abort(f"Failed to create {new_version} tag, probably already exists.")
else:
_LOGGER.info("Pushing local tags...")
shell(cmd("git push --tags"))

0 comments on commit 3f1c16f

Please sign in to comment.