add cicd
Some checks failed
Release VPS agent / Build and publish Linux agent (push) Failing after 22s
Deploy control plane / Build, push, and deploy production (push) Failing after 22s

This commit is contained in:
2026-09-23 22:46:01 +03:00
parent 0fc35589e6
commit a10c2de01d
10 changed files with 743 additions and 3 deletions

145
scripts/publish-gitea-release.sh Executable file
View File

@@ -0,0 +1,145 @@
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
usage() {
printf 'Usage: %s TAG ARTIFACT_DIRECTORY\n' "$0" >&2
}
[[ $# -eq 2 ]] || { usage; exit 2; }
TAG="$1"
ARTIFACT_DIRECTORY="$2"
API_URL="${GITEA_API_URL:-${GITHUB_API_URL:-}}"
REPOSITORY="${GITEA_REPOSITORY:-${GITHUB_REPOSITORY:-}}"
TOKEN="${GITEA_TOKEN:-}"
[[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || {
printf 'refusing to publish invalid release tag: %s\n' "$TAG" >&2
exit 2
}
[[ "$API_URL" =~ ^https?://[^/]+(/.*)?$ ]] || {
printf 'GITEA_API_URL or GITHUB_API_URL must be an HTTP(S) URL\n' >&2
exit 2
}
[[ "$REPOSITORY" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]] || {
printf 'GITEA_REPOSITORY or GITHUB_REPOSITORY must be owner/repository\n' >&2
exit 2
}
[[ -n "$TOKEN" ]] || { printf 'GITEA_TOKEN is required\n' >&2; exit 2; }
[[ -d "$ARTIFACT_DIRECTORY" ]] || {
printf 'artifact directory does not exist: %s\n' "$ARTIFACT_DIRECTORY" >&2
exit 2
}
for command_name in curl jq sha256sum; do
command -v "$command_name" >/dev/null || {
printf '%s is required\n' "$command_name" >&2
exit 1
}
done
ARTIFACT_DIRECTORY="$(cd "$ARTIFACT_DIRECTORY" && pwd -P)"
REQUIRED_ASSETS=(
vps-agent-linux-amd64
vps-agent-linux-arm64
SHA256SUMS
)
for asset_name in "${REQUIRED_ASSETS[@]}"; do
[[ -f "$ARTIFACT_DIRECTORY/$asset_name" ]] || {
printf 'required release asset is missing: %s\n' "$asset_name" >&2
exit 1
}
done
(
cd "$ARTIFACT_DIRECTORY"
sha256sum --check SHA256SUMS
)
API_URL="${API_URL%/}"
RELEASES_URL="$API_URL/repos/$REPOSITORY/releases"
RESPONSE_FILE="$(mktemp)"
RELEASE_ID=""
RELEASE_CREATED=false
cleanup() {
local exit_status=$?
rm -f "$RESPONSE_FILE"
if [[ $exit_status -ne 0 && "$RELEASE_CREATED" == true && -n "$RELEASE_ID" ]]; then
printf 'publication failed; removing incomplete release %s\n' "$TAG" >&2
curl --silent --show-error --fail \
--request DELETE \
--header "Authorization: token $TOKEN" \
"$RELEASES_URL/$RELEASE_ID" >/dev/null || true
fi
exit "$exit_status"
}
trap cleanup EXIT
status_code="$(curl --silent --show-error \
--output "$RESPONSE_FILE" \
--write-out '%{http_code}' \
--header "Authorization: token $TOKEN" \
"$RELEASES_URL/tags/$TAG")"
case "$status_code" in
404) ;;
200)
printf 'release %s already exists; refusing to overwrite it\n' "$TAG" >&2
exit 1
;;
*)
printf 'failed to check existing release (HTTP %s): %s\n' \
"$status_code" "$(jq -r '.message // .error // "unknown API error"' "$RESPONSE_FILE" 2>/dev/null || true)" >&2
exit 1
;;
esac
RELEASE_BODY="${GITEA_RELEASE_BODY:-Automated vps-agent release for $TAG. Verify downloaded binaries with SHA256SUMS before installation.}"
release_payload="$(jq -n \
--arg tag "$TAG" \
--arg name "VPS Agent $TAG" \
--arg body "$RELEASE_BODY" \
'{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')"
status_code="$(curl --silent --show-error \
--output "$RESPONSE_FILE" \
--write-out '%{http_code}' \
--request POST \
--header "Authorization: token $TOKEN" \
--header 'Content-Type: application/json' \
--data "$release_payload" \
"$RELEASES_URL")"
if [[ "$status_code" != 201 ]]; then
printf 'failed to create release (HTTP %s): %s\n' \
"$status_code" "$(jq -r '.message // .error // "unknown API error"' "$RESPONSE_FILE" 2>/dev/null || true)" >&2
exit 1
fi
RELEASE_ID="$(jq -er '.id' "$RESPONSE_FILE")"
RELEASE_CREATED=true
ASSETS=("${REQUIRED_ASSETS[@]}")
if [[ -f "$ARTIFACT_DIRECTORY/SHA256SUMS.minisig" ]]; then
ASSETS+=(SHA256SUMS.minisig)
fi
for asset_name in "${ASSETS[@]}"; do
status_code="$(curl --silent --show-error \
--output "$RESPONSE_FILE" \
--write-out '%{http_code}' \
--request POST \
--header "Authorization: token $TOKEN" \
--form "attachment=@$ARTIFACT_DIRECTORY/$asset_name" \
"$RELEASES_URL/$RELEASE_ID/assets?name=$asset_name")"
if [[ "$status_code" != 201 ]]; then
printf 'failed to upload %s (HTTP %s): %s\n' \
"$asset_name" "$status_code" \
"$(jq -r '.message // .error // "unknown API error"' "$RESPONSE_FILE" 2>/dev/null || true)" >&2
exit 1
fi
printf 'uploaded %s\n' "$asset_name"
done
RELEASE_CREATED=false
printf 'published release %s with %d assets\n' "$TAG" "${#ASSETS[@]}"