#!/bin/bash # Exit on error, undefined variables, and pipe failures set -euo pipefail # Configuration GITHUB_USERNAME=$1 IMAGE_NAME="finance-devcontainer" IMAGE_TAG="latest" # Load environment variables from .env file if it exists ENV_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/.env" if [ -f "$ENV_FILE" ]; then echo "Loading environment from $ENV_FILE" # Use grep to find the PAT line and extract the value, handling both Unix and Windows line endings GITHUB_PERSONAL_ACCESS_TOKEN=$(grep -a "^GITHUB_PERSONAL_ACCESS_TOKEN=" "$ENV_FILE" | sed 's/^GITHUB_PERSONAL_ACCESS_TOKEN=//' | tr -d '\r') export GITHUB_PERSONAL_ACCESS_TOKEN fi # Check for required username argument if [ -z "${GITHUB_USERNAME:-}" ]; then echo "Error: GitHub username is required" echo "Usage: $0 " echo "Example: $0 acedanger" exit 1 fi # Check for required tools for cmd in docker gh; do if ! command -v "$cmd" >/dev/null 2>&1; then echo "Error: $cmd is required but not installed" exit 1 fi done # Verify PAT is loaded if [ -z "${GITHUB_PERSONAL_ACCESS_TOKEN:-}" ]; then echo "Error: GITHUB_PERSONAL_ACCESS_TOKEN is not set" echo "Please ensure it is defined in $ENV_FILE" exit 1 fi # Check GitHub authentication if ! gh auth status >/dev/null 2>&1; then echo "Error: Not authenticated with GitHub. Please run 'gh auth login' first" exit 1 fi # Get absolute path to Dockerfile SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" DOCKERFILE_PATH="$SCRIPT_DIR/Dockerfile" FULL_IMAGE_NAME="ghcr.io/$GITHUB_USERNAME/$IMAGE_NAME:$IMAGE_TAG" echo "=== Building Development Container ===" echo "Username: $GITHUB_USERNAME" echo "Image: $FULL_IMAGE_NAME" echo "Dockerfile: $DOCKERFILE_PATH" echo "Using PAT: ${GITHUB_PERSONAL_ACCESS_TOKEN:0:4}... (first 4 chars)" # Build the image echo -e "\n=> Building image..." if ! docker build -t "$FULL_IMAGE_NAME" -f "$DOCKERFILE_PATH" "$SCRIPT_DIR"; then echo "Error: Docker build failed" exit 1 fi # Log in to GitHub Container Registry echo -e "\n=> Logging into GitHub Container Registry..." echo "$GITHUB_PERSONAL_ACCESS_TOKEN" | docker login ghcr.io -u "$GITHUB_USERNAME" --password-stdin # Push to GitHub Container Registry echo -e "\n=> Pushing image to GitHub Container Registry..." if ! docker push "$FULL_IMAGE_NAME"; then echo "Error: Failed to push image" echo "Please check your GitHub PAT has the required permissions:" echo " - read:packages" echo " - write:packages" exit 1 fi echo -e "\n=== Success! ===" echo "The development container image has been built and pushed" echo -e "\nTo use this image, update your devcontainer.json with:" echo '{ "image": "'$FULL_IMAGE_NAME'" }' echo -e "\nNext steps:" echo "1. Update .devcontainer/devcontainer.json with the image reference above" echo "2. Rebuild your development container in VS Code" echo " (Command Palette -> 'Dev Containers: Rebuild Container')"