diff --git a/.github/actions/deb-package/action.yml b/.github/actions/deb-package/action.yml new file mode 100644 index 000000000..6c513d689 --- /dev/null +++ b/.github/actions/deb-package/action.yml @@ -0,0 +1,125 @@ +name: Package the Linux binary as a .deb +description: >- + Packages a Linux native binary as open-java-format__.deb, which installs + /usr/bin/open-java-format, and installs it with apt on the oldest Ubuntu and Debian it supports. ci.yml runs it + on every pull request, release.yml for the GitHub release and the apt repository, openjavaformat/apt. + +inputs: + binary: + description: The native binary, for Linux on x86-64 or AArch64 + required: true + version: + description: The version that the binary prints, which is the package version too (with 0~ in front when it does not start with a digit) + required: true + out-dir: + description: The directory to write the package into + required: true + +outputs: + package: + description: The path of the package + value: ${{ steps.build.outputs.package }} + +runs: + using: composite + steps: + # The architecture is read off the binary's ELF header, and so is the libc6 dependency: the newest GLIBC_ + # symbol version that the binary asks for. A binary built on a newer image than the one release.yml names + # then cannot slip past older systems: apt refuses the package there instead of installing a binary that + # does not start. SOURCE_DATE_EPOCH, when the caller sets it, is the time of every file in the package, + # which makes a rebuild the same bytes. + - name: Build the package + id: build + shell: bash + env: + BINARY: ${{ inputs.binary }} + VERSION: ${{ inputs.version }} + OUT_DIR: ${{ inputs.out-dir }} + COPYRIGHT: ${{ github.action_path }}/copyright + run: | + set -euo pipefail + # Debian's rule for an upstream version without an epoch: a digit first, then these characters. Outside + # a tag the binary's version can be a bare commit hash, which may start with a letter: 0~ in front makes + # it a version that sorts below every release. + deb_version=$VERSION + if ! [[ $deb_version =~ ^[0-9] ]]; then + deb_version=0~$deb_version + fi + if ! [[ $deb_version =~ ^[0-9][A-Za-z0-9.+~-]*$ ]]; then + echo "Not a Debian package version: $deb_version" >&2 + exit 1 + fi + + machine=$(readelf --file-header "$BINARY" | sed -n 's/^ *Machine: *//p') + case $machine in + 'Advanced Micro Devices X86-64') arch=amd64 ;; + AArch64) arch=arm64 ;; + *) + echo "$BINARY is not a Linux binary for x86-64 or AArch64: $machine" >&2 + exit 1 + ;; + esac + glibc=$(readelf --version-info --wide "$BINARY" | grep -o 'GLIBC_[0-9][0-9.]*' | sed 's/^GLIBC_//' \ + | sort -uV | tail -n 1) + if [ -z "$glibc" ]; then + echo "$BINARY asks for no GLIBC_ symbol version: not a glibc binary?" >&2 + exit 1 + fi + + # The modes of the package's directories, mktemp's 0700 included, are those of what it installs. + umask 022 + root=$(mktemp -d) + chmod 0755 "$root" + install -D --mode 0755 "$BINARY" "$root/usr/bin/open-java-format" + install -D --mode 0644 "$COPYRIGHT" "$root/usr/share/doc/open-java-format/copyright" + mkdir "$root/DEBIAN" + cat > "$root/DEBIAN/control" < + Installed-Size: $(du -sk --apparent-size "$root/usr" | cut -f 1) + Depends: libc6 (>= $glibc), zlib1g + Section: devel + Priority: optional + Homepage: https://openjavaformat.dev + Description: modern, lambda-friendly, 120-character Java formatter + open-java-format reformats Java source files to one consistent style. This + package is its native command-line binary, which needs no JDK. + CONTROL + + epoch=${SOURCE_DATE_EPOCH:-$(date +%s)} + find "$root" -exec touch --no-dereference --date "@$epoch" {} + + mkdir -p "$OUT_DIR" + package=$OUT_DIR/open-java-format_${deb_version}_$arch.deb + # xz, which every dpkg reads: dpkg-deb's default, zstd, needs dpkg 1.21.18 or later. + SOURCE_DATE_EPOCH=$epoch dpkg-deb --root-owner-group -Zxz --build "$root" "$package" + rm -rf "$root" + dpkg-deb --info "$package" control + echo "package=$package" >> "$GITHUB_OUTPUT" + + # In a fresh container each: apt brings the dependencies, then the binary must run from /usr/bin, print the + # version, and format a file. + - name: Install the package on Ubuntu 22.04 and Debian 12 + shell: bash + env: + PACKAGE: ${{ steps.build.outputs.package }} + VERSION: ${{ inputs.version }} + run: | + set -euo pipefail + for image in ubuntu:22.04 debian:12; do + echo "::group::$image" + docker run --rm --env VERSION --volume "$(realpath "$PACKAGE"):/open-java-format.deb:ro" "$image" sh -euxc ' + export DEBIAN_FRONTEND=noninteractive + apt-get update -qq + apt-get install -qq --no-install-recommends /open-java-format.deb + test "$(command -v open-java-format)" = /usr/bin/open-java-format + # It prints the version on stderr. + test "$(open-java-format --version 2>&1)" = "open-java-format: Version $VERSION" + cd "$(mktemp -d)" + printf "class A{int x;}\n" > A.java + open-java-format --replace A.java + printf "class A {\n int x;\n}\n" | diff - A.java + ' + echo "::endgroup::" + done diff --git a/.github/actions/deb-package/copyright b/.github/actions/deb-package/copyright new file mode 100644 index 000000000..33d7c4ee4 --- /dev/null +++ b/.github/actions/deb-package/copyright @@ -0,0 +1,24 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: open-java-format +Upstream-Contact: https://github.com/openjavaformat/open-java-format/issues +Source: https://github.com/openjavaformat/open-java-format + +Files: * +Copyright: open-java-format contributors, palantir-java-format contributors, google-java-format contributors +License: Apache-2.0 + +License: Apache-2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + . + On Debian systems, the complete text of the Apache License, Version 2.0 + can be found in "/usr/share/common-licenses/Apache-2.0". diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9476b53e7..f4d78e5dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -170,11 +170,13 @@ jobs: strategy: fail-fast: false matrix: - # The newest images. Arm Linux and Intel macOS have no free -latest label, so their images are - # named. + # Linux on the previous Ubuntu LTS, not the newest: a binary built against a newer glibc may need that + # glibc to start, and the older one keeps the binaries running on older systems. GitHub retires an + # image once two newer ones are out. release.yml builds on the same images. Elsewhere the newest + # images; Intel macOS has no free -latest label, so its image is named. runner: - - ubuntu-latest - - ubuntu-26.04-arm + - ubuntu-24.04 + - ubuntu-24.04-arm # macOS on Apple silicon is built for a release only, in release.yml. # - macos-latest - macos-26-intel @@ -237,6 +239,24 @@ jobs: "$binary" --replace C.java printf 'import module java.base;\n\nrecord Box(int a, int b) {}\n\nvoid main() {\n Object o = new Box(1, 2);\n if (o instanceof Box(_, _)) {\n IO.println(List.of(1));\n }\n}\n' | diff - C.java + # Packaged as release.yml packages it for the GitHub release and the apt repository, under the version + # the binary prints. + - name: Find the binary and its version + id: binary + if: runner.os == 'Linux' + run: | + binary="$(ls open-java-format-native/build/native/nativeCompile/open-java-format-* | grep -v '\.txt$')" + echo "path=$binary" >> "$GITHUB_OUTPUT" + echo "version=$("$binary" --version 2>&1 | sed -n 's/^open-java-format: Version //p')" >> "$GITHUB_OUTPUT" + + - name: Package and install the binary as a .deb + if: runner.os == 'Linux' + uses: $/.github/actions/deb-package + with: + binary: ${{ steps.binary.outputs.path }} + version: ${{ steps.binary.outputs.version }} + out-dir: ${{ runner.temp }}/deb-package + - name: Test the plugins against the image run: ./gradlew -PnativeImage=true :open-java-format-jdk-bootstrap:test :gradle-open-java-format:test env: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 03cef331a..4546aa7ef 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,8 +11,8 @@ name: Release # Once Maven Central serves both, the Gradle plugins go to the Gradle Plugin Portal, signed with the same # release key, and the IntelliJ plugin goes to the JetBrains Marketplace, verified against the IDEs it # supports first. After publishing, a draft GitHub release on the tag collects the runnable jar, the Gradle -# and IDE plugins, the native binaries and the WebAssembly module, which a job of its own builds on Oracle -# GraalVM. +# and IDE plugins, the native binaries, the Linux binaries again as .deb packages, and the WebAssembly module, +# which a job of its own builds on Oracle GraalVM. # # The release notes, release-notes/.md, are on the tag before it is pushed: they are the text of # the GitHub release and the IntelliJ plugin's change notes, and a tag without them publishes nothing. @@ -132,10 +132,10 @@ jobs: strategy: fail-fast: false matrix: - # The newest images, as in ci.yml. + # The images of ci.yml: Linux on the previous Ubuntu LTS, for an older glibc, the rest the newest. runner: - - ubuntu-latest - - ubuntu-26.04-arm + - ubuntu-24.04 + - ubuntu-24.04-arm - macos-latest - macos-26-intel - windows-latest @@ -176,6 +176,53 @@ jobs: if-no-files-found: error retention-days: 1 + # The Linux binaries as .deb packages, for the GitHub release and for the apt repository, openjavaformat/apt, + # which serves the packages of the newest release. Each is built on the runner its binary was built on and + # installed with apt on the oldest Ubuntu and Debian it supports before it goes into the release, by the + # deb-package action that ci.yml runs too. The time of the tag's commit is the time of every file in a + # package, so a re-run makes the same bytes. + deb-packages: + name: deb package (${{ matrix.runner }}) + needs: native-images + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + runner: + - ubuntu-24.04 + - ubuntu-24.04-arm + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Collect the binary + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: native-image-${{ matrix.runner }} + path: native-image + + - name: Find the binary + id: binary + run: | + echo "path=$(ls native-image/open-java-format-linux-glibc_*)" >> "$GITHUB_OUTPUT" + echo "SOURCE_DATE_EPOCH=$(git log -1 --format=%ct)" >> "$GITHUB_ENV" + + - name: Package and install the binary + uses: $/.github/actions/deb-package + with: + binary: ${{ steps.binary.outputs.path }} + version: ${{ github.ref_name }} + out-dir: deb-package + + - name: Hand the package to the GitHub release + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: deb-package-${{ matrix.runner }} + path: deb-package/*.deb + if-no-files-found: error + retention-days: 1 + # Not `native`: ci.yml already has a job with that id, and `act --job native` could not tell them apart. native-deploy: name: native deploy @@ -433,14 +480,14 @@ jobs: JETBRAINS_MARKETPLACE_TOKEN: ${{ secrets.JETBRAINS_MARKETPLACE_TOKEN }} # What Maven Central does not carry — the runnable formatter jar, the Gradle plugins' jar, the IntelliJ - # plugin zip, the Eclipse plugin jar, every platform's native binary as a plain download, and the - # WebAssembly module — goes into a draft GitHub release on the tag, each file signed with the release key. - # Its text is release-notes/.md, and the plugin zip has the same change notes as the one on the - # Marketplace. Only once both deployments are published and the module is built; publishing the draft is a - # click on GitHub, and a re-run fails while a release for the tag exists. + # plugin zip, the Eclipse plugin jar, every platform's native binary as a plain download, the .deb + # packages and the WebAssembly module — goes into a draft GitHub release on the tag, each file signed with + # the release key. Its text is release-notes/.md, and the plugin zip has the same change notes as + # the one on the Marketplace. Only once both deployments are published and the packages and the module are + # built; publishing the draft is a click on GitHub, and a re-run fails while a release for the tag exists. github-release: name: draft GitHub release - needs: [publish, wasm, release-notes] + needs: [publish, deb-packages, wasm, release-notes] runs-on: ubuntu-latest permissions: contents: write @@ -463,6 +510,13 @@ jobs: pattern: native-image-* merge-multiple: true + - name: Collect the .deb packages + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + path: deb-packages + pattern: deb-package-* + merge-multiple: true + - name: Collect the WebAssembly module uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: @@ -477,8 +531,8 @@ jobs: - name: Build the plugins and open the draft release run: >- - ./gradlew -PreleaseTarget=github -PnativeImages=native-images -PwasmModule=wasm-module - -PchangeNotes=change-notes/change-notes.html jreleaserRelease + ./gradlew -PreleaseTarget=github -PnativeImages=native-images -PdebPackages=deb-packages + -PwasmModule=wasm-module -PchangeNotes=change-notes/change-notes.html jreleaserRelease env: JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} JRELEASER_GPG_SECRET_KEY: ${{ secrets.JRELEASER_GPG_SECRET_KEY }} diff --git a/buildSrc/src/main/groovy/open-java-format.release-conventions.gradle b/buildSrc/src/main/groovy/open-java-format.release-conventions.gradle index 6b0c772f1..b9f68601d 100644 --- a/buildSrc/src/main/groovy/open-java-format.release-conventions.gradle +++ b/buildSrc/src/main/groovy/open-java-format.release-conventions.gradle @@ -14,8 +14,8 @@ plugins { // `-PreleaseTarget=github` deploys nothing. It is for `jreleaserRelease`, which opens a draft GitHub // release on the tag with what Maven Central does not carry — the runnable formatter jar, the Gradle // plugins' jar, the IntelliJ plugin zip, the Eclipse plugin jar, every platform's native binary as a -// plain download and the WebAssembly module for openjavaformat.dev's playground — each file signed, plus a -// checksum file. Its text is release-notes/.md. +// plain download, the Linux binaries as .deb packages, and the WebAssembly module for openjavaformat.dev's +// playground — each file signed, plus a checksum file. Its text is release-notes/.md. // // Publishing is a run of its own: with JRELEASER_MAVENCENTRAL_DEPLOYMENT_ID set, JReleaser publishes that // deployment, which an earlier run uploaded and the Portal validated, and nothing is built. The ID has to @@ -47,8 +47,10 @@ def stagingRepositories = releasedProjects.collect { path -> } // The GitHub release takes the binaries the per-platform jobs collected from -PnativeImages=, exactly -// as the native deployment does, and the WebAssembly module's zip that the wasm job built from -PwasmModule=. +// as the native deployment does, the .deb packages built from the Linux ones from -PdebPackages=, and the +// WebAssembly module's zip that the wasm job built from -PwasmModule=. def nativeBinaries = [] +def debPackages = [] def wasmModules = [] if (releaseTarget == 'github') { def collectedNativeImages = providers.gradleProperty('nativeImages').getOrNull() @@ -63,6 +65,18 @@ if (releaseTarget == 'github') { throw new GradleException("No native images to release in ${collectedNativeImages}") } + def collectedDebPackages = providers.gradleProperty('debPackages').getOrNull() + if (collectedDebPackages == null) { + throw new GradleException('-PreleaseTarget=github needs -PdebPackages= holding the .deb packages') + } + debPackages = fileTree(file(collectedDebPackages)) { + include 'open-java-format_*.deb' + }.files.sort { it.name } + // One per Linux binary: the apt repository serves both architectures or neither. + if (debPackages*.name.collect { it.replaceAll(/.*_/, '') } != ['amd64.deb', 'arm64.deb']) { + throw new GradleException("Expected an amd64 and an arm64 .deb package in ${collectedDebPackages}, found ${debPackages*.name}") + } + def collectedWasmModule = providers.gradleProperty('wasmModule').getOrNull() if (collectedWasmModule == null) { throw new GradleException('-PreleaseTarget=github needs -PwasmModule= holding open-java-format-wasm-.zip') @@ -123,7 +137,7 @@ jreleaser { // Name templates, not Gradle's version: the root project sets its version only after applying this // plugin. The jars and the zip are built by the tasks jreleaserRelease depends on below. files { - (nativeBinaries + wasmModules).each { file -> + (nativeBinaries + debPackages + wasmModules).each { file -> artifact { path = file.absolutePath }