|
| 1 | +# 🚀 Remote Debug Workflow - SSH Tunnel Edition |
| 2 | +# =============================================== |
| 3 | +# This workflow creates a secure SSH tunnel for remote debugging sessions |
| 4 | +# |
| 5 | +# ✨ Features: |
| 6 | +# • 🔐 Secure SSH access to build environments |
| 7 | +# • 🐛 Pre-installed Go debugger (Delve) |
| 8 | +# • 🌍 Multi-platform support (Linux, macOS, Windows) |
| 9 | +# • 🔄 Port forwarding for seamless debug sessions |
| 10 | +# • 🐳 Docker sidecar for enhanced compatibility |
| 11 | +# • 📁 Persistent Go environment configuration |
| 12 | + |
| 13 | +name: 🚧 Debug with SSH |
| 14 | + |
| 15 | +on: |
| 16 | + workflow_dispatch: |
| 17 | + inputs: |
| 18 | + os: |
| 19 | + description: '🖥️ Select your debugging platform' |
| 20 | + required: false |
| 21 | + type: choice |
| 22 | + options: |
| 23 | + - '🐧 Ubuntu LTS (amd64)' |
| 24 | + - '🐧 Ubuntu LTS (arm64)' |
| 25 | + - '🍎 macOS Latest (arm64)' |
| 26 | + - '🍎 macOS 13 (amd64)' |
| 27 | + - '🪟 Windows Latest (amd64)' |
| 28 | + - '🪟 Windows Latest (arm64)' |
| 29 | + |
| 30 | +jobs: |
| 31 | + select-os: |
| 32 | + name: 🗺️ Platform Selection → ${{ inputs.os }} |
| 33 | + runs-on: ubuntu-latest |
| 34 | + outputs: |
| 35 | + runner-os: ${{ steps.map-os.outputs.runner-os }} |
| 36 | + os-type: ${{ steps.map-os.outputs.os-type }} |
| 37 | + use-docker-sidecar: ${{ steps.map-os.outputs.use-docker-sidecar }} |
| 38 | + private-key: ${{ steps.generate-key.outputs.private-key }} |
| 39 | + public-key: ${{ steps.generate-key.outputs.public-key }} |
| 40 | + steps: |
| 41 | + - name: 🎯 Map OS selection to runner |
| 42 | + id: map-os |
| 43 | + env: |
| 44 | + INPUT_OS: ${{ inputs.os }} |
| 45 | + run: | |
| 46 | + echo "🔍 Mapping OS selection: $INPUT_OS" |
| 47 | + case "$INPUT_OS" in |
| 48 | + *"Ubuntu LTS (amd64)"*) |
| 49 | + echo "✅ Selected: Ubuntu Latest (AMD64)" |
| 50 | + echo "runner-os=ubuntu-latest" >> $GITHUB_OUTPUT |
| 51 | + echo "os-type=linux" >> $GITHUB_OUTPUT |
| 52 | + echo "use-docker-sidecar=false" >> $GITHUB_OUTPUT |
| 53 | + ;; |
| 54 | + *"Ubuntu LTS (arm64)"*) |
| 55 | + echo "✅ Selected: Ubuntu 24.04 (ARM64)" |
| 56 | + echo "runner-os=ubuntu-24.04-arm" >> $GITHUB_OUTPUT |
| 57 | + echo "os-type=linux" >> $GITHUB_OUTPUT |
| 58 | + echo "use-docker-sidecar=false" >> $GITHUB_OUTPUT |
| 59 | + ;; |
| 60 | + *"macOS Latest (arm64)"*) |
| 61 | + echo "✅ Selected: macOS Latest (ARM64)" |
| 62 | + echo "runner-os=macos-latest" >> $GITHUB_OUTPUT |
| 63 | + echo "os-type=macos" >> $GITHUB_OUTPUT |
| 64 | + echo "use-docker-sidecar=true" >> $GITHUB_OUTPUT |
| 65 | + ;; |
| 66 | + *"macOS 13 (amd64)"*) |
| 67 | + echo "✅ Selected: macOS 13 (AMD64)" |
| 68 | + echo "runner-os=macos-13" >> $GITHUB_OUTPUT |
| 69 | + echo "os-type=macos" >> $GITHUB_OUTPUT |
| 70 | + echo "use-docker-sidecar=false" >> $GITHUB_OUTPUT |
| 71 | + ;; |
| 72 | + *"Windows Latest (amd64)"*) |
| 73 | + echo "✅ Selected: Windows Latest (AMD64)" |
| 74 | + echo "runner-os=windows-latest" >> $GITHUB_OUTPUT |
| 75 | + echo "os-type=windows" >> $GITHUB_OUTPUT |
| 76 | + echo "use-docker-sidecar=false" >> $GITHUB_OUTPUT |
| 77 | + ;; |
| 78 | + *"Windows Latest (arm64)"*) |
| 79 | + echo "✅ Selected: Windows 11 (ARM64)" |
| 80 | + echo "runner-os=windows-11-arm" >> $GITHUB_OUTPUT |
| 81 | + echo "os-type=windows" >> $GITHUB_OUTPUT |
| 82 | + echo "use-docker-sidecar=true" >> $GITHUB_OUTPUT |
| 83 | + ;; |
| 84 | + *) |
| 85 | + echo "⚠️ No specific OS selected, defaulting to Ubuntu Latest" |
| 86 | + echo "runner-os=ubuntu-latest" >> $GITHUB_OUTPUT |
| 87 | + echo "os-type=linux" >> $GITHUB_OUTPUT |
| 88 | + echo "use-docker-sidecar=false" >> $GITHUB_OUTPUT |
| 89 | + ;; |
| 90 | + esac |
| 91 | +
|
| 92 | + - name: 🔑 Generate SSH Key Pair (ED25519) |
| 93 | + id: generate-key |
| 94 | + run: | |
| 95 | + echo "🔐 Generating secure SSH key pair..." |
| 96 | + ssh-keygen -t ed25519 -N "" -f ./id_ed25519 |
| 97 | + |
| 98 | + echo "📤 Exporting private SSH key" |
| 99 | + { |
| 100 | + echo "private-key<<EOF" |
| 101 | + cat ./id_ed25519 |
| 102 | + echo "EOF" |
| 103 | + } >> "$GITHUB_OUTPUT" |
| 104 | + |
| 105 | + echo "📤 Exporting public SSH key" |
| 106 | + echo "public-key=$(cat ./id_ed25519.pub)" >> $GITHUB_OUTPUT |
| 107 | + echo "✅ SSH key pair generated successfully!" |
| 108 | +
|
| 109 | + linux-sidecar: |
| 110 | + name: 🐳 Linux Docker Sidecar |
| 111 | + needs: [select-os] |
| 112 | + runs-on: ubuntu-latest |
| 113 | + if: needs.select-os.outputs.use-docker-sidecar == 'true' |
| 114 | + steps: |
| 115 | + - name: 🚀 Launch Linux Docker sidecar |
| 116 | + uses: lexbritvin/docker-sidecar-action/run-sidecar@main |
| 117 | + with: |
| 118 | + ssh-server-authorized-keys: ${{ needs.select-os.outputs.public-key }} |
| 119 | + use-bore: 'true' |
| 120 | + |
| 121 | + - name: ⏳ Wait for debug session to complete |
| 122 | + uses: lexbritvin/wait-action@v1 |
| 123 | + with: |
| 124 | + condition-type: 'job' |
| 125 | + job-name: '/How to connect/' |
| 126 | + timeout-seconds: 3600 |
| 127 | + poll-interval-seconds: 30 |
| 128 | + |
| 129 | + debug-session: |
| 130 | + name: 👉 How to connect 👈 |
| 131 | + needs: select-os |
| 132 | + runs-on: ${{ needs.select-os.outputs.runner-os || 'ubuntu-latest' }} |
| 133 | + steps: |
| 134 | + - name: 📥 Checkout repository |
| 135 | + uses: actions/checkout@v4 |
| 136 | + |
| 137 | + - name: 💻 System Information |
| 138 | + uses: lexbritvin/os-info-action@v1 |
| 139 | + |
| 140 | + - name: 🐹 Set up Go environment |
| 141 | + uses: actions/setup-go@v5 |
| 142 | + with: |
| 143 | + go-version-file: 'go.mod' |
| 144 | + |
| 145 | + - name: 🛠️ Install debugging tools |
| 146 | + shell: bash |
| 147 | + run: | |
| 148 | + echo "🔧 Setting up debugging environment..." |
| 149 | +
|
| 150 | + echo "📁 Configure Go temporary directory" |
| 151 | + export GOTMPDIR="$(pwd)/.gotmp" |
| 152 | + mkdir -p $GOTMPDIR |
| 153 | + echo "📁 Using temp directory: $GOTMPDIR" |
| 154 | + echo "GOTMPDIR=$GOTMPDIR" >> $GITHUB_ENV |
| 155 | +
|
| 156 | + if [[ "${{ runner.os }}" == "macOS" ]]; then |
| 157 | + echo "🍎 Configuring Go binary path for macOS..." |
| 158 | + ln -s $(which go) /usr/local/bin/go |
| 159 | + echo "✅ Go binary linked to user path" |
| 160 | + fi |
| 161 | + |
| 162 | + # Check Windows ARM64 compatibility |
| 163 | + if [[ "${{ runner.os }}" == "Windows" && "${{ runner.arch }}" == "ARM64" ]]; then |
| 164 | + echo "⚠️ WARNING: Delve debugger is not available on Windows ARM64" |
| 165 | + echo "🚫 Remote debugging features will be limited on this platform" |
| 166 | + else |
| 167 | + echo "📦 Installing Delve debugger..." |
| 168 | + go install github.com/go-delve/delve/cmd/dlv@latest |
| 169 | + echo "✅ Delve installed successfully!" |
| 170 | + fi |
| 171 | + |
| 172 | + echo "📦 Downloading Go modules..." |
| 173 | + go mod download |
| 174 | + go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest |
| 175 | + echo "✅ Dependencies ready!" |
| 176 | +
|
| 177 | + - name: 💾 Save Go environment variables |
| 178 | + shell: bash |
| 179 | + run: | |
| 180 | + echo "💾 Persisting Go environment configuration..." |
| 181 | + echo "📋 Saving: GOTMPDIR=${GOTMPDIR}" |
| 182 | + |
| 183 | + case "$RUNNER_OS" in |
| 184 | + "Linux"|"macOS") |
| 185 | + echo "🐧🍎 Configuring Unix-like environment..." |
| 186 | + # Add Go environment variables to system profile |
| 187 | + echo "export GOTMPDIR=\"${GOTMPDIR}\"" | sudo tee -a /etc/profile |
| 188 | + echo "✅ Environment saved to /etc/profile" |
| 189 | + echo "💡 To use: source /etc/profile" |
| 190 | + ;; |
| 191 | + |
| 192 | + "Windows") |
| 193 | + echo "🪟 Configuring Windows environment..." |
| 194 | + # 🔧 Set system environment variables |
| 195 | + setx GOTMPDIR "${GOTMPDIR}" //M 2>/dev/null |
| 196 | + echo "✅ Environment saved to Windows system variables" |
| 197 | + echo "💡 To use: Open new terminal session" |
| 198 | + ;; |
| 199 | + |
| 200 | + *) |
| 201 | + echo "❌ Unsupported OS: $RUNNER_OS" |
| 202 | + exit 1 |
| 203 | + ;; |
| 204 | + esac |
| 205 | +
|
| 206 | + - name: 🐳 Install Local Docker |
| 207 | + uses: lexbritvin/setup-docker-action@main |
| 208 | + if: needs.select-os.outputs.use-docker-sidecar == 'false' && needs.select-os.outputs.os-type != 'linux' |
| 209 | + |
| 210 | + - name: 🐳 Set up Remote Docker |
| 211 | + id: docker-setup |
| 212 | + uses: lexbritvin/docker-sidecar-action/setup-remote-docker@main |
| 213 | + if: needs.select-os.outputs.use-docker-sidecar == 'true' |
| 214 | + with: |
| 215 | + private-key: ${{ needs.select-os.outputs.private-key }} |
| 216 | + use-remote-share: 'true' |
| 217 | + |
| 218 | + - name: 🔗 Establish SSH Debug Session |
| 219 | + id: ssh-session |
| 220 | + uses: lexbritvin/ssh-session-action@v1 |
| 221 | + with: |
| 222 | + use-bore: 'true' |
| 223 | + use-actor-ssh-keys: 'true' |
| 224 | + detached: 'true' |
| 225 | + wait-timeout: '3600' |
| 226 | + |
| 227 | + - name: 👉 How to connect 👈 |
| 228 | + env: |
| 229 | + HELP_MESSAGE: ${{ steps.ssh-session.outputs.help-message }} |
| 230 | + EXTRA_HELP: | |
| 231 | + ╔══════════════════════════════════════════════════════════════════════════════════════════╗ |
| 232 | + 🐛 GO DEBUGGING WITH DELVE 🚀 |
| 233 | + ╚══════════════════════════════════════════════════════════════════════════════════════════╝ |
| 234 | + |
| 235 | + \033[1;36m┌─ 🔄 PORT FORWARDING SETUP\033[0m |
| 236 | + \033[1;36m│\033[0m \033[1mFor Delve debugging, forward port 2345:\033[0m |
| 237 | + \033[1;36m│\033[0m \033[1;33mssh -L 2345:localhost:2345 [...]\033[0m |
| 238 | + \033[1;36m└─\033[0m |
| 239 | + |
| 240 | + \033[1;32m┌─ 🧪 TESTING COMMANDS\033[0m |
| 241 | + \033[1;32m│\033[0m \033[1m• Run all tests:\033[0m |
| 242 | + \033[1;32m│\033[0m \033[1;96mgo test -v ./...\033[0m |
| 243 | + \033[1;32m│\033[0m \033[1m• Run specific test:\033[0m |
| 244 | + \033[1;32m│\033[0m \033[1;96mgo test -v -run TestFunctionName ./...\033[0m |
| 245 | + \033[1;32m│\033[0m \033[1m• Run with coverage:\033[0m |
| 246 | + \033[1;32m│\033[0m \033[1;96mgo test -v -cover ./...\033[0m |
| 247 | + \033[1;32m└─\033[0m |
| 248 | + |
| 249 | + \033[1;33m┌─ 🐛 DEBUGGING COMMANDS\033[0m |
| 250 | + \033[1;33m│\033[0m \033[1m• Debug specific test:\033[0m |
| 251 | + \033[1;33m│\033[0m \033[1;95mdlv --listen=:2345 --headless --api-version=2 test ./... -- -test.run TestName\033[0m |
| 252 | + \033[1;33m│\033[0m \033[1m• Debug main application:\033[0m |
| 253 | + \033[1;33m│\033[0m \033[1;95mdlv debug --headless --listen=:2345 --api-version=2 ./cmd/main -- [args...]\033[0m |
| 254 | + \033[1;33m│\033[0m \033[1m• Attach to running process:\033[0m |
| 255 | + \033[1;33m│\033[0m \033[1;95mdlv attach --headless --listen=:2345 --api-version=2 [PID]\033[0m |
| 256 | + \033[1;33m└─\033[0m |
| 257 | + |
| 258 | + \033[1;34m┌─ 🔗 IDE INTEGRATION\033[0m |
| 259 | + \033[1;34m│\033[0m \033[1m• GoLand/IntelliJ IDEA:\033[0m |
| 260 | + \033[1;34m│\033[0m \033[1m→ Run/Debug Configurations → Go Remote\033[0m |
| 261 | + \033[1;34m│\033[0m \033[1m→ Host: localhost, Port: 2345\033[0m |
| 262 | + \033[1;34m│\033[0m \033[1m• VS Code:\033[0m |
| 263 | + \033[1;34m│\033[0m \033[1m→ Use 'Go: Connect to server' command\033[0m |
| 264 | + \033[1;34m│\033[0m \033[1m→ Configure launch.json with 'connect' mode\033[0m |
| 265 | + \033[1;34m└─\033[0m |
| 266 | + |
| 267 | + \033[1;35m┌─ 💡 HELPFUL TIPS\033[0m |
| 268 | + \033[1;35m│\033[0m \033[1m• Set breakpoints before starting debug session\033[0m |
| 269 | + \033[1;35m│\033[0m \033[1m• Use 'dlv help' for more commands\033[0m |
| 270 | + \033[1;35m│\033[0m \033[1m• Check firewall settings if connection fails\033[0m |
| 271 | + \033[1;35m│\033[0m \033[1m• Session will auto-terminate after 30 minutes\033[0m |
| 272 | + \033[1;35m└─\033[0m |
| 273 | + |
| 274 | + \033[1;36m📚 Resources:\033[0m |
| 275 | + \033[1m• Delve Documentation: https://github.com/go-delve/delve/tree/master/Documentation\033[0m |
| 276 | + \033[1m• GoLand Remote Debug: https://www.jetbrains.com/help/go/attach-to-running-go-processes-with-debugger.html\033[0m |
| 277 | + \033[1m• VS Code Go Debug: https://github.com/golang/vscode-go/blob/master/docs/debugging.md\033[0m |
| 278 | + shell: bash |
| 279 | + run: | |
| 280 | + echo "🎉 SSH Debug Session Started Successfully!" |
| 281 | + |
| 282 | + # Display the SSH connection instructions with enhanced formatting |
| 283 | + printf "%b\n" "$HELP_MESSAGE" |
| 284 | + |
| 285 | + # Display the debugging guide with colors |
| 286 | + printf "%b\n" "$EXTRA_HELP" |
| 287 | + |
| 288 | + echo "🎯 Happy debugging! Your session is ready to use." |
0 commit comments