From 66163a3d4aeeaa4e7ca4235fe86d4147af7392b9 Mon Sep 17 00:00:00 2001 From: itvi-1234 Date: Tue, 17 Nov 2026 19:14:10 +0530 Subject: [PATCH] ci: build deb and rpm packages and a Homebrew formula, and announce releases Add system packages for the Java profiler, as the TypeScript profiler has them. scripts/release-package.py builds, from the jar of a release, the jar, rc-profiler-java_.tar.gz (the launcher and the jar, which the Homebrew formula installs), and SHA256SUMS; and for a stable tag with --system-packages also a deb and an rpm for amd64 and arm64, and the formula. The deb and rpm carry a Java runtime made with jlink from the Temurin JDK 21.0.13 pinned by checksum in distribution/jdk.lock.json, holding only the modules the jar needs (jdeps, plus jdk.compiler), so they need nothing else installed. The launcher finds the jar and that runtime through ../lib/rc-profiler-java. The formula depends on openjdk@21 and sets JAVA_HOME to it unless the user has set one. The release workflow builds these for a stable tag, installs the packages in clean Ubuntu, Debian and Rocky Linux containers, and publishes SHA256SUMS in place of checksums.txt, because the Homebrew and package repositories look for that name. A prerelease still gets the jar and the archive. After a stable release it dispatches rc-profiler-java-release-published to homebrew-tools and packages, as the TypeScript profiler does; that needs the package-distribution environment and a HOMEBREW_REPO_DISPATCH_TOKEN secret, and skips with a notice without one. test.yml gets a packages job that builds and installs the packages on every pull request, so a change to the packaging is checked before a release. The helpers have 19 unit tests. Not verified locally: building the real packages downloads and runs the pinned JDK's jlink and jdeps, which this change's author could not do, so the first real run of the packages job is the check. --- .github/workflows/release.yml | 95 +++++++-- .github/workflows/test.yml | 35 ++++ .gitignore | 1 + distribution/homebrew/rc-profiler-java.rb | 22 ++ distribution/jdk.lock.json | 17 ++ distribution/nfpm.yaml | 17 ++ scripts/check-packages.sh | 48 +++++ scripts/release-package.py | 237 ++++++++++++++++++++++ scripts/test_release_package.py | 226 +++++++++++++++++++++ 9 files changed, 685 insertions(+), 13 deletions(-) create mode 100644 distribution/homebrew/rc-profiler-java.rb create mode 100644 distribution/jdk.lock.json create mode 100644 distribution/nfpm.yaml create mode 100755 scripts/check-packages.sh create mode 100755 scripts/release-package.py create mode 100644 scripts/test_release_package.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6f51a39..a7d38e3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,6 +23,8 @@ jobs: build: needs: test runs-on: ubuntu-24.04 + outputs: + stable: ${{ steps.kind.outputs.stable }} steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: @@ -33,11 +35,18 @@ jobs: java-version: '21' cache: maven - name: Check the release version + id: kind run: | if [[ ! "$RC_PROFILER_VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then echo 'release tags must have the form vMAJOR.MINOR.PATCH, optionally with a prerelease suffix' >&2 exit 1 fi + # Only a stable release gets system packages and is announced to Homebrew, apt and yum. + if [[ "$RC_PROFILER_VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "stable=true" >> "$GITHUB_OUTPUT" + else + echo "stable=false" >> "$GITHUB_OUTPUT" + fi - name: Build and test the jar at the release version run: | version="${RC_PROFILER_VERSION#v}" @@ -50,22 +59,37 @@ jobs: test -s "$jar" test "$(java -jar "$jar" --version)" = "$version" java -jar "$jar" --help - - name: Package the jar and the launcher + - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0 + if: steps.kind.outputs.stable == 'true' + with: + go-version: '1.26.5' + cache: false + - name: Install nfpm + if: steps.kind.outputs.stable == 'true' + env: + GOBIN: ${{ runner.temp }}/release-tools + run: | + go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.48.0 + echo "$GOBIN" >> "$GITHUB_PATH" + - name: Test the packaging helpers + run: python3 -m unittest scripts/test_release_package.py + - name: Build the release files run: | version="${RC_PROFILER_VERSION#v}" - assets="$RUNNER_TEMP/release-assets" - package="$RUNNER_TEMP/package/rc-profiler-java_$version" - mkdir -p "$assets" "$package" - cp "target/runtimeconditions-java-profiler-$version.jar" "$assets/runtimeconditions-java-profiler-$version.jar" - cp "target/runtimeconditions-java-profiler-$version.jar" "$package/runtimeconditions-java-profiler.jar" - cp distribution/rc-profiler-java README.md LICENSE "$package/" - tar -C "$RUNNER_TEMP/package" -czf "$assets/rc-profiler-java_$version.tar.gz" "rc-profiler-java_$version" + flags=() + if [[ "${{ steps.kind.outputs.stable }}" == true ]]; then flags+=(--system-packages); fi + python3 scripts/release-package.py --version "$RC_PROFILER_VERSION" \ + --jar "target/runtimeconditions-java-profiler-$version.jar" \ + --output "$RUNNER_TEMP/release-assets" "${flags[@]}" - name: Check that the archive runs run: | version="${RC_PROFILER_VERSION#v}" mkdir -p "$RUNNER_TEMP/unpacked" tar -C "$RUNNER_TEMP/unpacked" -xzf "$RUNNER_TEMP/release-assets/rc-profiler-java_$version.tar.gz" test "$("$RUNNER_TEMP/unpacked/rc-profiler-java_$version/rc-profiler-java" --version)" = "$version" + - name: Install the system packages in clean Ubuntu, Debian and Rocky Linux containers + if: steps.kind.outputs.stable == 'true' + run: scripts/check-packages.sh "$RUNNER_TEMP/release-assets" - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: release-assets @@ -84,14 +108,13 @@ jobs: with: name: release-assets path: release-assets - - name: Verify the assets and create checksums + - name: Verify the assets against their checksums working-directory: release-assets run: | version="${RC_PROFILER_VERSION#v}" test -s "runtimeconditions-java-profiler-$version.jar" test -s "rc-profiler-java_$version.tar.gz" - sha256sum *.jar *.tar.gz > checksums.txt - sha256sum --check checksums.txt + sha256sum --check SHA256SUMS - name: Publish the tagged release working-directory: release-assets env: @@ -108,10 +131,56 @@ jobs: echo 'refusing to replace an already published release' >&2 exit 1 fi - gh release upload "$RC_PROFILER_VERSION" *.jar *.tar.gz checksums.txt --repo "$GITHUB_REPOSITORY" --clobber + gh release upload "$RC_PROFILER_VERSION" * --repo "$GITHUB_REPOSITORY" --clobber else - gh release create "$RC_PROFILER_VERSION" *.jar *.tar.gz checksums.txt \ + gh release create "$RC_PROFILER_VERSION" * \ --repo "$GITHUB_REPOSITORY" --verify-tag --draft --generate-notes --title "$RC_PROFILER_VERSION" fi gh release edit "$RC_PROFILER_VERSION" --repo "$GITHUB_REPOSITORY" \ --draft=false --prerelease="$prerelease" --latest="$latest" + + notify-homebrew: + needs: [build, publish] + if: needs.build.outputs.stable == 'true' + runs-on: ubuntu-24.04 + environment: package-distribution + permissions: + contents: none + steps: + - name: Check that the dispatch token is configured + id: token + env: + TOKEN: ${{ secrets.HOMEBREW_REPO_DISPATCH_TOKEN }} + run: | + if [ -n "$TOKEN" ]; then echo "present=true" >> "$GITHUB_OUTPUT"; else echo "::notice::HOMEBREW_REPO_DISPATCH_TOKEN is not set, so the Homebrew formula is not updated"; fi + - name: Trigger the Homebrew formula update + if: steps.token.outputs.present == 'true' + uses: peter-evans/repository-dispatch@28959ce8df70de7be546dd1250a005dd32156697 # v4.0.1 + with: + token: ${{ secrets.HOMEBREW_REPO_DISPATCH_TOKEN }} + repository: runtimeconditions/homebrew-tools + event-type: rc-profiler-java-release-published + client-payload: '{"tag":"${{ github.ref_name }}"}' + + notify-linux-packages: + needs: [build, publish] + if: needs.build.outputs.stable == 'true' + runs-on: ubuntu-24.04 + environment: package-distribution + permissions: + contents: none + steps: + - name: Check that the dispatch token is configured + id: token + env: + TOKEN: ${{ secrets.HOMEBREW_REPO_DISPATCH_TOKEN }} + run: | + if [ -n "$TOKEN" ]; then echo "present=true" >> "$GITHUB_OUTPUT"; else echo "::notice::HOMEBREW_REPO_DISPATCH_TOKEN is not set, so the apt and yum repositories are not updated"; fi + - name: Trigger the apt and yum repository publication + if: steps.token.outputs.present == 'true' + uses: peter-evans/repository-dispatch@28959ce8df70de7be546dd1250a005dd32156697 # v4.0.1 + with: + token: ${{ secrets.HOMEBREW_REPO_DISPATCH_TOKEN }} + repository: runtimeconditions/packages + event-type: rc-profiler-java-release-published + client-payload: '{"tag":"${{ github.ref_name }}"}' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c56ed8d..3bb8419 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,3 +25,38 @@ jobs: java-version: ${{ matrix.java }} cache: maven - run: mvn -B -ntp verify + + # Builds the deb, rpm and Homebrew formula as a release would, and installs the packages in clean containers that + # have no Java. A release builds them itself, so this is skipped on a tag. + packages: + if: ${{ !startsWith(github.ref, 'refs/tags/') }} + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + with: + persist-credentials: false + - uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0 + with: + distribution: temurin + java-version: '21' + cache: maven + - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0 + with: + go-version: '1.26.5' + cache: false + - name: Install nfpm + env: + GOBIN: ${{ runner.temp }}/release-tools + run: | + go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.48.0 + echo "$GOBIN" >> "$GITHUB_PATH" + - name: Test the packaging helpers + run: python3 -m unittest scripts/test_release_package.py + - name: Build the jar + run: mvn -B -ntp -DskipTests package + - name: Build the packages + run: | + jar=$(ls target/runtimeconditions-java-profiler-*.jar | grep -v original) + python3 scripts/release-package.py --version v0.0.0 --jar "$jar" --output "$RUNNER_TEMP/packages" --system-packages + - name: Install them in clean Ubuntu, Debian and Rocky Linux containers + run: scripts/check-packages.sh "$RUNNER_TEMP/packages" diff --git a/.gitignore b/.gitignore index f2564bc..cf311e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store target/ +__pycache__/ diff --git a/distribution/homebrew/rc-profiler-java.rb b/distribution/homebrew/rc-profiler-java.rb new file mode 100644 index 0000000..8a6fa60 --- /dev/null +++ b/distribution/homebrew/rc-profiler-java.rb @@ -0,0 +1,22 @@ +# Release template. scripts/release-package.py replaces the version and checksum. +class RcProfilerJava < Formula + desc "Runtime Conditions profiler for Java codebases" + homepage "https://github.com/runtimeconditions/java-rc-profiler" + version "{{VERSION}}" + url "https://github.com/runtimeconditions/java-rc-profiler/releases/download/v#{version}/rc-profiler-java_#{version}.tar.gz" + sha256 "{{SHA256}}" + license "MIT" + + depends_on "openjdk@21" + + def install + libexec.install "rc-profiler-java", "runtimeconditions-java-profiler.jar" + # The launcher uses JAVA_HOME when it is set and otherwise this JDK. + (bin/"rc-profiler-java").write_env_script libexec/"rc-profiler-java", + Language::Java.overridable_java_home_env("21") + end + + test do + assert_match version.to_s, shell_output("#{bin}/rc-profiler-java --version") + end +end diff --git a/distribution/jdk.lock.json b/distribution/jdk.lock.json new file mode 100644 index 0000000..67c7acc --- /dev/null +++ b/distribution/jdk.lock.json @@ -0,0 +1,17 @@ +{ + "vendor": "Eclipse Temurin", + "version": "21.0.13+9", + "top": "jdk-21.0.13+9", + "platforms": { + "amd64": { + "url": "https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.13%2B9/OpenJDK21U-jdk_x64_linux_hotspot_21.0.13_9.tar.gz", + "file": "OpenJDK21U-jdk_x64_linux_hotspot_21.0.13_9.tar.gz", + "sha256": "d7f5d5cf865a2902b70ff549b97586a7d3f234e016f041ab447ea740c7d66d1c" + }, + "arm64": { + "url": "https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.13%2B9/OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.13_9.tar.gz", + "file": "OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.13_9.tar.gz", + "sha256": "e3522e707fc7dbe2a72c30746b1e7068301a0213ecd213d4491f35a14eb55578" + } + } +} diff --git a/distribution/nfpm.yaml b/distribution/nfpm.yaml new file mode 100644 index 0000000..9972515 --- /dev/null +++ b/distribution/nfpm.yaml @@ -0,0 +1,17 @@ +name: rc-profiler-java +arch: ${GOARCH} +platform: linux +version: ${PACKAGE_VERSION} +release: 1 +section: utils +priority: optional +maintainer: Runtime Conditions +description: Runtime Conditions profiler for Java codebases, with its own Java runtime +vendor: Runtime Conditions +homepage: https://github.com/runtimeconditions/java-rc-profiler +license: MIT +contents: + - src: usr + dst: /usr + type: tree +umask: 0o022 diff --git a/scripts/check-packages.sh b/scripts/check-packages.sh new file mode 100755 index 0000000..337f467 --- /dev/null +++ b/scripts/check-packages.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# Install the built packages in clean containers and run the profiler. +# +# scripts/check-packages.sh PACKAGE_DIR [FIXTURES_DIR] +# +# The containers have no Java, so this proves that the packages carry their own runtime. +# FIXTURES_DIR defaults to src/test/resources, which holds generated binding packages and workloads. +set -eu + +packages=$(cd "${1:?usage: check-packages.sh PACKAGE_DIR [FIXTURES_DIR]}" && pwd) +fixtures=$(cd "${2:-$(dirname "$0")/../src/test/resources}" && pwd) + +# Runs inside the container, after the package is installed. +check=' +set -eu +if command -v java >/dev/null 2>&1; then echo "java is on PATH, the test would prove nothing" >&2; exit 1; fi +version=$(rc-profiler-java --version --json) +echo "$version" | grep -q "\"coreSchemas\":\[\"0.4.0\"\]" +profile=$(rc-profiler-java profile generate --project /fixtures/workloads/owned --classpath /fixtures/packages/owned-kind-interface --name owned --workload-uri https://example.test/owned) +echo "$profile" | grep -q "^ - kind: service" +' + +run() { + image=$1 + install=$2 + docker pull -q "$image" >/dev/null + if docker run --rm -v "$packages:/packages:ro" -v "$fixtures:/fixtures:ro" "$image" \ + sh -c "set -e; $install >/dev/null 2>&1; $check" >/dev/null 2>&1; then + echo "$image: ok" + else + echo "$image: FAILED" >&2 + return 1 + fi +} + +arch=$(uname -m) +case $arch in + x86_64) deb=amd64; rpm=x86_64 ;; + aarch64|arm64) deb=arm64; rpm=aarch64 ;; + *) echo "unsupported architecture $arch" >&2; exit 1 ;; +esac + +for image in ubuntu:24.04 debian:12; do + run "$image" "apt-get install -y /packages/rc-profiler-java_*_$deb.deb" +done +for image in rockylinux:8 rockylinux:9; do + run "$image" "dnf install -y /packages/rc-profiler-java-*.$rpm.rpm" +done diff --git a/scripts/release-package.py b/scripts/release-package.py new file mode 100755 index 0000000..c8ce399 --- /dev/null +++ b/scripts/release-package.py @@ -0,0 +1,237 @@ +#!/usr/bin/env python3 +"""Build the release files of the Java profiler. + +For one release tag this writes, into a new or empty directory: + + runtimeconditions-java-profiler-.jar the jar, which needs a JDK 17 or newer + rc-profiler-java_.tar.gz the launcher and the jar, for Homebrew and for download + SHA256SUMS the checksum of every file above and below + +and, for a stable tag, when --system-packages is given: + + rc-profiler-java_-1_.deb the launcher and the jar with their own Java runtime, for apt + rc-profiler-java--1..rpm the same, for yum and dnf + rc-profiler-java.rb the Homebrew formula for this release + +The deb and rpm carry a Java runtime made with jlink from the Temurin JDK pinned in distribution/jdk.lock.json, +holding only the modules the jar needs, so installing them needs nothing but the system package manager. Homebrew +depends on its own openjdk formula. Run on Linux amd64: it runs the pinned amd64 jlink for both architectures, +with each architecture's own jmods. +""" + +import argparse +import gzip +import hashlib +import io +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import tarfile +import tempfile +import urllib.request + +STABLE = re.compile(r"^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$") +ANY = re.compile(r"^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?$") +NAME = "rc-profiler-java" +JAR = "runtimeconditions-java-profiler.jar" +# jdeps finds the modules the jar's code names; the compiler is found through a service loader, so name it too. +ALWAYS_NEEDED = ("jdk.compiler",) + + +def run(args, *, cwd=None, env=None): + subprocess.run(args, cwd=cwd, env=env, check=True) + + +def sha256_file(path): + digest = hashlib.sha256() + with open(path, "rb") as handle: + for block in iter(lambda: handle.read(1 << 20), b""): + digest.update(block) + return digest.hexdigest() + + +def fetch(url, destination, expected_sha256): + """Download url to destination and refuse it unless it matches the pinned checksum.""" + with urllib.request.urlopen(url, timeout=300) as response, open(destination, "wb") as out: + shutil.copyfileobj(response, out) + actual = sha256_file(destination) + if actual != expected_sha256: + Path(destination).unlink() + raise ValueError(f"{url}: checksum {actual} differs from the pinned {expected_sha256}") + + +def jdk(lock, goarch, cache, whole): + """Download the pinned JDK for one architecture and return its directory. + + The JDK that runs jlink is extracted whole; another architecture's only gives its jmods.""" + platform = lock["platforms"][goarch] + archive = cache / platform["file"] + if not archive.exists(): + fetch(platform["url"], archive, platform["sha256"]) + elif sha256_file(archive) != platform["sha256"]: + raise ValueError(f"{archive}: cached JDK archive differs from the pinned checksum") + out = cache / goarch + top = lock["top"] + out.mkdir(exist_ok=True) + with tarfile.open(archive, "r:gz") as tar: + members = [m for m in tar.getmembers() if whole or m.name.startswith(f"{top}/jmods/")] + if not members: + raise ValueError(f"{archive}: nothing to extract under {top}") + tar.extractall(out, members=members, filter="data") + return out / top + + +def runtime_modules(jdeps, jar): + """The modules the jar needs, as a comma-separated list.""" + found = subprocess.run( + [str(jdeps), "--multi-release", "21", "--ignore-missing-deps", "--print-module-deps", str(jar)], + check=True, capture_output=True, text=True, + ).stdout.strip() + modules = [item for item in found.split(",") if item] + for needed in ALWAYS_NEEDED: + if needed not in modules: + modules.append(needed) + if "java.base" not in modules: + raise ValueError(f"jdeps did not list java.base for {jar}: {found!r}") + return ",".join(sorted(set(modules))) + + +def make_runtime(jlink, jmods, modules, output): + run([str(jlink), "--module-path", str(jmods), "--add-modules", modules, "--strip-debug", "--no-header-files", + "--no-man-pages", "--compress=zip-6", "--output", str(output)]) + + +def normalize(root, epoch): + """Give every file and directory the same modification time, so equal input gives equal packages.""" + for path in [root, *root.rglob("*")]: + os.utime(path, (epoch, epoch), follow_symlinks=False) + + +def stage_package_root(launcher, jar, runtime, project_license, root, epoch): + usr = root / "usr" + (usr / "bin").mkdir(parents=True) + shutil.copy2(launcher, usr / "bin" / NAME) + (usr / "bin" / NAME).chmod(0o755) + library = usr / "lib" / NAME + library.mkdir(parents=True) + shutil.copy2(jar, library / JAR) + # The launcher finds the jar in ../lib/rc-profiler-java, and a runtime in a runtime directory beside it. + shutil.copytree(runtime, library / "runtime", symlinks=True) + documentation = usr / "share/doc" / NAME + documentation.mkdir(parents=True) + shutil.copy2(project_license, documentation / "copyright") + normalize(root, epoch) + + +def make_archive(root, output, epoch): + """Write a reproducible tar.gz: sorted entries, fixed times, no owner names.""" + buffer = io.BytesIO() + with gzip.GzipFile(fileobj=buffer, mode="wb", mtime=0) as compressed: + with tarfile.open(fileobj=compressed, mode="w", format=tarfile.PAX_FORMAT) as archive: + for path in sorted(root.rglob("*")): + info = archive.gettarinfo(str(path), arcname=path.relative_to(root).as_posix()) + info.uid = info.gid = 0 + info.uname = info.gname = "" + info.mtime = epoch + if info.isreg(): + with open(path, "rb") as handle: + archive.addfile(info, handle) + else: + archive.addfile(info) + Path(output).write_bytes(buffer.getvalue()) + + +def render_formula(template, version, archive_sha256): + rendered = template.replace("{{VERSION}}", version).replace("{{SHA256}}", archive_sha256) + if "{{" in rendered: + raise ValueError("Homebrew formula template has unresolved release fields") + return rendered + + +def checksums(output): + files = sorted(path for path in output.iterdir() if path.name != "SHA256SUMS") + return "".join(f"{sha256_file(path)} {path.name}\n" for path in files) + + +def source_date_epoch(repository): + value = os.environ.get("SOURCE_DATE_EPOCH") + if value is not None: + return int(value) + try: + out = subprocess.run(["git", "log", "-1", "--format=%ct"], cwd=repository, check=True, capture_output=True, text=True) + return int(out.stdout.strip()) + except (OSError, subprocess.CalledProcessError, ValueError): + return 0 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--version", required=True, help="release tag, such as v0.3.0; system packages need a stable one") + parser.add_argument("--jar", required=True, type=Path, help="the jar built at this version") + parser.add_argument("--output", required=True, type=Path, help="new or empty directory for the release files") + parser.add_argument("--system-packages", action="store_true", help="also build the deb, rpm and Homebrew formula") + parser.add_argument("--repository", type=Path, default=Path(__file__).resolve().parent.parent) + args = parser.parse_args() + if not ANY.fullmatch(args.version): + raise ValueError("release requires a vMAJOR.MINOR.PATCH tag, optionally with a prerelease suffix") + if args.system_packages and not STABLE.fullmatch(args.version): + raise ValueError("system packages need a stable vMAJOR.MINOR.PATCH tag") + version = args.version[1:] + repository = args.repository.resolve() + if not args.jar.is_file(): + raise ValueError(f"{args.jar} is not a file") + if args.output.exists() and any(args.output.iterdir()): + raise ValueError("release output directory must be new or empty") + args.output.mkdir(parents=True, exist_ok=True) + output = args.output.resolve() + epoch = source_date_epoch(repository) + launcher = repository / "distribution" / NAME + project_license = repository / "LICENSE" + + shutil.copy2(args.jar, output / f"runtimeconditions-java-profiler-{version}.jar") + with tempfile.TemporaryDirectory(prefix="rc-profiler-java-release-") as temporary: + work = Path(temporary) + archive_root = work / "archive" + package = archive_root / f"{NAME}_{version}" + package.mkdir(parents=True) + shutil.copy2(launcher, package / NAME) + (package / NAME).chmod(0o755) + shutil.copy2(args.jar, package / JAR) + shutil.copy2(repository / "README.md", package / "README.md") + shutil.copy2(project_license, package / "LICENSE") + normalize(archive_root, epoch) + archive = output / f"{NAME}_{version}.tar.gz" + make_archive(archive_root, archive, epoch) + + if args.system_packages: + lock = json.loads((repository / "distribution/jdk.lock.json").read_text()) + nfpm_config = (repository / "distribution/nfpm.yaml").resolve() + cache = work / "jdk" + cache.mkdir() + host = jdk(lock, "amd64", cache, whole=True) + modules = runtime_modules(host / "bin/jdeps", args.jar) + for goarch in sorted(lock["platforms"]): + jmods = host / "jmods" if goarch == "amd64" else jdk(lock, goarch, cache, whole=False) / "jmods" + runtime = work / goarch / "runtime" + runtime.parent.mkdir() + make_runtime(host / "bin/jlink", jmods, modules, runtime) + package_root = work / goarch / "package-root" + stage_package_root(launcher, args.jar, runtime, project_license, package_root, epoch) + env = dict(os.environ, GOARCH=goarch, PACKAGE_VERSION=version, SOURCE_DATE_EPOCH=str(epoch)) + for packager in ("deb", "rpm"): + run(["nfpm", "package", "--config", str(nfpm_config), "--packager", packager, "--target", str(output)], + cwd=package_root, env=env) + shutil.rmtree(work / goarch) + template = (repository / "distribution/homebrew" / f"{NAME}.rb").read_text() + (output / f"{NAME}.rb").write_text(render_formula(template, version, sha256_file(archive))) + (output / "SHA256SUMS").write_text(checksums(output)) + + +if __name__ == "__main__": + try: + main() + except (OSError, ValueError, subprocess.CalledProcessError) as error: + raise SystemExit(f"release-package: {error}") diff --git a/scripts/test_release_package.py b/scripts/test_release_package.py new file mode 100644 index 0000000..c8d4c60 --- /dev/null +++ b/scripts/test_release_package.py @@ -0,0 +1,226 @@ +"""Tests for the helpers in release-package.py. Run: python3 -m unittest scripts/test_release_package.py""" + +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import stat +import subprocess +import tarfile +import tempfile +import unittest +from unittest import mock + +SCRIPT = Path(__file__).with_name("release-package.py") +ROOT = SCRIPT.parent.parent +spec = importlib.util.spec_from_file_location("release_package", SCRIPT) +release = importlib.util.module_from_spec(spec) +spec.loader.exec_module(release) + + +class VersionTagTests(unittest.TestCase): + def test_a_stable_tag_has_no_suffix(self): + for tag in ("v0.1.0", "v1.20.3"): + self.assertTrue(release.STABLE.fullmatch(tag), tag) + for tag in ("v0.1.0-rc.1", "0.1.0", "v1.0", "v01.0.0", "main", ""): + self.assertFalse(release.STABLE.fullmatch(tag), tag) + + def test_any_release_tag_may_have_a_prerelease_suffix(self): + for tag in ("v0.1.0", "v0.1.0-rc.1", "v2.0.0-beta.2"): + self.assertTrue(release.ANY.fullmatch(tag), tag) + for tag in ("0.1.0", "v1.0", "v1.0.0.1", "v1.0.0-", "main"): + self.assertFalse(release.ANY.fullmatch(tag), tag) + + +class FetchTests(unittest.TestCase): + def test_a_download_must_match_the_pinned_checksum(self): + with tempfile.TemporaryDirectory() as directory: + source = Path(directory, "jdk.tar.gz") + source.write_bytes(b"pretend archive") + good = hashlib.sha256(b"pretend archive").hexdigest() + destination = Path(directory, "good") + release.fetch(source.as_uri(), destination, good) + self.assertEqual(destination.read_bytes(), b"pretend archive") + bad = Path(directory, "bad") + with self.assertRaisesRegex(ValueError, "differs from the pinned"): + release.fetch(source.as_uri(), bad, "0" * 64) + self.assertFalse(bad.exists(), "a download with the wrong checksum must not be kept") + + +class JdkLockTests(unittest.TestCase): + def setUp(self): + self.lock = json.loads((ROOT / "distribution/jdk.lock.json").read_text()) + + def test_the_lock_pins_a_checksum_for_each_architecture(self): + self.assertEqual(sorted(self.lock["platforms"]), ["amd64", "arm64"]) + version = self.lock["version"].replace("+", "_") + for goarch, platform in self.lock["platforms"].items(): + self.assertRegex(platform["sha256"], r"^[0-9a-f]{64}$", goarch) + self.assertTrue(platform["url"].startswith("https://"), goarch) + self.assertTrue(platform["url"].endswith(platform["file"]), goarch) + self.assertIn(version, platform["file"], goarch) + self.assertEqual(self.lock["top"], "jdk-" + self.lock["version"]) + + def test_the_bundled_jdk_is_new_enough_for_the_jar(self): + pom = (ROOT / "pom.xml").read_text() + release_level = int(pom.split("")[1].split("<")[0]) + self.assertGreaterEqual(int(self.lock["version"].split(".")[0]), release_level) + + def test_the_two_architectures_come_from_the_same_jdk_release(self): + files = [platform["file"] for platform in self.lock["platforms"].values()] + versions = {file.split("hotspot_")[1].removesuffix(".tar.gz") for file in files} + self.assertEqual(len(versions), 1, files) + + +class RuntimeTests(unittest.TestCase): + def jdeps(self, directory, output): + script = Path(directory, "jdeps") + script.write_text(f"#!/bin/sh\necho '{output}'\n") + script.chmod(0o755) + return script + + def test_the_runtime_has_the_modules_the_jar_needs_and_the_compiler(self): + with tempfile.TemporaryDirectory() as directory: + jdeps = self.jdeps(directory, "java.base,java.desktop,java.sql") + self.assertEqual(release.runtime_modules(jdeps, Path("x.jar")), "java.base,java.desktop,java.sql,jdk.compiler") + jdeps = self.jdeps(directory, "java.base,jdk.compiler") + self.assertEqual(release.runtime_modules(jdeps, Path("x.jar")), "java.base,jdk.compiler") + + def test_a_list_without_java_base_is_refused(self): + with tempfile.TemporaryDirectory() as directory: + with self.assertRaisesRegex(ValueError, "java.base"): + release.runtime_modules(self.jdeps(directory, "java.sql"), Path("x.jar")) + + def test_jlink_is_asked_for_a_small_image_of_those_modules(self): + with mock.patch.object(release, "run") as run: + release.make_runtime(Path("/jdk/bin/jlink"), Path("/jmods"), "java.base,jdk.compiler", Path("/out")) + command = run.call_args.args[0] + self.assertEqual(command[:5], ["/jdk/bin/jlink", "--module-path", "/jmods", "--add-modules", "java.base,jdk.compiler"]) + self.assertIn("--strip-debug", command) + self.assertEqual(command[-2:], ["--output", "/out"]) + + +class LayoutTests(unittest.TestCase): + def test_the_package_puts_the_launcher_where_it_finds_the_jar_and_the_runtime(self): + with tempfile.TemporaryDirectory() as directory: + base = Path(directory) + (base / "launcher").write_text("#!/bin/sh\n") + (base / "app.jar").write_bytes(b"jar") + (base / "LICENSE").write_text("license") + runtime = base / "runtime" + (runtime / "bin").mkdir(parents=True) + (runtime / "bin/java").write_text("java") + (runtime / "legal").mkdir() + (runtime / "legal/link").symlink_to("../bin/java") + root = base / "root" + release.stage_package_root(base / "launcher", base / "app.jar", runtime, base / "LICENSE", root, 1700000000) + self.assertTrue(os.access(root / "usr/bin/rc-profiler-java", os.X_OK)) + self.assertEqual((root / "usr/lib/rc-profiler-java/runtimeconditions-java-profiler.jar").read_bytes(), b"jar") + self.assertEqual((root / "usr/lib/rc-profiler-java/runtime/bin/java").read_text(), "java") + self.assertTrue((root / "usr/lib/rc-profiler-java/runtime/legal/link").is_symlink(), "links in the runtime are kept") + self.assertEqual((root / "usr/share/doc/rc-profiler-java/copyright").read_text(), "license") + self.assertTrue(all(path.lstat().st_mtime == 1700000000 for path in [root, *root.rglob("*")])) + + def test_the_real_launcher_finds_the_jar_and_runtime_of_that_layout(self): + # /bin/rc-profiler-java looks for /lib/rc-profiler-java, which is what the packages install. + launcher = (ROOT / "distribution/rc-profiler-java").read_text() + self.assertIn('"$here/../lib/rc-profiler-java"', launcher) + self.assertIn('"$app/runtime/bin/java"', launcher) + self.assertIn(release.JAR, launcher) + + +class ArchiveTests(unittest.TestCase): + def test_equal_input_gives_equal_bytes_with_fixed_owner_and_time(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory, "root") + (root / "rc-profiler-java_1.2.3").mkdir(parents=True) + (root / "rc-profiler-java_1.2.3/LICENSE").write_text("license") + first, second = Path(directory, "a.tar.gz"), Path(directory, "b.tar.gz") + release.make_archive(root, first, 1700000000) + release.make_archive(root, second, 1700000000) + self.assertEqual(first.read_bytes(), second.read_bytes()) + with tarfile.open(first) as archive: + self.assertEqual([m.name for m in archive.getmembers()], ["rc-profiler-java_1.2.3", "rc-profiler-java_1.2.3/LICENSE"]) + self.assertTrue(all(m.uid == 0 and m.gid == 0 and m.mtime == 1700000000 for m in archive.getmembers())) + + +class FormulaTests(unittest.TestCase): + def test_the_release_fields_are_filled_in(self): + template = (ROOT / "distribution/homebrew/rc-profiler-java.rb").read_text() + rendered = release.render_formula(template, "1.2.3", "a" * 64) + self.assertIn('version "1.2.3"', rendered) + self.assertIn('sha256 "' + "a" * 64 + '"', rendered) + self.assertNotIn("{{", rendered) + + def test_the_formula_installs_the_two_files_the_archive_holds(self): + template = (ROOT / "distribution/homebrew/rc-profiler-java.rb").read_text() + self.assertIn("rc-profiler-java_#{version}.tar.gz", template) + self.assertIn('libexec.install "rc-profiler-java", "runtimeconditions-java-profiler.jar"', template) + self.assertIn('depends_on "openjdk@21"', template) + + def test_an_unresolved_field_is_an_error(self): + with self.assertRaisesRegex(ValueError, "unresolved"): + release.render_formula("{{VERSION}} {{OTHER}}", "1.2.3", "a" * 64) + + +class ChecksumTests(unittest.TestCase): + def test_every_file_but_the_list_itself_is_listed_in_sorted_order(self): + with tempfile.TemporaryDirectory() as directory: + out = Path(directory) + (out / "b.deb").write_bytes(b"b") + (out / "a.jar").write_bytes(b"a") + (out / "SHA256SUMS").write_text("stale") + self.assertEqual( + release.checksums(out), + f"{hashlib.sha256(b'a').hexdigest()} a.jar\n{hashlib.sha256(b'b').hexdigest()} b.deb\n", + ) + + +class CommandLineTests(unittest.TestCase): + def run_script(self, *args): + return subprocess.run(["python3", str(SCRIPT), *args], capture_output=True, text=True) + + def test_a_prerelease_builds_the_jar_the_archive_and_the_checksums_only(self): + with tempfile.TemporaryDirectory() as directory: + jar = Path(directory, "x.jar") + jar.write_bytes(b"jar") + out = Path(directory, "out") + done = self.run_script("--version", "v0.1.0-rc.1", "--jar", str(jar), "--output", str(out)) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual( + sorted(path.name for path in out.iterdir()), + ["SHA256SUMS", "rc-profiler-java_0.1.0-rc.1.tar.gz", "runtimeconditions-java-profiler-0.1.0-rc.1.jar"], + ) + with tarfile.open(out / "rc-profiler-java_0.1.0-rc.1.tar.gz") as archive: + names = [m.name for m in archive.getmembers()] + self.assertEqual(names, [ + "rc-profiler-java_0.1.0-rc.1", "rc-profiler-java_0.1.0-rc.1/LICENSE", "rc-profiler-java_0.1.0-rc.1/README.md", + "rc-profiler-java_0.1.0-rc.1/rc-profiler-java", "rc-profiler-java_0.1.0-rc.1/runtimeconditions-java-profiler.jar"]) + launcher = archive.getmember("rc-profiler-java_0.1.0-rc.1/rc-profiler-java") + self.assertTrue(launcher.mode & stat.S_IXUSR, "the launcher must stay executable") + listed = {line.split(" ")[1] for line in (out / "SHA256SUMS").read_text().splitlines()} + self.assertEqual(listed, {"rc-profiler-java_0.1.0-rc.1.tar.gz", "runtimeconditions-java-profiler-0.1.0-rc.1.jar"}) + + def test_system_packages_need_a_stable_tag(self): + with tempfile.TemporaryDirectory() as directory: + jar = Path(directory, "x.jar") + jar.write_bytes(b"jar") + done = self.run_script("--version", "v0.1.0-rc.1", "--jar", str(jar), "--output", str(Path(directory, "o")), "--system-packages") + self.assertEqual(done.returncode, 1) + self.assertIn("stable", done.stderr) + + def test_a_bad_tag_a_missing_jar_and_a_used_output_directory_are_refused(self): + with tempfile.TemporaryDirectory() as directory: + jar = Path(directory, "x.jar") + jar.write_bytes(b"jar") + self.assertIn("vMAJOR.MINOR.PATCH", self.run_script("--version", "1.0", "--jar", str(jar), "--output", str(Path(directory, "a"))).stderr) + self.assertIn("is not a file", self.run_script("--version", "v1.0.0", "--jar", str(Path(directory, "no.jar")), "--output", str(Path(directory, "b"))).stderr) + used = Path(directory, "used") + used.mkdir() + (used / "x").write_text("x") + self.assertIn("new or empty", self.run_script("--version", "v1.0.0", "--jar", str(jar), "--output", str(used)).stderr) + + +if __name__ == "__main__": + unittest.main()