CI: deploy met deploy-sleutel (PI_MAIN_SSH_KEY) met terugval op wachtwoord; rssh/rscp-wrappers
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -44,7 +44,19 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
# Runner-independent: only installs what is missing, and only uses sudo when not already root and sudo exists
|
# 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).
|
# (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: |
|
run: |
|
||||||
SUDO=""
|
SUDO=""
|
||||||
if [ "$(id -u)" != "0" ] && command -v sudo >/dev/null 2>&1; then SUDO="sudo"; fi
|
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
|
$SUDO apt-get update && $SUDO apt-get install -y sshpass openssh-client
|
||||||
fi
|
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
|
- name: Compute release timestamp
|
||||||
id: release
|
id: release
|
||||||
run: echo "timestamp=$(date -u +%Y%m%d%H%M%S)" >> "$GITHUB_OUTPUT"
|
run: echo "timestamp=$(date -u +%Y%m%d%H%M%S)" >> "$GITHUB_OUTPUT"
|
||||||
@@ -78,10 +127,7 @@ jobs:
|
|||||||
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
||||||
run: |
|
run: |
|
||||||
echo "Environment: ${{ inputs.environment }}"
|
echo "Environment: ${{ inputs.environment }}"
|
||||||
sshpass -p "$SSH_PASS" ssh \
|
rssh \
|
||||||
-p "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
"$SSH_USER@$SSH_HOST" \
|
|
||||||
"bash ~/scripts/backup-slpsoftware-db.sh ${{ inputs.environment }}"
|
"bash ~/scripts/backup-slpsoftware-db.sh ${{ inputs.environment }}"
|
||||||
|
|
||||||
# Uploads the published output to a fresh, timestamped release directory rather than
|
# Uploads the published output to a fresh, timestamped release directory rather than
|
||||||
@@ -102,14 +148,9 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo "Environment: ${{ inputs.environment }}, transport: ${{ inputs.transport }}"
|
echo "Environment: ${{ inputs.environment }}, transport: ${{ inputs.transport }}"
|
||||||
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
||||||
sshpass -p "$SSH_PASS" ssh \
|
rssh \
|
||||||
-p "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
"$SSH_USER@$SSH_HOST" \
|
|
||||||
"mkdir -p $RELEASE_DIR"
|
"mkdir -p $RELEASE_DIR"
|
||||||
sshpass -p "$SSH_PASS" scp \
|
rscp \
|
||||||
-P "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
-r ${{ inputs.artifact_name }}/* \
|
-r ${{ inputs.artifact_name }}/* \
|
||||||
"$SSH_USER@$SSH_HOST:$RELEASE_DIR/"
|
"$SSH_USER@$SSH_HOST:$RELEASE_DIR/"
|
||||||
|
|
||||||
@@ -131,10 +172,7 @@ jobs:
|
|||||||
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
||||||
run: |
|
run: |
|
||||||
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
||||||
sshpass -p "$SSH_PASS" ssh \
|
rssh \
|
||||||
-p "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
"$SSH_USER@$SSH_HOST" \
|
|
||||||
"mkdir -p ${{ inputs.deploy_path }}/shared/wwwroot-web && \
|
"mkdir -p ${{ inputs.deploy_path }}/shared/wwwroot-web && \
|
||||||
mkdir -p $RELEASE_DIR/wwwroot && \
|
mkdir -p $RELEASE_DIR/wwwroot && \
|
||||||
rm -rf $RELEASE_DIR/wwwroot/web && \
|
rm -rf $RELEASE_DIR/wwwroot/web && \
|
||||||
@@ -158,10 +196,7 @@ jobs:
|
|||||||
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
||||||
run: |
|
run: |
|
||||||
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
||||||
sshpass -p "$SSH_PASS" ssh \
|
rssh \
|
||||||
-p "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
"$SSH_USER@$SSH_HOST" \
|
|
||||||
"mkdir -p ${{ inputs.deploy_path }}/shared/modules && \
|
"mkdir -p ${{ inputs.deploy_path }}/shared/modules && \
|
||||||
ls ${{ inputs.deploy_path }}/shared/modules/*.dll 2>/dev/null | while read -r f; do \
|
ls ${{ inputs.deploy_path }}/shared/modules/*.dll 2>/dev/null | while read -r f; do \
|
||||||
name=\$(basename \"\$f\"); \
|
name=\$(basename \"\$f\"); \
|
||||||
@@ -180,10 +215,7 @@ jobs:
|
|||||||
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
||||||
run: |
|
run: |
|
||||||
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
RELEASE_DIR="${{ inputs.deploy_path }}/releases/${{ steps.release.outputs.timestamp }}"
|
||||||
sshpass -p "$SSH_PASS" ssh \
|
rssh \
|
||||||
-p "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
"$SSH_USER@$SSH_HOST" \
|
|
||||||
"ln -sfn $RELEASE_DIR ${{ inputs.deploy_path }}/current && \
|
"ln -sfn $RELEASE_DIR ${{ inputs.deploy_path }}/current && \
|
||||||
systemctl --user restart ${{ inputs.service_name }}"
|
systemctl --user restart ${{ inputs.service_name }}"
|
||||||
|
|
||||||
@@ -214,8 +246,9 @@ jobs:
|
|||||||
SSH_PORT: ${{ secrets.PI_MAIN_PORT }}
|
SSH_PORT: ${{ secrets.PI_MAIN_PORT }}
|
||||||
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }}
|
||||||
run: |
|
run: |
|
||||||
sshpass -p "$SSH_PASS" ssh \
|
rssh \
|
||||||
-p "$SSH_PORT" \
|
|
||||||
-o StrictHostKeyChecking=no \
|
|
||||||
"$SSH_USER@$SSH_HOST" \
|
|
||||||
"cd ${{ inputs.deploy_path }}/releases && ls -1t | tail -n +3 | xargs -r rm -rf"
|
"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}"
|
||||||
|
|||||||
Reference in New Issue
Block a user