Files
Echoion/scripts/release-component.sh
T
faumaray 2c8164584b
Modular container CI / release-build (push) Failing after 17m56s
Publish components through Gitea packages
2026-07-29 23:45:09 +03:00

403 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "usage: $0 COMPONENT_RELEASE_JSON" >&2
exit 2
}
(($# == 1)) || usage
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
root=$(cd -- "$script_dir/.." && pwd -P)
source_root=$(pwd -P)
configuration=$(realpath -- "$1")
# shellcheck source=scripts/lib/component-ssh.sh
source "$script_dir/lib/component-ssh.sh"
required_commands=(
cmake
cmp
ctest
curl
find
git
jq
ninja
openssl
python3
realpath
ssh
zstd
)
for command_name in "${required_commands[@]}"; do
command -v "$command_name" >/dev/null ||
{
echo "release-component: missing command: $command_name" >&2
exit 2
}
done
required_environment=(
ECHOION_COMPONENT_SSH_KEY
ECHOION_DISTRIBUTION_BASE_URL
ECHOION_FEED_BASE_URL
ECHOION_FEED_COMMIT_SIGNING_KEY
ECHOION_FEED_PRIVATE_KEY
ECHOION_FEED_REPOSITORY
ECHOION_FEED_SIGNING_KEY_ID
ECHOION_RELEASE_TAG_ALLOWED_SIGNERS
GITEA_API_URL
GITEA_RELEASE_TOKEN
GITEA_REPOSITORY
)
for variable in "${required_environment[@]}"; do
if [[ -z ${!variable:-} ]]; then
echo "release-component: $variable is required" >&2
exit 2
fi
done
distribution_backend=${ECHOION_DISTRIBUTION_BACKEND:-s3}
case "$distribution_backend" in
gitea-generic)
if [[ $GITEA_API_URL != */api/v1 ]]; then
echo "release-component: GITEA_API_URL must end in /api/v1" >&2
exit 2
fi
gitea_root=${GITEA_API_URL%/api/v1}
if [[ $ECHOION_DISTRIBUTION_BASE_URL != \
"$gitea_root"/api/packages/*/generic ]]; then
echo "release-component: Gitea distribution URL must use the configured Gitea package registry" >&2
exit 2
fi
;;
s3)
command -v aws >/dev/null ||
{
echo "release-component: missing command: aws" >&2
exit 2
}
if [[ -z ${ECHOION_OBJECT_STORAGE_BUCKET:-} ]]; then
echo "release-component: ECHOION_OBJECT_STORAGE_BUCKET is required for the S3 backend" >&2
exit 2
fi
;;
*)
echo "release-component: unsupported distribution backend: $distribution_backend" >&2
exit 2
;;
esac
component_id=$(jq -er '
if .schemaVersion == 1 and
(.componentId | type == "string" and length > 0) and
(.productCompatibility | type == "string" and length > 0) and
(.cmakeOptions | type == "object")
then .componentId
else error("invalid component release configuration")
end
' "$configuration")
if [[ ! $component_id =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$ ]]; then
echo "release-component: invalid component ID" >&2
exit 2
fi
repository_name=${GITEA_REPOSITORY#*/}
if [[ $GITEA_REPOSITORY != */* || -z $repository_name ]]; then
echo "release-component: GITEA_REPOSITORY must be owner/name" >&2
exit 2
fi
work_root=${RUNNER_TEMP:-"$source_root/.component-release"}
work_root=$(realpath -m -- "$work_root/echoion-$component_id")
components="$work_root/components"
dependencies="$work_root/dependencies"
build="$work_root/build"
install_root="$work_root/install"
dist="$work_root/dist"
feed_state="$work_root/feed-state"
secrets_dir="$work_root/secrets"
mkdir -p "$work_root"
rm -rf -- "$components" "$dependencies" "$build" "$install_root" "$dist" \
"$feed_state" "$secrets_dir"
mkdir -p "$secrets_dir"
chmod 0700 "$secrets_dir"
cleanup() {
rm -rf -- "$secrets_dir"
}
trap cleanup EXIT
umask 077
feed_key="$secrets_dir/feed-ed25519.pem"
commit_key="$secrets_dir/feed-commit-key"
allowed_signers="$secrets_dir/tag-allowed-signers"
printf '%s\n' "$ECHOION_FEED_PRIVATE_KEY" >"$feed_key"
printf '%s\n' "$ECHOION_FEED_COMMIT_SIGNING_KEY" >"$commit_key"
printf '%s\n' "$ECHOION_RELEASE_TAG_ALLOWED_SIGNERS" >"$allowed_signers"
chmod 0600 "$feed_key" "$commit_key" "$allowed_signers"
echoion_configure_component_ssh \
"$secrets_dir" "$ECHOION_COMPONENT_SSH_KEY"
openssl pkey -in "$feed_key" -noout
git clone --filter=blob:none --branch release-feed --single-branch \
"$ECHOION_FEED_REPOSITORY" "$feed_state"
event=${GITEA_EVENT_NAME:-push}
sequence=$(python3 -c 'import time; print(time.time_ns() // 1_000_000)')
if [[ $event == schedule ]]; then
channel=nightly
release_tag="nightly-$(date -u +%Y%m%d)-${GITEA_RUN_NUMBER:-$sequence}"
requested_arguments=(--nightly-sequence "$sequence")
else
release_tag=${GITEA_REF_NAME:-}
if [[ -z $release_tag || $release_tag != v* ]]; then
echo "release-component: a v* tag is required" >&2
exit 2
fi
version_from_tag=${release_tag#v}
if [[ $version_from_tag == *-* ]]; then
channel=beta
else
channel=stable
fi
requested_arguments=(--requested-version "$version_from_tag")
"$script_dir/verify-gitea-release-tag.sh" \
--tag "$release_tag" \
--event-sha "${GITEA_SHA:-$(git rev-parse HEAD)}" \
--allowed-signers "$allowed_signers" \
--notes-output "$work_root/release-notes.md" >/dev/null
fi
metadata="$work_root/release-metadata.json"
set +e
python3 "$script_dir/prepare-component-release.py" \
--registry "$root/components.json" \
--configuration "$configuration" \
--repository "$source_root" \
--feed-root "$feed_state" \
--channel "$channel" \
"${requested_arguments[@]}" \
--output "$metadata"
prepare_status=$?
set -e
if ((prepare_status == 3)) && [[ $channel == nightly ]]; then
echo "release-component: $component_id has no unpublished nightly source"
exit 0
fi
((prepare_status == 0)) || exit "$prepare_status"
component_version=$(jq -er .componentVersion "$metadata")
source_sha=$(jq -er .sourceSha "$metadata")
source_tree_hash=$(jq -er .sourceTreeHash "$metadata")
product_compatibility=$(jq -er .productCompatibility "$metadata")
abi_hash=$(jq -r '.abiHash // empty' "$metadata")
product_version=$(python3 - "$root/CMakeLists.txt" <<'PY'
import pathlib
import re
import sys
source = pathlib.Path(sys.argv[1]).read_text(encoding="utf-8")
match = re.search(
r"(?m)^project\(Echoion[ \t]+VERSION[ \t]+"
r"([0-9]+\.[0-9]+\.[0-9]+)[ \t]+LANGUAGES[ \t]+CXX\)$",
source,
)
if match is None:
raise SystemExit("release-component: cannot read Echoion product version")
print(match.group(1))
PY
)
ECHOION_COMPONENT_SSH_KEY=$ECHOION_COMPONENT_SSH_KEY \
"$script_dir/ci-checkout-components.sh" "$components" "$dependencies"
build_dependencies="$work_root/build-dependencies.json"
python3 - "$root/components.json" "$root/components.lock.json" \
"$component_id" "$build_dependencies" <<'PY'
import json
import pathlib
import sys
registry = json.loads(pathlib.Path(sys.argv[1]).read_text())
lock = json.loads(pathlib.Path(sys.argv[2]).read_text())
component = registry["components"][sys.argv[3]]
result = {}
for dependency in component["dependencies"]:
value = lock["components"][dependency]
result[dependency] = {
"componentVersion": value["componentVersion"],
"revision": value["revision"],
"abiHash": value["abiHash"],
}
pathlib.Path(sys.argv[4]).write_text(
json.dumps(result, sort_keys=True, separators=(",", ":")) + "\n"
)
PY
cmake_arguments=(
-S "$source_root"
-B "$build"
-G Ninja
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX="$install_root"
-DECHOION_COMPONENT_VERSION="$component_version"
-DECHOION_PRODUCT_VERSION="$product_version"
-DCPM_Void_SOURCE="$dependencies/VoidLibrary"
-DCPM_VoidTeamspeak_SOURCE="$dependencies/VoidTeamspeak"
)
while IFS= read -r dependency; do
cmake_arguments+=("-DCPM_${dependency}_SOURCE=$components/$dependency")
done < <(jq -r --arg id "$component_id" '
.components[$id].dependencies | keys[]
' "$root/components.json")
while IFS=$'\t' read -r option value; do
cmake_arguments+=("-D${option}=${value}")
done < <(jq -r '.cmakeOptions | to_entries[] | [.key, .value] | @tsv' \
"$configuration")
cmake "${cmake_arguments[@]}"
cmake --build "$build" --parallel
ctest --test-dir "$build" --output-on-failure
cmake --install "$build"
package_arguments=(
--registry "$root/components.json"
--component "$component_id"
--package-root "$install_root"
--output "$dist"
--component-version "$component_version"
--product-compatibility "$product_compatibility"
--source-sha "$source_sha"
--source-tree-hash "$source_tree_hash"
--build-dependencies "$build_dependencies"
)
if [[ -n $abi_hash ]]; then
package_arguments+=(--abi-hash "$abi_hash")
fi
SOURCE_DATE_EPOCH=$(git show -s --format=%ct HEAD) \
python3 "$script_dir/package-component.py" "${package_arguments[@]}"
manifest=$(find "$dist" -maxdepth 1 -type f -name '*.manifest.json' -print)
archive=$(find "$dist" -maxdepth 1 -type f -name '*.tar.zst' -print)
[[ -f $manifest && -f $archive ]] ||
{
echo "release-component: packaging did not produce one release" >&2
exit 1
}
artifact_name=$(basename "$archive")
verified="$work_root/verified-archive"
if [[ $distribution_backend == gitea-generic ]]; then
artifact_url="${ECHOION_DISTRIBUTION_BASE_URL%/}/$component_id/$component_version/$artifact_name"
upload_response="$work_root/gitea-package-upload-response"
upload_status=$(
curl --silent --show-error \
--output "$upload_response" \
--write-out '%{http_code}' \
--header "Authorization: token $GITEA_RELEASE_TOKEN" \
--upload-file "$archive" \
"$artifact_url"
)
if [[ $upload_status != 201 && $upload_status != 409 ]]; then
echo "release-component: Gitea package upload failed with HTTP $upload_status" >&2
exit 1
fi
curl --fail --silent --show-error \
--header "Authorization: token $GITEA_RELEASE_TOKEN" \
--output "$verified" \
"$artifact_url"
cmp -- "$archive" "$verified" ||
{
echo "release-component: immutable package conflict: $artifact_url" >&2
exit 1
}
else
object_key="${ECHOION_OBJECT_STORAGE_PREFIX:-components}/$component_id/$component_version/$artifact_name"
aws_arguments=()
if [[ -n ${ECHOION_OBJECT_STORAGE_ENDPOINT:-} ]]; then
aws_arguments+=(--endpoint-url "$ECHOION_OBJECT_STORAGE_ENDPOINT")
fi
set +e
aws "${aws_arguments[@]}" s3api put-object \
--bucket "$ECHOION_OBJECT_STORAGE_BUCKET" \
--key "$object_key" \
--body "$archive" \
--if-none-match '*' >/dev/null
upload_status=$?
set -e
if ((upload_status != 0)); then
existing="$work_root/existing-archive"
aws "${aws_arguments[@]}" s3api get-object \
--bucket "$ECHOION_OBJECT_STORAGE_BUCKET" \
--key "$object_key" "$existing" >/dev/null
cmp -- "$archive" "$existing" ||
{
echo "release-component: immutable object conflict: $object_key" >&2
exit 1
}
fi
aws "${aws_arguments[@]}" s3api get-object \
--bucket "$ECHOION_OBJECT_STORAGE_BUCKET" \
--key "$object_key" "$verified" >/dev/null
cmp -- "$archive" "$verified"
fi
if [[ $channel == nightly ]]; then
printf 'Automated %s nightly from %s.\n' "$component_id" "$source_sha" \
>"$work_root/release-notes.md"
fi
owner=${GITEA_REPOSITORY%%/*}
prerelease=()
if [[ $channel != stable ]]; then
prerelease+=(--prerelease)
fi
missing_tag=()
if [[ $channel == nightly ]]; then
missing_tag+=(--allow-missing-draft-tag)
fi
python3 "$script_dir/gitea_release.py" \
--api-url "$GITEA_API_URL" \
publish \
--owner "$owner" \
--repo "$repository_name" \
--tag "$release_tag" \
--target "$source_sha" \
--name "$component_id $component_version" \
--body-file "$work_root/release-notes.md" \
--artifacts "$dist" \
"${prerelease[@]}" \
"${missing_tag[@]}"
python3 "$script_dir/publish-component-release.py" \
--registry "$root/components.json" \
--component "$component_id" \
--manifest "$manifest" \
--archive "$archive" \
--feed-root "$feed_state" \
--base-url "$ECHOION_FEED_BASE_URL" \
--artifact-base-url "$ECHOION_DISTRIBUTION_BASE_URL" \
--channel "$channel" \
--sequence "$sequence" \
--valid-days 45 \
--key-id "$ECHOION_FEED_SIGNING_KEY_ID" \
--private-key "$feed_key"
git -C "$feed_state" config user.name 'Echoion Component Release Bot'
git -C "$feed_state" config user.email 'release-bot@echoion.invalid'
git -C "$feed_state" config gpg.format ssh
git -C "$feed_state" config user.signingkey "$commit_key"
git -C "$feed_state" config commit.gpgsign true
git -C "$feed_state" add feeds state
if ! git -C "$feed_state" diff --cached --quiet; then
git -C "$feed_state" commit -S -m \
"Feed: $component_id $channel $component_version"
git -C "$feed_state" cat-file commit HEAD | grep -q '^gpgsig '
git -C "$feed_state" push origin HEAD:release-feed
fi
git -C "$root" diff --exit-code -- components.lock.json
echo "release-component: published $component_id $component_version"