add cicd
This commit is contained in:
131
scripts/deploy-control-plane.sh
Executable file
131
scripts/deploy-control-plane.sh
Executable file
@@ -0,0 +1,131 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
IFS=$'\n\t'
|
||||
umask 077
|
||||
|
||||
usage() {
|
||||
printf 'Usage: %s TAG IMAGE_PREFIX\n' "$0" >&2
|
||||
}
|
||||
|
||||
[[ $# -eq 2 ]] || { usage; exit 2; }
|
||||
|
||||
TAG="$1"
|
||||
IMAGE_PREFIX="$2"
|
||||
PROFILES="${CONTROL_PLANE_PROFILES:-}"
|
||||
|
||||
[[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || {
|
||||
printf 'invalid deployment tag: %s\n' "$TAG" >&2
|
||||
exit 2
|
||||
}
|
||||
[[ "$IMAGE_PREFIX" =~ ^[a-z0-9][a-z0-9./:_-]*[a-z0-9]$ ]] || {
|
||||
printf 'invalid container image prefix\n' >&2
|
||||
exit 2
|
||||
}
|
||||
[[ -z "$PROFILES" || "$PROFILES" == observability ]] || {
|
||||
printf 'CONTROL_PLANE_PROFILES must be empty or observability\n' >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
for command_name in docker flock mktemp; do
|
||||
command -v "$command_name" >/dev/null || {
|
||||
printf '%s is required on the deployment server\n' "$command_name" >&2
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
docker compose version >/dev/null
|
||||
|
||||
for required_path in .env docker-compose.yml docker-compose.prod.yml secrets; do
|
||||
[[ -e "$required_path" ]] || {
|
||||
printf 'required deployment path is missing: %s/%s\n' "$PWD" "$required_path" >&2
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
|
||||
exec 9>.deploy.lock
|
||||
if ! flock -n 9; then
|
||||
printf 'another control-plane deployment is already running\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
NEXT_ENV="$(mktemp .release.env.next.XXXXXX)"
|
||||
cleanup() {
|
||||
rm -f "$NEXT_ENV"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
printf '%s\n' \
|
||||
"CONTROL_PLANE_BACKEND_IMAGE=${IMAGE_PREFIX}-backend:${TAG}" \
|
||||
"CONTROL_PLANE_FRONTEND_IMAGE=${IMAGE_PREFIX}-frontend:${TAG}" \
|
||||
"CONTROL_PLANE_GRAFANA_IMAGE=${IMAGE_PREFIX}-grafana:${TAG}" \
|
||||
> "$NEXT_ENV"
|
||||
|
||||
PROFILE_ARGS=()
|
||||
SERVICES=(postgres backend frontend)
|
||||
if [[ "$PROFILES" == observability ]]; then
|
||||
PROFILE_ARGS=(--profile observability)
|
||||
SERVICES+=(grafana)
|
||||
fi
|
||||
|
||||
compose() {
|
||||
local release_env="$1"
|
||||
shift
|
||||
docker compose \
|
||||
--env-file .env \
|
||||
--env-file "$release_env" \
|
||||
-f docker-compose.yml \
|
||||
-f docker-compose.prod.yml \
|
||||
"${PROFILE_ARGS[@]}" \
|
||||
"$@"
|
||||
}
|
||||
|
||||
# Validate all interpolation and secret paths before changing the active tag.
|
||||
compose "$NEXT_ENV" config --quiet
|
||||
|
||||
HAD_PREVIOUS=false
|
||||
if [[ -f .release.env ]]; then
|
||||
cp -f .release.env .release.env.previous
|
||||
HAD_PREVIOUS=true
|
||||
fi
|
||||
mv -f "$NEXT_ENV" .release.env
|
||||
NEXT_ENV=""
|
||||
|
||||
printf 'pulling control-plane images for %s\n' "$TAG"
|
||||
compose .release.env pull backend frontend
|
||||
if [[ "$PROFILES" == observability ]]; then
|
||||
compose .release.env pull grafana
|
||||
fi
|
||||
|
||||
printf 'deploying control plane %s\n' "$TAG"
|
||||
if compose .release.env up \
|
||||
--detach \
|
||||
--no-build \
|
||||
--remove-orphans \
|
||||
--wait \
|
||||
--wait-timeout 180 \
|
||||
"${SERVICES[@]}"; then
|
||||
compose .release.env ps
|
||||
printf 'control plane %s is healthy\n' "$TAG"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
printf 'deployment %s failed health checks\n' "$TAG" >&2
|
||||
if [[ "$HAD_PREVIOUS" == true ]]; then
|
||||
printf 'restoring the previous container image set\n' >&2
|
||||
cp -f .release.env.previous .release.env
|
||||
compose .release.env pull backend frontend || true
|
||||
if [[ "$PROFILES" == observability ]]; then
|
||||
compose .release.env pull grafana || true
|
||||
fi
|
||||
if ! compose .release.env up \
|
||||
--detach \
|
||||
--no-build \
|
||||
--remove-orphans \
|
||||
--wait \
|
||||
--wait-timeout 180 \
|
||||
"${SERVICES[@]}"; then
|
||||
printf 'automatic image rollback also failed; operator action is required\n' >&2
|
||||
fi
|
||||
else
|
||||
printf 'no previous release metadata exists; automatic rollback is unavailable\n' >&2
|
||||
fi
|
||||
exit 1
|
||||
145
scripts/publish-gitea-release.sh
Executable file
145
scripts/publish-gitea-release.sh
Executable 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[@]}"
|
||||
Reference in New Issue
Block a user