name: Build macOS Installer ISO/DMG image on: workflow_dispatch: inputs: macos_version: description: 'Select a macOS version to build' required: true type: choice options: - 'Tahoe' - 'Sequoia' - 'Sonoma' - 'Ventura' - 'Monterey' - 'Big Sur' - 'Catalina' - 'Mojave' - 'High Sierra' image_format: description: 'Select image format, iso for VM, dmg for burn to USB drive' required: true type: choice options: - 'iso' - 'dmg' specific_version: description: 'Optional: Specific version number, e.g., 15.7.1' required: false type: string jobs: build-installer: runs-on: macos-15-intel timeout-minutes: 120 steps: - name: Checkout repository uses: actions/checkout@v5 - name: System Information run: | echo "Runner OS: $(sw_vers -productName) $(sw_vers -productVersion)" echo "Architecture: $(uname -m)" echo "Available disk space:" df -h echo "Memory:" top -l 1 | grep PhysMem - name: Fetch available macOS installers id: fetch_installers run: | echo "Fetching available installers..." softwareupdate --list-full-installers 2>/dev/null | grep "* Title:" | sed 's/^[[:space:]]*//' > installers.txt cat installers.txt # Parse and find matching installer if [ -n "${{ github.event.inputs.specific_version }}" ]; then INSTALLER_VERSION="${{ github.event.inputs.specific_version }}" echo "Looking for specific version: $INSTALLER_VERSION" else # Get latest version for selected macOS INSTALLER_VERSION=$(grep "${{ github.event.inputs.macos_version }}" installers.txt | head -1 | sed -n 's/.*Version: \([^,]*\).*/\1/p') echo "Found latest ${{ github.event.inputs.macos_version }} version: $INSTALLER_VERSION" fi if [ -z "$INSTALLER_VERSION" ]; then echo "Error: Could not find installer version" exit 1 fi echo "version=$INSTALLER_VERSION" >> $GITHUB_OUTPUT - name: Download macOS installer run: | echo "Downloading macOS ${{ github.event.inputs.macos_version }} version ${{ steps.fetch_installers.outputs.version }}" # Download with retries MAX_RETRIES=3 RETRY_COUNT=0 while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do if softwareupdate --fetch-full-installer --full-installer-version "${{ steps.fetch_installers.outputs.version }}"; then echo "Download successful!" break else RETRY_COUNT=$((RETRY_COUNT + 1)) echo "Download failed. Retry $RETRY_COUNT of $MAX_RETRIES..." sleep 5 fi done if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then echo "Download failed after $MAX_RETRIES attempts" exit 1 fi # Verify installer exists and set path INSTALLER_PATH="/Applications/Install macOS ${{ github.event.inputs.macos_version }}.app" if [ ! -d "$INSTALLER_PATH" ]; then echo "Error: Installer not found at $INSTALLER_PATH" ls -la /Applications/Install*.app 2>/dev/null || echo "No Install*.app found" exit 1 fi echo "Installer found at: $INSTALLER_PATH" - name: Create ISO if: github.event.inputs.image_format == 'iso' id: create_iso run: | echo "Creating ISO for macOS ${{ github.event.inputs.macos_version }}" # Set variables ISO_NAME="macOS_${{ github.event.inputs.macos_version }}_${{ steps.fetch_installers.outputs.version }}" ISO_NAME="${ISO_NAME// /_}" SPARSE_IMAGE="/tmp/${ISO_NAME}.sparseimage" ISO_FILE="${GITHUB_WORKSPACE}/${ISO_NAME}.iso" VOLUME_NAME="${ISO_NAME}_installer" INSTALLER_PATH="/Applications/Install macOS ${{ github.event.inputs.macos_version }}.app" echo "ISO will be created at: $ISO_FILE" echo "Using installer at: $INSTALLER_PATH" # Create sparse image echo "Creating sparse image..." hdiutil create -size 20g -volname "$VOLUME_NAME" -fs HFS+ -type SPARSE -attach "$SPARSE_IMAGE" # Create install media echo "Creating install media..." sudo "$INSTALLER_PATH/Contents/Resources/createinstallmedia" --volume "/Volumes/$VOLUME_NAME" --nointeraction # Should wait for a few seconds sync && sleep 5 # Detach volume echo "Detaching volume..." INSTALLER_NAME="Install macOS ${{ github.event.inputs.macos_version }}" if hdiutil detach "/Volumes/$INSTALLER_NAME" || hdiutil detach "/Volumes/$INSTALLER_NAME" -force; then echo "Volume detached successfully" else echo "Failed to detach volume" fi sleep 2 # Create hybrid ISO echo "Creating hybrid ISO image..." hdiutil makehybrid -hfs -udf -o "$ISO_FILE" "$SPARSE_IMAGE" # Show ISO info and set output if [ -f "$ISO_FILE" ]; then ISO_SIZE=$(du -h "$ISO_FILE" | cut -f1) echo "" echo "=========================================" echo "ISO created successfully!" echo "ISO size: $ISO_SIZE" echo "=========================================" echo "" echo "image_file=$ISO_FILE" >> $GITHUB_OUTPUT echo "image_name=${ISO_NAME}.iso" >> $GITHUB_OUTPUT else echo "Error: ISO file not created" exit 1 fi - name: Create DMG if: github.event.inputs.image_format == 'dmg' id: create_dmg run: | echo "Creating macOS ${{ github.event.inputs.macos_version }} DMG image" # Set variables DMG_NAME="macOS_${{ github.event.inputs.macos_version }}_${{ steps.fetch_installers.outputs.version }}" DMG_NAME="${DMG_NAME// /_}" DMG_FILE="${GITHUB_WORKSPACE}/${DMG_NAME}.dmg" VOLUME_NAME="Install_Temp" INSTALLER_PATH="/Applications/Install macOS ${{ github.event.inputs.macos_version }}.app" echo "DMG will be created at: $DMG_FILE" echo "Using installer at: $INSTALLER_PATH" # Step 1: Create a 20GB disk image and attach it echo "Step 1: Creating 20GB DMG disk image..." hdiutil create -size 20g -volname "$VOLUME_NAME" -fs HFS+ -type UDIF -attach "$DMG_FILE" # Get the disk identifier DISK_ID=$(diskutil list | grep "$VOLUME_NAME" | head -1 | awk '{print $NF}' | sed 's/s[0-9]*$//') # Step 2: Create install media echo "Step 2: Creating install media ..." sudo "$INSTALLER_PATH/Contents/Resources/createinstallmedia" --volume "/Volumes/$VOLUME_NAME" --nointeraction sync && sleep 5 # Step 3: Check minimum size requirements echo "Step 3: Check minimum size requirements..." INSTALLER_PARTITION="/dev/${DISK_ID}s2" diskutil resizeVolume "$INSTALLER_PARTITION" limits | tee resize_limits.txt # Extract the byte value from parentheses MIN_SIZE=$(grep "Minimum (constrained by file usage)" resize_limits.txt | sed -n 's/.*(\([0-9]*\) Bytes).*/\1/p') echo "Minimum size: $MIN_SIZE bytes" # Step 4: Consolidate installer data by adding a dummy partition, this forces macOS to move all data to the front of the partition echo "Step 4: Resizing and consolidating data..." diskutil resizeVolume "$INSTALLER_PARTITION" "${MIN_SIZE}B" HFS+ Dummy 0 sync && sleep 5 # Step 5: Remove dummy partition, leaving free space at the end echo "Step 5: Removing dummy partition..." DUMMY_PARTITION=${DISK_ID}s3 diskutil eraseVolume free free "$DUMMY_PARTITION" sync && sleep 5 # Step 6: Expand installer partition back echo "Step 6: Expanding installer partition back..." diskutil resizeVolume "$INSTALLER_PARTITION" 0 sync && sleep 5 # Step 7: Detach the disk echo "Step 7: Detaching disk..." hdiutil detach "$DISK_ID" || hdiutil detach "$DISK_ID" -force sleep 2 # Step 8: Optimize DMG size echo "Step 8: Optimizing DMG size..." hdiutil resize -size min "$DMG_FILE" # Show final DMG info if [ -f "$DMG_FILE" ]; then DMG_SIZE=$(du -h "$DMG_FILE" | cut -f1) echo "" echo "=========================================" echo "Optimize DMG created successfully!" echo "DMG size: $DMG_SIZE" echo "=========================================" echo "" echo "image_file=$DMG_FILE" >> $GITHUB_OUTPUT echo "image_name=${DMG_NAME}.dmg" >> $GITHUB_OUTPUT else echo "Error: DMG file not created" exit 1 fi - name: Upload ISO artifact if: github.event.inputs.image_format == 'iso' uses: actions/upload-artifact@v5 with: name: macOS-${{ github.event.inputs.macos_version }}-${{ steps.fetch_installers.outputs.version }}-ISO path: ${{ steps.create_iso.outputs.image_file }} compression-level: 0 retention-days: 3 - name: Upload DMG artifact if: github.event.inputs.image_format == 'dmg' uses: actions/upload-artifact@v5 with: name: macOS-${{ github.event.inputs.macos_version }}-${{ steps.fetch_installers.outputs.version }}-DMG path: ${{ steps.create_dmg.outputs.image_file }} compression-level: 0 retention-days: 3 - name: Summary run: | if [ "${{ github.event.inputs.image_format }}" == "iso" ]; then echo "## Successfully built macOS ${{ github.event.inputs.macos_version }} ${{ steps.fetch_installers.outputs.version }} ISO" >> $GITHUB_STEP_SUMMARY else echo "## Successfully built macOS ${{ github.event.inputs.macos_version }} ${{ steps.fetch_installers.outputs.version }} DMG" >> $GITHUB_STEP_SUMMARY fi echo "> [!Tip]" >> $GITHUB_STEP_SUMMARY echo "> Enable [Cloudflare WARP](https://one.one.one.one/) for faster downloads." >> $GITHUB_STEP_SUMMARY