# Requires -Version 5.0 param( [Parameter(Mandatory=$true)] [string]$GitHubUsername ) # Stop on first error $ErrorActionPreference = "Stop" # Configuration $ImageName = "finance-devcontainer" $ImageTag = "latest" $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $DockerfilePath = Join-Path $ScriptDir "Dockerfile" $EnvFile = Join-Path $ScriptDir ".env" $FullImageName = "ghcr.io/$GitHubUsername/$ImageName`:$ImageTag" # Function to check required tools function Test-RequiredTools { $tools = @("docker", "gh") foreach ($tool in $tools) { if (-not (Get-Command $tool -ErrorAction SilentlyContinue)) { Write-Error "Error: $tool is required but not installed" exit 1 } } } # Function to load environment variables from .env file function Get-EnvContent { if (Test-Path $EnvFile) { Write-Host "Loading environment from $EnvFile" $envContent = Get-Content $EnvFile foreach ($line in $envContent) { if ($line -match '^GITHUB_PERSONAL_ACCESS_TOKEN=(.*)$') { return $matches[1] } } } Write-Error "Error: GITHUB_PERSONAL_ACCESS_TOKEN not found in $EnvFile" exit 1 } # Verify prerequisites Write-Host "=== Building Development Container ===" Write-Host "Username: $GitHubUsername" Write-Host "Image: $FullImageName" Write-Host "Dockerfile: $DockerfilePath" # Check required tools Test-RequiredTools # Check GitHub authentication try { gh auth status } catch { Write-Error "Error: Not authenticated with GitHub. Please run 'gh auth login' first" exit 1 } # Get GitHub PAT from .env file $GitHubPAT = Get-EnvContent if ([string]::IsNullOrEmpty($GitHubPAT)) { Write-Error "Error: GITHUB_PERSONAL_ACCESS_TOKEN is empty" exit 1 } Write-Host "Using PAT: $($GitHubPAT.Substring(0, 4))... (first 4 chars)" # Build the image Write-Host "`n=> Building image..." try { docker build -t $FullImageName -f $DockerfilePath $ScriptDir } catch { Write-Error "Error: Docker build failed" exit 1 } # Log in to GitHub Container Registry Write-Host "`n=> Logging into GitHub Container Registry..." $GitHubPAT | docker login ghcr.io -u $GitHubUsername --password-stdin if ($LASTEXITCODE -ne 0) { Write-Error "Error: Failed to authenticate with GitHub Container Registry" exit 1 } # Push to GitHub Container Registry Write-Host "`n=> Pushing image to GitHub Container Registry..." docker push $FullImageName if ($LASTEXITCODE -ne 0) { Write-Error @" Error: Failed to push image Please check your GitHub PAT has the required permissions: - read:packages - write:packages "@ exit 1 } Write-Host "`n=== Success! ===" Write-Host "The development container image has been built and pushed" Write-Host "`nTo use this image, update your devcontainer.json with:" Write-Host @" { "image": "$FullImageName" } "@ Write-Host "`nNext steps:" Write-Host "1. Update .devcontainer/devcontainer.json with the image reference above" Write-Host "2. Rebuild your development container in VS Code" Write-Host " (Command Palette -> 'Dev Containers: Rebuild Container')"