Files
MoShiro/ci/release-linux.sh
T

128 lines
5.0 KiB
Bash

#!/usr/bin/env bash
# Build one Linux architecture/component, then publish its package or OCI image.
set -euo pipefail
arch="${1:?usage: release-linux.sh <amd64|arm64> <agent|probe|backend>}"
component="${2:?usage: release-linux.sh <amd64|arm64> <agent|probe|backend>}"
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
tag="${CI_COMMIT_TAG:-}"
[[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || {
echo "release must run from a vMAJOR.MINOR.PATCH tag" >&2
exit 2
}
version="${tag#v}"
case "$arch" in
amd64) rid=linux-x64; objcopy=llvm-objcopy ;;
arm64) rid=linux-arm64; objcopy=aarch64-linux-gnu-objcopy ;;
*) echo "unsupported architecture: $arch" >&2; exit 2 ;;
esac
case "$component" in
agent|probe|backend) ;;
*) echo "unsupported component: $component" >&2; exit 2 ;;
esac
project_version="$(sed -n 's#.*<Version>\(.*\)</Version>.*#\1#p' "$root/backend/Directory.Build.props" | head -1)"
if [[ "$version" != "$project_version" ]]; then
echo "tag version $version does not match project version $project_version" >&2
exit 1
fi
output="$root/.release/linux-$arch/$component"
build_root="$root/.release/build/linux-$arch-$component"
stage_root="$root/.release/stage/linux-$arch-$component"
mkdir -p "$output" "$build_root" "$stage_root"
publish_aot() {
local name="$1" project="$2" destination="$3"
(cd "$root/backend" && dotnet publish "$project" \
-m:3 \
-c Release -r "$rid" --self-contained true \
-p:PublishAot=true -p:StripSymbols=true -p:DebugType=none \
-p:ObjCopyName="$objcopy" \
--artifacts-path "$build_root" \
-o "$destination")
}
if [[ "$component" == backend ]]; then
[[ -n "${ZOT_USERNAME:-}" && -n "${ZOT_PASSWORD:-}" ]] || {
echo "Zot credentials are required" >&2; exit 2;
}
registry="${ZOT_REGISTRY:-192.168.2.162:5000}"
image="$registry/monitor/monitor-backend"
publish_backend() {
(cd "$root/backend" && dotnet publish src/Monitor.Backend \
-m:3 \
-c Release --no-self-contained \
-p:UseAppHost=false -p:OpenApiGenerateDocuments=false \
--artifacts-path "$build_root" \
-o "$output")
}
publish_backend
container_context="$root/.release/container/linux-$arch"
mkdir -p "$container_context/app" "$container_context/data"
cp -a "$output/." "$container_context/app/"
: > "$container_context/data/.keep"
export REGISTRY_AUTH_FILE="$root/.release/auth/zot-linux-$arch.json"
mkdir -p "$(dirname "$REGISTRY_AUTH_FILE")"
buildah login --tls-verify=false --username "$ZOT_USERNAME" --password-stdin "$registry" <<< "$ZOT_PASSWORD" >/dev/null
local_image="localhost/monitor-backend:$version-$arch"
buildah bud --tls-verify=false --arch "$arch" \
--build-arg "APP_VERSION=$version" \
-f "$root/packaging/container/Backend.Containerfile" \
-t "$local_image" "$container_context" >/dev/null
buildah push --tls-verify=false "$local_image" "docker://$image:$version-$arch" >/dev/null
echo "pushed Zot image $image:$version-$arch"
exit 0
fi
[[ -n "${GITEA_TOKEN:-}" ]] || { echo "GITEA_TOKEN is required" >&2; exit 2; }
gitea_base="${GITEA_BASE:-http://192.168.2.162:3100}"
gitea_host="${gitea_base#https://}"
gitea_host="${gitea_host#http://}"
gitea_host="${gitea_host%%/*}"
gitea_host="${gitea_host%%:*}"
netrc="$(mktemp)"
chmod 600 "$netrc"
printf 'machine %s login justme password %s\n' "$gitea_host" "$GITEA_TOKEN" > "$netrc"
trap 'rm -f "$netrc"' EXIT
upload_package() {
local file="$1" name status
name="$(basename "$file")"
status="$(curl --silent --show-error --retry 3 --netrc-file "$netrc" \
--upload-file "$file" --output /dev/null --write-out '%{http_code}' \
"$gitea_base/api/packages/justme/generic/monitor-node/$version/$name")"
case "$status" in
2*) echo "uploaded Gitea package $name" ;;
409) echo "Gitea package $name already exists; reusing the existing release file" ;;
*) echo "Gitea package upload failed for $name (HTTP $status)" >&2; return 1 ;;
esac
}
stage="$stage_root/$component"
mkdir -p "$stage/systemd"
publish_aot "$component" "$component/src/Monitor.$(tr '[:lower:]' '[:upper:]' <<< "${component:0:1}")${component:1}" "$output/$component"
cp "$output/$component/monitor-$component" "$stage/monitor-$component"
cp "$root/packaging/install.sh" "$stage/install.sh"
cp "$root/LICENSE" "$stage/LICENSE"
cp "$root/backend/$component/README.md" "$stage/README.md"
cp "$root/packaging/systemd/monitor-$component.service" "$stage/systemd/monitor-$component.service"
if [[ "$component" == agent ]]; then
publish_aot fleet-enroll fleet/tools/fleet-enroll "$output/fleet-enroll"
cp "$output/fleet-enroll/fleet-enroll" "$stage/fleet-enroll"
else
mkdir -p "$stage/openwrt"
cp "$root/packaging/openwrt/monitor-probe.init" "$stage/openwrt/"
cp "$root/packaging/openwrt/monitor.keep" "$stage/openwrt/"
fi
archive="$output/monitor-$component-v$version-linux-$arch.tar.gz"
tar -C "$stage" -czf "$archive" .
(cd "$output" && sha256sum "$(basename "$archive")" > "$(basename "$archive").sha256")
upload_package "$archive"
upload_package "$archive.sha256"