From a40469014e82d330a413f607ff2a6f03b2573883 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Wed, 11 Nov 2026 14:57:37 +0100 Subject: [PATCH] CI: deploy met deploy-sleutel (PI_MAIN_SSH_KEY) met terugval op wachtwoord; rssh/rscp-wrappers Co-Authored-By: Claude Sonnet 5.5 --- .gitea/workflows/deploy-scp.yaml | 89 ++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/.gitea/workflows/deploy-scp.yaml b/.gitea/workflows/deploy-scp.yaml index 06ebe0b..b39105b 100644 --- a/.gitea/workflows/deploy-scp.yaml +++ b/.gitea/workflows/deploy-scp.yaml @@ -44,7 +44,19 @@ jobs: steps: # Runner-independent: only installs what is missing, and only uses sudo when not already root and sudo exists # (a container on the PC runner has neither sudo nor a need for it; the Pi runner may need it). - - name: Ensure ssh tooling + # + # Authentication: the Pi-Main host only allows password login from the Pi runner's address, so every runner other + # than that one needs a key. If the secret PI_MAIN_SSH_KEY (private key of the deploy account) is set and works, + # the wrappers `rssh` / `rscp` below use it (-i, IdentitiesOnly, BatchMode); otherwise they fall back to sshpass + # with the password, which keeps working from the Pi runner. The key lives in a temp directory that is removed + # at the end of the job; it is never printed. + - name: Ensure ssh tooling and authentication + env: + SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} + SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} + SSH_PORT: ${{ secrets.PI_MAIN_PORT }} + SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} + SSH_KEY: ${{ secrets.PI_MAIN_SSH_KEY }} run: | SUDO="" if [ "$(id -u)" != "0" ] && command -v sudo >/dev/null 2>&1; then SUDO="sudo"; fi @@ -52,6 +64,43 @@ jobs: $SUDO apt-get update && $SUDO apt-get install -y sshpass openssh-client fi + D=$(mktemp -d) + mkdir -p "$D/bin" + echo "DEPLOY_TMP=$D" >> "$GITHUB_ENV" + echo "$D/bin" >> "$GITHUB_PATH" + + if [ -n "$SSH_KEY" ]; then + printf '%s\n' "$SSH_KEY" > "$D/key" + chmod 600 "$D/key" + if ssh -i "$D/key" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=15 \ + -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" true; then + touch "$D/key_ok" + echo "Authentication: deploy key" + else + echo "Deploy key rejected or host unreachable with it; falling back to password" + fi + else + echo "No deploy key set; using password" + fi + + cat > "$D/bin/rssh" <<'WRAP' + #!/bin/sh + D="$(dirname "$0")/.." + if [ -f "$D/key_ok" ]; then + exec ssh -i "$D/key" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=no -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "$@" + fi + exec sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" "$@" + WRAP + cat > "$D/bin/rscp" <<'WRAP' + #!/bin/sh + D="$(dirname "$0")/.." + if [ -f "$D/key_ok" ]; then + exec scp -i "$D/key" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=no -P "$SSH_PORT" "$@" + fi + exec sshpass -p "$SSH_PASS" scp -o StrictHostKeyChecking=no -P "$SSH_PORT" "$@" + WRAP + chmod +x "$D/bin/rssh" "$D/bin/rscp" + - name: Compute release timestamp id: release run: echo "timestamp=$(date -u +%Y%m%d%H%M%S)" >> "$GITHUB_OUTPUT" @@ -78,10 +127,7 @@ jobs: SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} run: | echo "Environment: ${{ inputs.environment }}" - sshpass -p "$SSH_PASS" ssh \ - -p "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ - "$SSH_USER@$SSH_HOST" \ + rssh \ "bash ~/scripts/backup-slpsoftware-db.sh ${{ inputs.environment }}" # Uploads the published output to a fresh, timestamped release directory rather than @@ -102,14 +148,9 @@ jobs: run: | echo "Environment: ${{ inputs.environment }}, transport: ${{ inputs.transport }}" RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}" - sshpass -p "$SSH_PASS" ssh \ - -p "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ - "$SSH_USER@$SSH_HOST" \ + rssh \ "mkdir -p $RELEASE_DIR" - sshpass -p "$SSH_PASS" scp \ - -P "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ + rscp \ -r ${{ inputs.artifact_name }}/* \ "$SSH_USER@$SSH_HOST:$RELEASE_DIR/" @@ -131,10 +172,7 @@ jobs: SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} run: | RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}" - sshpass -p "$SSH_PASS" ssh \ - -p "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ - "$SSH_USER@$SSH_HOST" \ + rssh \ "mkdir -p ${{ inputs.deploy_path }}/shared/wwwroot-web && \ mkdir -p $RELEASE_DIR/wwwroot && \ rm -rf $RELEASE_DIR/wwwroot/web && \ @@ -158,10 +196,7 @@ jobs: SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} run: | RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}" - sshpass -p "$SSH_PASS" ssh \ - -p "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ - "$SSH_USER@$SSH_HOST" \ + rssh \ "mkdir -p ${{ inputs.deploy_path }}/shared/modules && \ ls ${{ inputs.deploy_path }}/shared/modules/*.dll 2>/dev/null | while read -r f; do \ name=\$(basename \"\$f\"); \ @@ -180,10 +215,7 @@ jobs: SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} run: | RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}" - sshpass -p "$SSH_PASS" ssh \ - -p "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ - "$SSH_USER@$SSH_HOST" \ + rssh \ "ln -sfn $RELEASE_DIR ${{ inputs.deploy_path }}/current && \ systemctl --user restart ${{ inputs.service_name }}" @@ -214,8 +246,9 @@ jobs: SSH_PORT: ${{ secrets.PI_MAIN_PORT }} SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} run: | - sshpass -p "$SSH_PASS" ssh \ - -p "$SSH_PORT" \ - -o StrictHostKeyChecking=no \ - "$SSH_USER@$SSH_HOST" \ + rssh \ "cd ${{ inputs.deploy_path }}/releases && ls -1t | tail -n +3 | xargs -r rm -rf" + + - name: Remove deploy key + if: always() + run: rm -rf "${DEPLOY_TMP:-/nonexistent}"