166 lines
7.2 KiB
YAML
166 lines
7.2 KiB
YAML
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 }}
|
|
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_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
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_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
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_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
|
|
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'"
|