Skip to content

Commit

Permalink
Merge pull request #1 from tmshkr/dev
Browse files Browse the repository at this point in the history
v1.2
  • Loading branch information
tmshkr authored Nov 28, 2023
2 parents 6853019 + b760cee commit 246aba5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .ssh/config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AllowUsers $USER
AuthorizedKeysFile $ssh_dir/authorized_keys
HostKey $ssh_dir/ssh_host_rsa_key
HostKey $ssh_dir/ssh_host_key
PidFile $ssh_dir/sshd.pid
Port $INPUT_SSH_PORT
SetEnv GITHUB_WORKSPACE="$GITHUB_WORKSPACE"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Installs ngrok and opens an SSH tunnel into your GitHub Actions runner.

Useful for debugging builds and previewing your app on a live server.
Useful for debugging builds, previewing your app on a live server, and managing concurrent workflows.

## Inputs/Outputs

Expand All @@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: tmshkr/ngrok-ssh@v1.1
- uses: tmshkr/ngrok-ssh@v1.2
with:
NGROK_AUTHTOKEN: ${{ secrets.NGROK_AUTHTOKEN }}
NGROK_CONFIG_FILE: "ngrok.yml"
Expand Down
19 changes: 13 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: ngrok-ssh
description: SSH into your GitHub Actions runner with ngrok.
inputs:
BASH_PROFILE:
description: "Path to a text file to append to the host's .bash_profile."
required: false
NGROK_AUTHTOKEN:
description: "Your ngrok auth token, required for TCP tunnels."
required: true
Expand All @@ -15,8 +18,14 @@ inputs:
description: "SSH port to use with ngrok."
required: false
default: 2222
SSH_PUBLIC_KEY:
description: "SSH public key to add to the authorized_keys file."
SSH_CLIENT_PUBLIC_KEY:
description: "Public key of an SSH client to add to the authorized_keys file."
required: false
SSH_HOST_PRIVATE_KEY:
description: "Private key to use for the SSH host."
required: false
SSH_HOST_PUBLIC_KEY:
description: "Public key to use for the SSH host."
required: false
USE_GITHUB_ACTOR_KEY:
description: "Whether to add the GITHUB_ACTOR's public keys to the authorized_keys file."
Expand All @@ -28,10 +37,8 @@ inputs:
default: false
outputs:
NGROK_TUNNELS:
description: "JSON string of provisioned ngrok tunnels from the /api/tunnels endpoint, including any additional configured tunnels."
SSH_COMMAND:
description: "The command to use to SSH into the runner."
SSH_HOST:
description: "JSON representation of provisioned ngrok tunnels from the /api/tunnels endpoint, including any additional configured tunnels."
SSH_HOSTNAME:
description: "The hostname of the SSH server."
SSH_HOST_PUBLIC_KEY:
description: "The public key of the SSH host."
Expand Down
63 changes: 32 additions & 31 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export ngrok_dir="$HOME/.ngrok"
mkdir -m 700 $ssh_dir
mkdir -m 700 $ngrok_dir

echo "Configuring sshd..."
envsubst <"$ACTION_PATH/.ssh/config" >"$ssh_dir/config"
envsubst <"$ACTION_PATH/.ssh/rc" >"$ssh_dir/rc" '$ssh_dir'
echo "cd $GITHUB_WORKSPACE" >>"$HOME/.bash_profile"

echo "Configuring ngrok..."
envsubst <"$ACTION_PATH/.ngrok/ngrok.yml" >"$ngrok_dir/ngrok.yml"
ngrok_config="$ngrok_dir/ngrok.yml"
Expand All @@ -29,6 +24,26 @@ if [ -z "$INPUT_NGROK_AUTHTOKEN" ]; then
exit 1
fi

echo "Configuring sshd..."
envsubst <"$ACTION_PATH/.ssh/config" >"$ssh_dir/config"
envsubst <"$ACTION_PATH/.ssh/rc" >"$ssh_dir/rc" '$ssh_dir'
echo "cd $GITHUB_WORKSPACE" >>"$HOME/.bash_profile"

if [ -n "$INPUT_BASH_PROFILE" ]; then
echo "Adding custom bash_profile..."
cat "$GITHUB_WORKSPACE/$INPUT_BASH_PROFILE" >>"$HOME/.bash_profile"
fi

if [ -n "$INPUT_SSH_HOST_PRIVATE_KEY" ] && [ -n "$INPUT_SSH_HOST_PUBLIC_KEY" ]; then
echo "Setting SSH host's public and private keys..."
cat "$INPUT_SSH_HOST_PRIVATE_KEY" >>"$ssh_dir/ssh_host_key"
cat "$INPUT_SSH_HOST_PUBLIC_KEY" >>"$ssh_dir/ssh_host_key.pub"
else
echo "Generating SSH host's public and private keys..."
ssh-keygen -q -t rsa -f "$ssh_dir/ssh_host_key" -N ''
fi
echo SSH_HOST_PUBLIC_KEY=$(cat "$ssh_dir/ssh_host_key.pub") >>"$GITHUB_OUTPUT"

# Setup ssh login credentials
if [ "$INPUT_USE_GITHUB_ACTOR_KEY" == true ]; then
curl -s "https://api.github.com/users/$GITHUB_ACTOR/keys" | jq -r '.[].key' >>"$ssh_dir/authorized_keys"
Expand All @@ -40,8 +55,8 @@ if [ "$INPUT_USE_GITHUB_ACTOR_KEY" == true ]; then
fi
fi

if [ -n "$INPUT_SSH_PUBLIC_KEY" ]; then
echo "$INPUT_SSH_PUBLIC_KEY" >>"$ssh_dir/authorized_keys"
if [ -n "$INPUT_SSH_CLIENT_PUBLIC_KEY" ]; then
echo "$INPUT_SSH_CLIENT_PUBLIC_KEY" >>"$ssh_dir/authorized_keys"
fi

if ! grep -q . "$ssh_dir/authorized_keys" || [ "$INPUT_SET_RANDOM_PASSWORD" == true ]; then
Expand All @@ -62,9 +77,6 @@ if ! command -v "ngrok" >/dev/null 2>&1; then
rm ngrok-v3-stable-linux-amd64.tgz
fi

echo 'Creating SSH server key...'
ssh-keygen -q -f "$ssh_dir/ssh_host_rsa_key" -N ''

echo "Starting SSH server..."
/usr/sbin/sshd -f "$ssh_dir/config"

Expand All @@ -73,19 +85,27 @@ ngrok start --all --config "$ngrok_config" --log "$ngrok_dir/ngrok.log" >/dev/nu

# Get ngrok tunnels and print them
tunnels="$(curl -s --retry-connrefused --retry 10 http://localhost:4040/api/tunnels)"
echo "NGROK_TUNNELS=$(echo $tunnels | jq -c '.tunnels | map(del(.config, .metrics))')" >>"$GITHUB_OUTPUT"

print_tunnels() {
echo $tunnels | jq -c '.tunnels[]' | while read tunnel; do
local tunnel_name=$(echo $tunnel | jq -r ".name")
local tunnel_url=$(echo $tunnel | jq -r ".public_url")
if [ "$tunnel_name" = "ssh" ]; then
local hostname=$(echo $tunnel_url | cut -d'/' -f3 | cut -d':' -f1)
local port=$(echo $tunnel_url | cut -d':' -f3)
hostname=$(echo $tunnel_url | cut -d'/' -f3 | cut -d':' -f1)
port=$(echo $tunnel_url | cut -d':' -f3)

echo "SSH_HOSTNAME=$hostname" >>"$GITHUB_OUTPUT"
echo "SSH_PORT=$port" >>"$GITHUB_OUTPUT"
echo "SSH_USER=$USER" >>"$GITHUB_OUTPUT"

echo "*********************************"
printf "\n"
echo "SSH command:"
echo "ssh $USER@$hostname -p $port"
printf "\n"
if [ -n "$random_password" ]; then
echo "SSH_PASSWORD=$random_password" >>"$GITHUB_OUTPUT"
echo "Random password:"
echo "$random_password"
printf "\n"
Expand All @@ -108,22 +128,3 @@ while true; do
echo "Waiting for SSH user to login..."
sleep 5
done

# Set outputs
echo "NGROK_TUNNELS=$(echo $tunnels | jq -c '.tunnels | map(del(.config, .metrics)) | tostring')" >>"$GITHUB_OUTPUT"
echo SSH_HOST_PUBLIC_KEY=\"$(cat "$ssh_dir/ssh_host_rsa_key.pub")\" >>"$GITHUB_OUTPUT"
echo $tunnels | jq -c '.tunnels[]' | while read tunnel; do
tunnel_name=$(echo $tunnel | jq -r ".name")
tunnel_url=$(echo $tunnel | jq -r ".public_url")
if [ "$tunnel_name" = "ssh" ]; then
hostname=$(echo $tunnel_url | cut -d'/' -f3 | cut -d':' -f1)
port=$(echo $tunnel_url | cut -d':' -f3)
echo "SSH_COMMAND=\"ssh $USER@$hostname -p $port\"" >>"$GITHUB_OUTPUT"
echo "SSH_HOST=\"$hostname\"" >>"$GITHUB_OUTPUT"
echo "SSH_PORT=\"$port\"" >>"$GITHUB_OUTPUT"
echo "SSH_USER=\"$USER\"" >>"$GITHUB_OUTPUT"
if [ -n "$random_password" ]; then
echo "SSH_PASSWORD=\"$random_password\"" >>"$GITHUB_OUTPUT"
fi
fi
done

0 comments on commit 246aba5

Please sign in to comment.