Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Release

# A pushed tag builds the plugin and attaches it to a draft release, which is
# published by hand. Every plugin repo uses this workflow; only env differs.
on:
push:
tags: ["v*", "[0-9]*"]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

permissions:
contents: write

env:
# setup.py imports octoprint_setuptools, so those builds need OctoPrint installed.
BUILD_ARGS: ""
# Extra copy of the plugin zip under an old name that installed versions still
# download on update; empty once they all use the tag's source archive.
LEGACY_ZIP: ""

jobs:
release:
name: Build & draft release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

- uses: actions/setup-python@v7
with:
python-version: "3.13"

- name: Install build tools
run: |
python -m pip install --upgrade pip build packaging setuptools wheel
python -m pip install "OctoPrint>=2,<3"

- name: Build sdist and wheel
run: |
# The variable holds several arguments; split it on purpose.
# shellcheck disable=SC2086
python -m build $BUILD_ARGS

- name: Check version and stage files
id: stage
env:
TAG: ${{ github.ref_name }}
REPO: ${{ github.event.repository.name }}
run: |
python - <<'PY'
import os
import shutil
import sys
import tarfile
import zipfile
from pathlib import Path, PurePosixPath

from packaging.version import InvalidVersion, Version


def fail(message):
print(f"::error::{message}")
sys.exit(1)


tag, repo, legacy = os.environ["TAG"], os.environ["REPO"], os.environ["LEGACY_ZIP"]
wheels, sdists = sorted(Path("dist").glob("*.whl")), sorted(Path("dist").glob("*.tar.gz"))
if len(wheels) != 1 or len(sdists) != 1:
fail(f"expected one wheel and one sdist, found {[p.name for p in wheels + sdists]}")

with zipfile.ZipFile(wheels[0]) as whl:
meta = next(n for n in whl.namelist() if n.endswith(".dist-info/METADATA"))
lines = whl.read(meta).decode("utf-8").splitlines()
version = next(line.split(":", 1)[1].strip() for line in lines if line.startswith("Version:"))

# v0.1.0-rc1 and 0.1.0rc1 name the same version.
try:
tag_version = Version(tag[1:] if tag[0] in "vV" else tag)
except InvalidVersion:
fail(f"tag {tag} is not a version")
if Version(version) != tag_version:
fail(f"tag {tag} does not match the package version {version}")

# The sdist as a zip: OctoPrint's plugin manager installs it from a URL.
out = Path("release")
out.mkdir()
plugin_zip = out / f"{repo}-{version}.zip"
with tarfile.open(sdists[0]) as tar, zipfile.ZipFile(plugin_zip, "w", zipfile.ZIP_DEFLATED) as zf:
for member in tar.getmembers():
path = PurePosixPath(member.name)
if member.isfile() and not path.is_absolute() and ".." not in path.parts:
info = zipfile.ZipInfo(str(path))
info.external_attr = (member.mode & 0o777) << 16
zf.writestr(info, tar.extractfile(member).read())
with zipfile.ZipFile(plugin_zip) as zf:
parts = [PurePosixPath(n).parts for n in zf.namelist()]
if not any(len(p) == 3 and p[1].startswith("octoprint_") and p[2] == "__init__.py" for p in parts):
fail(f"{plugin_zip.name} does not contain the plugin package")

for name in (f"{repo}-latest.zip", legacy):
if name:
shutil.copyfile(plugin_zip, out / name)
for path in wheels + sdists:
shutil.copy2(path, out)

# a, b, rc and dev versions are published as pre-releases.
prerelease = str(tag_version.is_prerelease).lower()
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh:
fh.write(f"prerelease={prerelease}\n")
print(f"{tag}: version {version}, pre-release {prerelease}")
for path in sorted(out.iterdir()):
print(f" {path.name}: {path.stat().st_size} bytes")
PY

- name: Create draft release
uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3
with:
draft: true
prerelease: ${{ steps.stage.outputs.prerelease }}
generate_release_notes: true
files: release/*
155 changes: 0 additions & 155 deletions .github/workflows/release.yml

This file was deleted.