Skip to content

Instantly share code, notes, and snippets.

@bpmct
Created March 15, 2025 17:57
Show Gist options
  • Save bpmct/2e21117aff86cfd63cbe3f25b3860c6e to your computer and use it in GitHub Desktop.
Save bpmct/2e21117aff86cfd63cbe3f25b3860c6e to your computer and use it in GitHub Desktop.
Building Coder on Windows - Guide and Automation Script (with randomized workspace names)

Building Coder on Windows - Complete Guide

This guide documents the process of setting up a Windows workspace using Coder and building the Coder repository on it.

Prerequisites

  • Access to a Coder deployment (e.g., dev.coder.com)
  • coder CLI installed and authenticated

Step 1: Create a Windows Workspace

# Generate a random suffix for workspace name to avoid conflicts
RANDOM_SUFFIX=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)
WORKSPACE_NAME="win-coder-${RANDOM_SUFFIX}"

# Create the workspace using the windows-with-good-rdp template
coder create --parameter display_enabled=false \
             --parameter gpu_enabled=false \
             --parameter zone="us-central1-a" \
             --template windows-with-good-rdp \
             --org=coder \
             ${WORKSPACE_NAME}

When prompted, confirm the creation by typing yes.

Step 2: Set Up SSH Configuration

# Ensure SSH directory exists
mkdir -p ~/.ssh && chmod 700 ~/.ssh

# Configure SSH for Coder
coder config-ssh -y

Step 3: Connect to the Windows Workspace

# Test connection to the workspace
ssh coder.${WORKSPACE_NAME} echo "Connection successful"

Step 4: Windows Environment Setup and Building Coder

Here are the PowerShell commands to run on the Windows workspace:

# Create a working directory
New-Item -Path "C:\build-test" -ItemType Directory -Force
Set-Location -Path "C:\build-test"

# Install Git
Write-Host "Installing Git..."
Invoke-WebRequest -Uri "https://github.com/git-for-windows/git/releases/download/v2.42.0.windows.2/Git-2.42.0.2-64-bit.exe" -OutFile "git-installer.exe"
Start-Process -FilePath "git-installer.exe" -ArgumentList "/VERYSILENT /NORESTART" -Wait

# Clone the Coder repository
$gitPath = "C:\Program Files\Git\bin\git.exe"
Write-Host "Using Git from: $gitPath"
& $gitPath clone https://github.com/coder/coder.git
Set-Location -Path "coder"

# Install Go (portable version)
Write-Host "Installing Go (portable version)..."
Invoke-WebRequest -Uri "https://go.dev/dl/go1.21.0.windows-amd64.zip" -OutFile "C:\build-test\go.zip"

# Create directory for extraction
New-Item -Path "C:\build-test\go3" -ItemType Directory -Force

# Extract Go using .NET
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\build-test\go.zip", "C:\build-test\go3")

# Set environment variables
$env:Path += ";C:\build-test\go3\go\bin"
$env:GOPATH = "C:\build-test\gopath"
[Environment]::SetEnvironmentVariable("Path", $env:Path, "User")
[Environment]::SetEnvironmentVariable("GOPATH", $env:GOPATH, "User")

# Verify Go installation
Write-Host "Verifying Go installation..."
& "C:\build-test\go3\go\bin\go.exe" version

# Build the Coder project
Write-Host "Building Coder project..."
Set-Location -Path "C:\build-test\coder"
& "C:\build-test\go3\go\bin\go.exe" build -o coder.exe ./cmd/coder

# Verify the build
if (Test-Path "coder.exe") {
    Write-Host "Build successful\! Coder binary created at: $(Get-Location)\coder.exe"
    & .\coder.exe version
} else {
    Write-Host "Build failed. The binary was not created."
}

Step 5: Verify the Build

# Check what's in the build directory
ssh coder.${WORKSPACE_NAME} powershell -Command "Get-ChildItem -Path 'C:\build-test\coder' -File | Where-Object { \$_.Extension -eq '.exe' } | Select-Object Name, Length, LastWriteTime"

# Verify the executable works
ssh coder.${WORKSPACE_NAME} powershell -Command "& 'C:\build-test\coder\coder.exe' version"

Notes and Troubleshooting

  1. Windows RDP Access: The Windows workspace created with the template includes RDP access with these credentials:

    • Username: Administrator
    • Password: coderRDP\!
  2. Build Time: The Coder build process can take between 5-15 minutes depending on the workspace resources and network connectivity.

  3. SSH Connection Issues: If SSH connection stalls, try adding a timeout or forcing a new connection:

    timeout 30 ssh coder.${WORKSPACE_NAME} echo "Connection test"
  4. Alternative Method for Script Execution: If sending a script via standard input doesn't work, you can create a Base64-encoded version:

    SCRIPT_B64=$(base64 -w 0 setup_coder_build.ps1)
    ssh coder.${WORKSPACE_NAME} powershell -Command "[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('$SCRIPT_B64')) | powershell -Command -"
  5. Environment Persistence: Once set up, the environment will persist in the workspace as long as the workspace exists. The Go installation, Git, and the cloned repository will remain intact.

#\!/bin/bash
# Complete script to create a Windows workspace in Coder and build the Coder repo
set -e
# Generate random workspace name with a prefix
RANDOM_SUFFIX=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)
WORKSPACE_NAME="win-coder-${RANDOM_SUFFIX}"
echo "==== Starting Windows workspace creation and Coder build process ===="
echo "Using workspace name: ${WORKSPACE_NAME}"
# Step 1: Create the workspace
echo "Creating Windows workspace..."
coder create --parameter display_enabled=false \
--parameter gpu_enabled=false \
--parameter zone="us-central1-a" \
--template windows-with-good-rdp \
--org=coder \
${WORKSPACE_NAME} <<< "yes"
# Step 2: Set up SSH
echo "Setting up SSH configuration..."
mkdir -p ~/.ssh && chmod 700 ~/.ssh
coder config-ssh -y
# Step 3: Wait for the workspace to be ready
echo "Waiting for workspace to be ready..."
sleep 60 # Give the workspace time to initialize
# Try connecting to verify the workspace is available
echo "Testing connection to workspace..."
ssh coder.${WORKSPACE_NAME} echo "Connection successful" || {
echo "Connection failed. Waiting another 60 seconds...";
sleep 60;
ssh coder.${WORKSPACE_NAME} echo "Retry connection" || {
echo "Connection still failed. Please check workspace status manually.";
exit 1;
}
}
# Step 4: Create PowerShell script for Windows setup
echo "Creating Windows setup script..."
cat > setup_coder_build.ps1 << 'EOPS'
# Create a working directory
Write-Host "Creating working directory..."
New-Item -Path "C:\build-test" -ItemType Directory -Force
Set-Location -Path "C:\build-test"
# Install Git
Write-Host "Installing Git..."
Invoke-WebRequest -Uri "https://github.com/git-for-windows/git/releases/download/v2.42.0.windows.2/Git-2.42.0.2-64-bit.exe" -OutFile "git-installer.exe"
Start-Process -FilePath "git-installer.exe" -ArgumentList "/VERYSILENT /NORESTART" -Wait
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
# Verify Git installation
try {
$gitPath = "C:\Program Files\Git\bin\git.exe"
if (Test-Path $gitPath) {
Write-Host "Git installed successfully at: $gitPath"
& $gitPath --version
} else {
Write-Host "Git installation path not found. Searching for git..."
$gitCmd = Get-Command "git" -ErrorAction SilentlyContinue
if ($gitCmd) {
Write-Host "Git found at: $($gitCmd.Source)"
git --version
} else {
Write-Host "Git not found in PATH. Using absolute path for next steps."
}
}
} catch {
Write-Host "Error checking Git: $_"
}
# Clone the Coder repository
Write-Host "Cloning the Coder repository..."
& $gitPath clone https://github.com/coder/coder.git
if (Test-Path "coder") {
Write-Host "Repository cloned successfully."
Set-Location -Path "coder"
} else {
Write-Host "Failed to clone repository."
exit 1
}
# Install Go (portable version)
Write-Host "Installing Go (portable version)..."
Invoke-WebRequest -Uri "https://go.dev/dl/go1.21.0.windows-amd64.zip" -OutFile "C:\build-test\go.zip"
# Create directory for extraction
Write-Host "Extracting Go archive..."
New-Item -Path "C:\build-test\go3" -ItemType Directory -Force
# Extract Go using .NET
try {
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\build-test\go.zip", "C:\build-test\go3")
Write-Host "Go archive extracted successfully."
} catch {
Write-Host "Error extracting Go archive: $_"
exit 1
}
# Set environment variables
$env:Path += ";C:\build-test\go3\go\bin"
$env:GOPATH = "C:\build-test\gopath"
[Environment]::SetEnvironmentVariable("Path", $env:Path, "User")
[Environment]::SetEnvironmentVariable("GOPATH", $env:GOPATH, "User")
# Verify Go installation
$goExe = "C:\build-test\go3\go\bin\go.exe"
if (Test-Path $goExe) {
Write-Host "Go installation successful. Version:"
& $goExe version
} else {
Write-Host "Go executable not found at expected location."
exit 1
}
# Build the Coder project
Write-Host "Building Coder project (this may take several minutes)..."
Set-Location -Path "C:\build-test\coder"
Write-Host "Current directory: $(Get-Location)"
# Download dependencies first
Write-Host "Downloading Go dependencies..."
& $goExe mod download
# Build the binary
Write-Host "Building Coder binary..."
& $goExe build -o coder.exe ./cmd/coder
# Verify the build
if (Test-Path "coder.exe") {
$fileInfo = Get-Item "coder.exe"
Write-Host "Build successful\! Coder binary created:"
Write-Host " - Path: $($fileInfo.FullName)"
Write-Host " - Size: $($fileInfo.Length) bytes"
Write-Host " - Created: $($fileInfo.LastWriteTime)"
# Run version command
Write-Host "Checking version information:"
& .\coder.exe version
} else {
Write-Host "Build failed. The binary was not created."
exit 1
}
Write-Host "Setup and build complete\!"
EOPS
# Step 5: Send script to Windows workspace and execute it
echo "Transferring and executing build script on workspace..."
cat setup_coder_build.ps1 | ssh coder.${WORKSPACE_NAME} powershell -Command "-"
# Step 6: Verify the build result
echo "Verifying build result..."
ssh coder.${WORKSPACE_NAME} 'dir "C:\build-test\coder\coder.exe" 2>NUL && echo "BUILD SUCCESSFUL\!" || echo "Build not complete yet or failed."'
echo "==== Process completed ===="
echo "Workspace name: ${WORKSPACE_NAME}"
echo "To connect via SSH: ssh coder.${WORKSPACE_NAME}"
echo "To view in browser: coder open ${WORKSPACE_NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment