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

View File

@@ -0,0 +1,163 @@
name: Deploy control plane
on:
push:
tags:
- "v*.*.*"
permissions:
contents: read
packages: write
jobs:
deploy-production:
name: Build, push, and deploy production
runs-on: ubuntu-latest
timeout-minutes: 45
env:
REGISTRY_HOST: ${{ vars.REGISTRY_HOST }}
REGISTRY_USERNAME: ${{ vars.REGISTRY_USERNAME }}
DEPLOY_HOST: ${{ vars.DEPLOY_HOST }}
DEPLOY_PORT: ${{ vars.DEPLOY_PORT }}
DEPLOY_USER: ${{ vars.DEPLOY_USER }}
DEPLOY_PATH: ${{ vars.DEPLOY_PATH }}
DEPLOY_PROFILES: ${{ vars.DEPLOY_PROFILES }}
steps:
- name: Checkout tag
uses: https://gitea.com/actions/checkout@v4
with:
fetch-depth: 1
- name: Validate deployment configuration
shell: bash
env:
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_SSH_KNOWN_HOSTS: ${{ secrets.DEPLOY_SSH_KNOWN_HOSTS }}
run: |
set -Eeuo pipefail
[[ "$GITHUB_REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || {
printf 'invalid deployment tag: %s\n' "$GITHUB_REF_NAME" >&2
exit 1
}
[[ "$REGISTRY_HOST" =~ ^[A-Za-z0-9.-]+(:[0-9]+)?$ ]] || {
printf 'REGISTRY_HOST must be a registry hostname with an optional port\n' >&2
exit 1
}
[[ "$REGISTRY_USERNAME" =~ ^[A-Za-z0-9._@-]+$ ]] || {
printf 'REGISTRY_USERNAME is invalid\n' >&2
exit 1
}
[[ -n "$REGISTRY_PASSWORD" ]] || { printf 'REGISTRY_PASSWORD is missing\n' >&2; exit 1; }
[[ "$DEPLOY_HOST" =~ ^[A-Za-z0-9.-]+$ ]] || { printf 'DEPLOY_HOST is invalid\n' >&2; exit 1; }
DEPLOY_PORT="${DEPLOY_PORT:-22}"
[[ "$DEPLOY_PORT" =~ ^[0-9]{1,5}$ ]] || { printf 'DEPLOY_PORT is invalid\n' >&2; exit 1; }
[[ "$DEPLOY_USER" =~ ^[A-Za-z_][A-Za-z0-9_-]*$ ]] || { printf 'DEPLOY_USER is invalid\n' >&2; exit 1; }
[[ "$DEPLOY_PATH" =~ ^/[A-Za-z0-9._/-]+$ ]] || { printf 'DEPLOY_PATH must be an absolute path without spaces\n' >&2; exit 1; }
[[ -z "$DEPLOY_PROFILES" || "$DEPLOY_PROFILES" == observability ]] || {
printf 'DEPLOY_PROFILES must be empty or observability\n' >&2
exit 1
}
[[ -n "$DEPLOY_SSH_KEY" ]] || { printf 'DEPLOY_SSH_KEY is missing\n' >&2; exit 1; }
[[ -n "$DEPLOY_SSH_KNOWN_HOSTS" ]] || { printf 'DEPLOY_SSH_KNOWN_HOSTS is missing\n' >&2; exit 1; }
repository_name="${GITHUB_REPOSITORY#*/}"
image_prefix="${REGISTRY_HOST}/${GITHUB_REPOSITORY_OWNER}/${repository_name}"
image_prefix="$(printf '%s' "$image_prefix" | tr '[:upper:]' '[:lower:]')"
printf 'DEPLOY_PORT=%s\n' "$DEPLOY_PORT" >> "$GITHUB_ENV"
printf 'IMAGE_PREFIX=%s\n' "$image_prefix" >> "$GITHUB_ENV"
- name: Login to container registry
shell: bash
env:
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: |
set -Eeuo pipefail
printf '%s' "$REGISTRY_PASSWORD" | docker login "$REGISTRY_HOST" \
--username "$REGISTRY_USERNAME" \
--password-stdin
- name: Test control plane
shell: bash
run: |
set -Eeuo pipefail
docker build --target test control-plane/backend
docker build --target test control-plane/frontend
- name: Build and push immutable images
shell: bash
run: |
set -Eeuo pipefail
docker build --pull \
--tag "${IMAGE_PREFIX}-backend:${GITHUB_REF_NAME}" \
control-plane/backend
docker build --pull \
--tag "${IMAGE_PREFIX}-frontend:${GITHUB_REF_NAME}" \
control-plane/frontend
docker build --pull \
--tag "${IMAGE_PREFIX}-grafana:${GITHUB_REF_NAME}" \
control-plane/grafana
docker push "${IMAGE_PREFIX}-backend:${GITHUB_REF_NAME}"
docker push "${IMAGE_PREFIX}-frontend:${GITHUB_REF_NAME}"
docker push "${IMAGE_PREFIX}-grafana:${GITHUB_REF_NAME}"
- name: Deploy on production server
shell: bash
env:
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_SSH_KNOWN_HOSTS: ${{ secrets.DEPLOY_SSH_KNOWN_HOSTS }}
run: |
set -Eeuo pipefail
ssh_directory="$(mktemp -d "${RUNNER_TEMP:-/tmp}/vps-control-ssh.XXXXXX")"
trap 'rm -rf "$ssh_directory"' EXIT
printf '%s\n' "$DEPLOY_SSH_KEY" > "$ssh_directory/id_ed25519"
printf '%s\n' "$DEPLOY_SSH_KNOWN_HOSTS" > "$ssh_directory/known_hosts"
chmod 0700 "$ssh_directory"
chmod 0600 "$ssh_directory/id_ed25519" "$ssh_directory/known_hosts"
ssh_options=(
-i "$ssh_directory/id_ed25519"
-o BatchMode=yes
-o IdentitiesOnly=yes
-o StrictHostKeyChecking=yes
-o "UserKnownHostsFile=$ssh_directory/known_hosts"
-o ConnectTimeout=10
-p "$DEPLOY_PORT"
)
scp_options=(
-i "$ssh_directory/id_ed25519"
-o BatchMode=yes
-o IdentitiesOnly=yes
-o StrictHostKeyChecking=yes
-o "UserKnownHostsFile=$ssh_directory/known_hosts"
-o ConnectTimeout=10
-P "$DEPLOY_PORT"
)
remote="${DEPLOY_USER}@${DEPLOY_HOST}"
ssh "${ssh_options[@]}" "$remote" "mkdir -p '$DEPLOY_PATH'"
scp "${scp_options[@]}" \
control-plane/docker-compose.yml \
"$remote:$DEPLOY_PATH/docker-compose.yml.next"
scp "${scp_options[@]}" \
control-plane/docker-compose.prod.yml \
"$remote:$DEPLOY_PATH/docker-compose.prod.yml.next"
scp "${scp_options[@]}" \
scripts/deploy-control-plane.sh \
"$remote:$DEPLOY_PATH/deploy-control-plane.sh.next"
ssh "${ssh_options[@]}" "$remote" \
"install -m 0644 '$DEPLOY_PATH/docker-compose.yml.next' '$DEPLOY_PATH/docker-compose.yml' && \
install -m 0644 '$DEPLOY_PATH/docker-compose.prod.yml.next' '$DEPLOY_PATH/docker-compose.prod.yml' && \
install -m 0755 '$DEPLOY_PATH/deploy-control-plane.sh.next' '$DEPLOY_PATH/deploy-control-plane.sh' && \
rm -f '$DEPLOY_PATH/docker-compose.yml.next' '$DEPLOY_PATH/docker-compose.prod.yml.next' '$DEPLOY_PATH/deploy-control-plane.sh.next'"
printf '%s\n' "$REGISTRY_PASSWORD" | ssh "${ssh_options[@]}" "$remote" \
"set -eu; \
auth_dir='$DEPLOY_PATH/.docker-ci'; \
trap 'rm -rf \"\$auth_dir\"' EXIT; \
mkdir -p \"\$auth_dir\"; chmod 0700 \"\$auth_dir\"; \
IFS= read -r registry_password; \
printf '%s' \"\$registry_password\" | docker --config \"\$auth_dir\" login '$REGISTRY_HOST' --username '$REGISTRY_USERNAME' --password-stdin; \
cd '$DEPLOY_PATH'; \
CONTROL_PLANE_PROFILES='$DEPLOY_PROFILES' DOCKER_CONFIG=\"\$auth_dir\" ./deploy-control-plane.sh '$GITHUB_REF_NAME' '$IMAGE_PREFIX'"

View File

@@ -0,0 +1,83 @@
name: Release VPS agent
on:
push:
tags:
- "v*.*.*"
permissions:
contents: read
releases: write
jobs:
release-agent:
name: Build and publish Linux agent
runs-on: ubuntu-latest
timeout-minutes: 45
env:
CARGO_TERM_COLOR: always
RUST_TOOLCHAIN: "1.85.1"
CROSS_VERSION: "0.2.5"
steps:
- name: Checkout tag
uses: https://gitea.com/actions/checkout@v4
with:
fetch-depth: 1
- name: Validate tag and package version
shell: bash
run: |
set -Eeuo pipefail
tag="${GITHUB_REF_NAME}"
if [[ ! "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
printf 'invalid release tag: %s\n' "$tag" >&2
exit 1
fi
package_version="$(awk -F ' *= *' '$1 == "version" {gsub(/"/, "", $2); print $2; exit}' agent/Cargo.toml)"
if [[ "v$package_version" != "$tag" ]]; then
printf 'tag %s does not match agent/Cargo.toml version %s\n' "$tag" "$package_version" >&2
exit 1
fi
- name: Prepare Rust and cross
shell: bash
run: |
set -Eeuo pipefail
for command_name in cargo rustup docker curl jq file sha256sum; do
command -v "$command_name" >/dev/null || {
printf 'runner prerequisite is missing: %s\n' "$command_name" >&2
exit 1
}
done
rustup toolchain install "$RUST_TOOLCHAIN" --profile minimal --no-self-update
rustup override set "$RUST_TOOLCHAIN"
cargo install cross --version "$CROSS_VERSION" --locked
docker info >/dev/null
- name: Build static Linux binaries
shell: bash
run: |
set -Eeuo pipefail
cross build --locked --release --manifest-path agent/Cargo.toml --target x86_64-unknown-linux-musl
cross build --locked --release --manifest-path agent/Cargo.toml --target aarch64-unknown-linux-musl
- name: Package and verify assets
shell: bash
run: |
set -Eeuo pipefail
./scripts/package-agent-release.sh \
--output dist \
--amd64 agent/target/x86_64-unknown-linux-musl/release/vps-agent \
--arm64 agent/target/aarch64-unknown-linux-musl/release/vps-agent
file dist/vps-agent-linux-amd64 dist/vps-agent-linux-arm64
grep -q 'statically linked' < <(file -b dist/vps-agent-linux-amd64)
grep -q 'statically linked' < <(file -b dist/vps-agent-linux-arm64)
- name: Publish Gitea release
shell: bash
env:
GITEA_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
set -Eeuo pipefail
./scripts/publish-gitea-release.sh "$GITHUB_REF_NAME" dist

View File

@@ -76,4 +76,8 @@ Grafana. Grafana never receives an agent address or agent credential.
## Development
See [docs/development.md](docs/development.md). CI-compatible commands are also
listed in [AGENTS.md](AGENTS.md). This repository is licensed under Apache-2.0.
listed in [AGENTS.md](AGENTS.md), and the tag-based Gitea release pipeline is
documented in [docs/gitea-release.md](docs/gitea-release.md). Production
control-plane delivery is documented in
[docs/gitea-deploy.md](docs/gitea-deploy.md). This repository is licensed under
Apache-2.0.

View File

@@ -7,6 +7,10 @@ COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/control-plane .
FROM build AS test
RUN --mount=type=cache,target=/root/.cache/go-build go test ./...
RUN --mount=type=cache,target=/root/.cache/go-build go vet ./...
FROM alpine:3.22
RUN apk add --no-cache ca-certificates tzdata && \
addgroup -S -g 65532 control && adduser -S -D -H -u 65532 -G control control
@@ -14,4 +18,3 @@ COPY --from=build --chown=65532:65532 /out/control-plane /usr/local/bin/control-
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/control-plane"]

View File

@@ -0,0 +1,9 @@
services:
backend:
image: ${CONTROL_PLANE_BACKEND_IMAGE:?CONTROL_PLANE_BACKEND_IMAGE is required}
frontend:
image: ${CONTROL_PLANE_FRONTEND_IMAGE:?CONTROL_PLANE_FRONTEND_IMAGE is required}
grafana:
image: ${CONTROL_PLANE_GRAFANA_IMAGE:?CONTROL_PLANE_GRAFANA_IMAGE is required}

View File

@@ -6,8 +6,12 @@ RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build
FROM build AS test
RUN npm run lint
RUN npm run typecheck
RUN npm test -- --run
FROM nginxinc/nginx-unprivileged:1.27-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build --chown=101:101 /app/dist /usr/share/nginx/html
EXPOSE 8080

127
docs/gitea-deploy.md Normal file
View File

@@ -0,0 +1,127 @@
# Gitea production deployment
`.gitea/workflows/deploy-control-plane.yml` runs for a pushed `vX.X.X` tag. It
runs backend and frontend checks in isolated Docker build stages, builds
immutable backend, frontend, and Grafana images, pushes them to the configured
container registry, copies only the Compose definitions and the deployment
helper over SSH, and waits for all enabled service health checks.
The production host keeps its own `.env`, `secrets/`, PostgreSQL volume, and
reverse-proxy configuration. The workflow never copies those values from CI.
## Gitea configuration
Keep these existing instance-level values:
| Kind | Name | Purpose |
| --- | --- | --- |
| Variable | `REGISTRY_HOST` | Registry hostname with optional port, without `https://` |
| Variable | `REGISTRY_USERNAME` | Registry login/user name |
| Secret | `REGISTRY_PASSWORD` | Gitea PAT with `write:package` access (which includes read) |
| Secret | `RELEASE_TOKEN` | Used by the separate agent release workflow |
Add the following Actions variables:
| Name | Example | Purpose |
| --- | --- | --- |
| `DEPLOY_HOST` | `control.example.net` | SSH hostname or IPv4 address |
| `DEPLOY_PORT` | `22` | SSH port; an empty value defaults to 22 |
| `DEPLOY_USER` | `vps-deploy` | Dedicated deployment user |
| `DEPLOY_PATH` | `/opt/vps-control` | Absolute deployment directory; an existing clone's `control-plane` directory is also valid |
| `DEPLOY_PROFILES` | `observability` | Empty for core services, or `observability` to run Grafana |
Add these Actions secrets:
| Name | Purpose |
| --- | --- |
| `DEPLOY_SSH_KEY` | Complete unencrypted OpenSSH private key for the deployment user |
| `DEPLOY_SSH_KNOWN_HOSTS` | Pinned OpenSSH known-hosts line for the production server |
The workflow asks Gitea for `contents: read` and `packages: write`. Repository or
owner Actions settings must not clamp package access below write. The registry
account must be allowed to push and pull packages for the repository owner.
Protect `v*` tags so they cannot be moved to another commit after publication.
The explicit PAT is intentional: Gitea documents that automatic job-token
authorization for publishing OCI packages is not implemented yet.
## Prepare the production server
Install Docker Engine, the current Docker Compose v2 plugin, `flock` (normally
provided by `util-linux`), and an SSH server. The deployment user needs access
to Docker and must own the deployment directory. Membership in the Docker group
is effectively root-equivalent, so use a dedicated host and key.
Example for Debian or Ubuntu after Docker is installed:
```bash
sudo useradd --create-home --shell /bin/bash vps-deploy
sudo usermod --append --groups docker vps-deploy
sudo install -d -o vps-deploy -g vps-deploy -m 0700 /opt/vps-control
```
Create an SSH key on a trusted administrator machine and install only its public
half on the server:
```bash
ssh-keygen -t ed25519 -f ./gitea-vps-control-deploy -C gitea-vps-control-deploy
ssh-copy-id -i ./gitea-vps-control-deploy.pub vps-deploy@control.example.net
```
Put the private-key file content into `DEPLOY_SSH_KEY`. Obtain the server host
key, verify its fingerprint through a separate trusted channel, and then put
the resulting complete line into `DEPLOY_SSH_KNOWN_HOSTS`:
```bash
ssh-keyscan -p 22 control.example.net
```
On the production server, create `/opt/vps-control/.env` and the secret files
before the first deployment. Their format is described in `control-plane/README.md`:
```bash
cd /opt/vps-control
mkdir -m 0700 secrets
openssl rand -hex 32 > secrets/db_password
openssl rand -base64 24 > secrets/admin_password
openssl rand -hex 32 > secrets/integration_token
openssl rand -base64 24 > secrets/grafana_admin_password
openssl rand -hex 32 > secrets/grafana_secret_key
install -m 0600 /secure/source/control-plane.token secrets/agent_token
chmod 0600 secrets/*
```
Create `.env` from `control-plane/.env.example` and set production bindings,
cookie security, retention, and optional bootstrap values. The pipeline refuses
to deploy when `.env` or `secrets/` is absent.
If Gitea uses a private CA, install that CA in Docker's trust store on both the
runner and production host. Do not disable registry TLS verification.
## Release and rollback behavior
Commit the workflow and deployment files before creating the tag:
```bash
git tag -a v0.1.0 -m "VPS Control v0.1.0"
git push origin main
git push origin v0.1.0
```
Images use immutable version tags:
```text
REGISTRY_HOST/owner/repository-backend:v0.1.0
REGISTRY_HOST/owner/repository-frontend:v0.1.0
REGISTRY_HOST/owner/repository-grafana:v0.1.0
```
The remote helper serializes deployments with `flock`, validates the rendered
Compose configuration, pulls images, and runs `docker compose up --wait`. If a
health check fails and a previous `.release.env` exists, it restores the prior
image set and tries to start it again. Registry credentials are kept in a
temporary Docker config and removed after the SSH session.
Image rollback does not roll back a database schema. Keep tested PostgreSQL
backups before releases that change migrations; forward-only migration rollback
requires restoring the corresponding database backup.

71
docs/gitea-release.md Normal file
View File

@@ -0,0 +1,71 @@
# Gitea release pipeline
The workflow in `.gitea/workflows/release-agent.yml` runs only for pushed tags
matching `v*.*.*`. It then applies a stricter `vX.X.X` check, verifies that the
tag matches the version in `agent/Cargo.toml`, builds static Linux binaries for
amd64 and arm64, creates `SHA256SUMS`, and publishes all three files in a Gitea
release.
## Runner requirements
Register an amd64 Gitea Actions runner whose `ubuntu-latest` label resolves to a
Linux environment with:
- `bash`, `curl`, `jq`, `file`, `sha256sum`, Git, and CA certificates;
- Rustup and Cargo;
- Docker with permission for the runner user to access its daemon;
- outbound access to the Rust registries and the container registry used by
`cross`.
The job installs the pinned Rust 1.85.1 toolchain and `cross` 0.2.5. `cross`
uses Docker to build both musl targets; no amd64 or arm64 build host is required.
Do not expose the Docker daemon over an unauthenticated TCP socket.
The simplest setup is a dedicated runner VM with an `ubuntu-latest:host` label.
If jobs run inside a container instead, that container needs the Docker CLI and
controlled access to the host Docker socket. A runner that executes untrusted
repositories must not share this Docker access.
## Secrets, variables, and permissions
Configure this instance- or repository-level Actions secret:
- `RELEASE_TOKEN` (required): a Gitea access token for a dedicated automation
account with write access to releases in this repository. Store it as a
**secret**, not as a variable.
The workflow requests `contents: read` and `releases: write`. The token owner
must also be allowed to read the repository and create releases. Protect the
release tags so only trusted maintainers or automation can push them.
No custom Actions variables are required. Existing `REGISTRY_HOST`,
`REGISTRY_USERNAME`, and `REGISTRY_PASSWORD` values are deliberately unused:
this pipeline publishes release assets and does not push a container image.
Gitea supplies `GITHUB_API_URL`, `GITHUB_REPOSITORY`, and `GITHUB_REF_NAME` to
the job automatically.
## Publishing a release
First update `agent/Cargo.toml` and commit the change. Then create and push the
matching annotated tag:
```bash
git tag -a v0.1.0 -m "VPS Agent v0.1.0"
git push origin v0.1.0
```
After the job succeeds, the `v0.1.0` release contains:
- `vps-agent-linux-amd64`
- `vps-agent-linux-arm64`
- `SHA256SUMS`
The publisher refuses to overwrite an existing release. If a job fails while
uploading assets, it removes the incomplete release so that the same tag can be
rerun safely. It never deletes the Git tag.
Before installation, verify the downloaded binary:
```bash
sha256sum --check SHA256SUMS
```

131
scripts/deploy-control-plane.sh Executable file
View 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
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[@]}"