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