Skip to content

Instantly share code, notes, and snippets.

@sgoger
Last active November 28, 2017 08:18
Show Gist options
  • Save sgoger/fd40dd270e54376b2657d4f73b859a10 to your computer and use it in GitHub Desktop.
Save sgoger/fd40dd270e54376b2657d4f73b859a10 to your computer and use it in GitHub Desktop.
SSH_AUTH_SOCK wrapper and error handling for CI usage of Docker

Description

This script allows error handling during a CI build. It launches an SSH Agent, executes your custom commands and then stops the SSH Agent. In case of an error, the script and the Agent are immediately stopped.

Usage

./run.sh "all my commands"

/!\ Note the quotes around the commands to run /!\

Example:

./run.sh "make install && make build && make release"
#!/bin/sh
# Credit going mostly to http://stackoverflow.com/a/22224317
# A lot of weird error handling glyphs... Don't worry, just read the comments, and you'll be fine. ;)
# ===================================================================================================
abort()
{
echo >&2 '
***************
*** ABORTED ***
***************
'
# That's the `finally` part of the script. Feel free to edit it.
# ==============================================================
ssh-agent -k || true
# Stop editing this function after this line.
# ===========================================
echo "An error occurred. Exiting..." >&2
exit 1
}
trap 'abort' 0
set -e
# Real script goes here. All other code is just some error handling to ensure SSH Agent
# is killed correctly at the end of the script.
# =====================================================================================
echo "Starting SSH Agent..."
eval `ssh-agent -s`
echo "add key"
ssh-add ~/.ssh/id_rsa
chmod 777 $SSH_AUTH_SOCK
echo "SSH Agent started!"
echo "Executing $@"
eval "$@"
# Do not modify under this line or sky would fall over your head.
# ===============================================================
trap : 0
echo >&2 '
************
*** DONE ***
************
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment