Skip to content

Commit fa5a62c

Browse files
ryan-williamsclaude
andcommitted
Write env vars to file instead of relying on sudo -E
The nohup + sudo -E approach wasn't reliably passing environment variables to the script. Now we: 1. Write all env vars to /tmp/lambda-gha-scripts/env.sh 2. Source that file before running the setup script Also updated update-lmbda-ssh to just write the HostName (other SSH settings stay in ~/.ssh/config). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ed628e2 commit fa5a62c

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

scripts/update-lmbda-ssh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#!/usr/bin/env bash
22
# Update the lmbda SSH host IP from the current running Lambda instance
33
# Usage: ./scripts/update-lmbda-ssh [instance_id]
4+
#
5+
# Setup: Add to top of ~/.ssh/config:
6+
# Include /Users/ryan/c/oa/lambda-gha/tmp/lmbda-ip.conf
47

58
set -e
69

710
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
811
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
9-
SSH_CONF="$PROJECT_DIR/tmp/lmbda-ssh.conf"
12+
SSH_CONF="$PROJECT_DIR/tmp/lmbda-ip.conf"
1013

1114
# Get instance IP (either by ID or first active instance)
1215
if [ -n "$1" ]; then
@@ -23,17 +26,11 @@ fi
2326
# Create tmp dir if needed
2427
mkdir -p "$PROJECT_DIR/tmp"
2528

26-
# Write SSH config fragment
29+
# Write just the Host block with HostName (other settings in ~/.ssh/config)
2730
cat > "$SSH_CONF" <<EOF
2831
# Auto-generated by scripts/update-lmbda-ssh
29-
# Include this in ~/.ssh/config with: Include $SSH_CONF
3032
Host lmbda
3133
HostName $IP
32-
User ubuntu
33-
IdentitiesOnly yes
34-
IdentityFile ~/.ssh/lambda_ecdsa
35-
StrictHostKeyChecking no
36-
UserKnownHostsFile /dev/null
3734
EOF
3835

3936
echo "Updated $SSH_CONF with IP: $IP"

src/lambda_gha/start.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,26 @@ def execute_setup_via_ssh(
456456
if scp_result.returncode != 0:
457457
raise RuntimeError(f"Failed to SCP {dest_name}: {scp_result.stderr}")
458458

459-
# Build env export commands (add SCRIPTS_DIR for local script access)
459+
# Add SCRIPTS_DIR for local script access
460460
env_vars["SCRIPTS_DIR"] = "/tmp/lambda-gha-scripts"
461-
env_exports = "\n".join(f'export {k}="{v}"' for k, v in env_vars.items())
462461

463-
# Build the setup command: export vars, run script from scripts dir
464-
setup_cmd = f'''
465-
{env_exports}
462+
# Write env vars to a file on the instance (more reliable than sudo -E)
463+
env_file_content = "\n".join(f'export {k}="{v}"' for k, v in env_vars.items())
464+
write_env_cmd = f"cat > /tmp/lambda-gha-scripts/env.sh << 'ENVEOF'\n{env_file_content}\nENVOF"
465+
466+
print(f"Writing environment file to instance...")
467+
env_result = subprocess.run(
468+
["ssh"] + ssh_opts + [f"{ssh_user}@{ip}", write_env_cmd],
469+
capture_output=True,
470+
text=True,
471+
)
472+
if env_result.returncode != 0:
473+
raise RuntimeError(f"Failed to write env file: {env_result.stderr}")
474+
475+
# Build the setup command: source env file, then run script
476+
setup_cmd = '''
466477
chmod +x /tmp/lambda-gha-scripts/*.sh
467-
sudo -E nohup /tmp/lambda-gha-scripts/runner-setup.sh > /var/log/runner-setup.log 2>&1 &
478+
sudo bash -c 'source /tmp/lambda-gha-scripts/env.sh && nohup /tmp/lambda-gha-scripts/runner-setup.sh > /var/log/runner-setup.log 2>&1 &'
468479
'''
469480

470481
print(f"Executing setup script...")

0 commit comments

Comments
 (0)