Unverified Commit 5bf4fe54 authored by Konstantin Bogdanov's avatar Konstantin Bogdanov
Browse files

clickhouse, clickhouse-lts: fix versioning

parent 89685a79
Loading
Loading
Loading
Loading
+37 −4
Original line number Diff line number Diff line
{
  lts ? false,
  version,
  rev,
  hash,
  nixUpdateExtraArgs ? [ ],
}:

{
@@ -34,6 +34,7 @@ in
llvmStdenv.mkDerivation (finalAttrs: {
  pname = "clickhouse";
  inherit version;
  inherit rev;

  src = fetchFromGitHub rec {
    owner = "ClickHouse";
@@ -127,6 +128,36 @@ llvmStdenv.mkDerivation (finalAttrs: {
    cargoSetupPostPatchHook() { true; }
  '';

  # Set the version the same way as ClickHouse CI does.
  #
  # https://github.com/clickhouse/clickhouse/blob/31127f21f8bb7ff21f737c4822de10ef5859c702/ci/jobs/scripts/clickhouse_version.py#L11-L20
  # https://github.com/clickhouse/clickhouse/blob/31127f21f8bb7ff21f737c4822de10ef5859c702/ci/jobs/build_clickhouse.py#L179
  preConfigure =
    let
      gitTagName = finalAttrs.version;
      versionStr = builtins.elemAt (lib.splitString "-" gitTagName) 0;

      parts = lib.splitVersion versionStr;

      major = builtins.elemAt parts 0;
      minor = builtins.elemAt parts 1;
      patch = builtins.elemAt parts 2;

      # The full commit hash is already available here:
      gitHash = rev;
    in
    ''
      cat <<'EOF' > cmake/autogenerated_versions.txt
      SET(VERSION_REVISION 0)
      SET(VERSION_MAJOR ${major})
      SET(VERSION_MINOR ${minor})
      SET(VERSION_PATCH ${patch})
      SET(VERSION_GITHASH ${gitHash})
      SET(VERSION_DESCRIBE ${gitTagName})
      SET(VERSION_STRING ${versionStr})
      EOF
    '';

  cmakeFlags = [
    "-DENABLE_CHDIG=OFF"
    "-DENABLE_TESTS=OFF"
@@ -176,9 +207,11 @@ llvmStdenv.mkDerivation (finalAttrs: {
  passthru = {
    tests = if lts then nixosTests.clickhouse-lts else nixosTests.clickhouse;

    updateScript = nix-update-script {
      extraArgs = nixUpdateExtraArgs;
    };
    updateScript = [
      ./update.sh

      (if lts then ./lts.nix else ./package.nix)
    ];
  };

  meta = with lib; {
+1 −6
Original line number Diff line number Diff line
import ./generic.nix {
  version = "25.8.10.7-lts";
  rev = "02ec3a1ea1e08fb18d2d9638f00b2b557fa1cc1c";
  hash = "sha256-EOZ2AfeBeXAWQqa25eQX3loE+xegt03lsCU1aQt/Ebs=";
  lts = true;
  nixUpdateExtraArgs = [
    "--version-regex"
    "^v?(.*-lts)$"
    "--override-filename"
    "pkgs/by-name/cl/clickhouse/lts.nix"
  ];
}
+1 −6
Original line number Diff line number Diff line
import ./generic.nix {
  version = "25.9.4.58-stable";
  rev = "08afb4f28eefe01513442c3647a2e00b703d5922";
  hash = "sha256-HRbqVSyDuvhkv0+PSgps9AXKdLlukrLA65OLx5gZ3c0=";
  lts = false;
  nixUpdateExtraArgs = [
    "--version-regex"
    "^v?(.*-stable|.*-lts)$"
    "--override-filename"
    "pkgs/by-name/cl/clickhouse/package.nix"
  ];
}
+78 −0
Original line number Diff line number Diff line
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p jq nix

set -euo pipefail
echoerr() { echo "$@" 1>&2; }

fetchgithub() {
    set +eo pipefail
    nix-build -A "$1" 2>&1 >/dev/null | grep "got:" | cut -d':' -f2 | sed 's| ||g'
    set -eo pipefail
}

fname="$1"
echoerr "Working on $fname"
shift

# Fetch latest tags from the repo, leave only stable and lts, use version sort in reverse order.
all_tags=$(curl -L -s ${GITHUB_TOKEN:+-u ":${GITHUB_TOKEN}"} https://api.github.com/repos/ClickHouse/ClickHouse/tags \
           | jq -r '.[].name | select(test("-(stable|lts)$"))' \
           | sort -Vr)

# Fail if no tags found
if [[ -z "$all_tags" ]]; then
    echoerr "Error: no suitable tag found in ClickHouse repo"
    exit 1
fi

pname="clickhouse"
if [[ "$fname" == *lts.nix ]]; then
    all_tags=$(echo "$all_tags" | grep -- "-lts$")
    pname="clickhouse-lts"
fi

latest_tag=$(echo "$all_tags" | head -n1)
version=${latest_tag##v}

# Do nothing if latest version is already there.
if grep -q "version = \"$version\"" "$fname"; then
    echoerr "Version $version is already the latest in $fname"
    exit 0
fi

echoerr "Latest tag for $fname: $latest_tag (version $version)"

# Get the annotated tag commit SHA.
rev=$(curl -L -s ${GITHUB_TOKEN:+-u ":${GITHUB_TOKEN}"} "https://api.github.com/repos/clickhouse/clickhouse/git/ref/tags/$latest_tag" \
      | jq -r '.object.sha')
rev=$(curl -L -s ${GITHUB_TOKEN:+-u ":${GITHUB_TOKEN}"} "https://api.github.com/repos/clickhouse/clickhouse/git/tags/$rev" \
      | jq -r '.object.sha')
echoerr "Resolved commit hash for tag $latest_tag: $rev"

# Update version.
sed -i -E "s@(version = \").*(\";)@\1$version\2@" "$fname"
grep -q "$version" "$fname"

# Update Git hash.
sed -i -E "s@(rev = \").*(\";)@\1$rev\2@" "$fname"
grep -q "$rev" "$fname"

# Update source hash.
# First, put lib.fakeHash value.
tmp_src_hash="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
sed -i -E "s@(hash = \").*(\";)@\1$tmp_src_hash\2@" "$fname"
grep -q "$tmp_src_hash" "$fname"

# Next, we need to run actual build due to presence of postFetch script.
echoerr "Running fetchFromGitHub on $pname to get new sources hash"

src_hash=$(fetchgithub "$pname.src")
if [[ -z "$src_hash" ]]; then
    echoerr "Error: failed to get source hash from nix-build"
    exit 1
fi

# Finally, update the source hash.
echoerr "New src hash: $src_hash"
sed -i -E "s@(hash = \").*(\";)@\1$src_hash\2@" "$fname"
grep -q "$src_hash" "$fname"