Skip to content

Instantly share code, notes, and snippets.

@yakkun
Last active April 16, 2024 03:25
Show Gist options
  • Save yakkun/3ad4a838a98a649ae6f0f3e383b3ba06 to your computer and use it in GitHub Desktop.
Save yakkun/3ad4a838a98a649ae6f0f3e383b3ba06 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
authorized_keys_file=${HOME}/.ssh/authorized_keys
tmp_authorized_keys_file=$(mktemp)
github_username='yakkun'
# Fetch authorized_keys from GitHub
curl -fsSL https://github.com/${github_username}.keys -o ${tmp_authorized_keys_file}
# Whether diff is available?
set +e
diff -q ${authorized_keys_file} ${tmp_authorized_keys_file} >/dev/null
if [ $? -ne 1 ]; then
echo 'No diffs, no deploy.'
exit 0
fi
set -e
# Validate contents if not empty
if [ -s ${tmp_authorized_keys_file} ]; then
if [ $(head -c 4 ${tmp_authorized_keys_file}) -ne 'ssh-' ]; then
echo 'Seems like not ssh authorized_key file, aborted.' >&2
exit 1
fi
fi
# Deploy
set +e
diff -u ${authorized_keys_file} ${tmp_authorized_keys_file}
set -e
cat ${tmp_authorized_keys_file} >${authorized_keys_file}
echo 'Deployed with diffs above.'
rm ${tmp_authorized_keys_file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment