param ( [string]$Path = ".", [string]$BuildEnv = "Stage" # Added parameter for build environment, defaults to "Stage" ) Write-Output "`n=== Starting Upload: Full Installer ZIP ===" # Define target Google Drive folders $stageUploadFolder = "AzaionGoogleDrive:AzaionSuiteBuilds" $prodUploadFolder = "AzaionGoogleDrive:AzaionSuiteLatestProduction" # Make sure this matches your actual rclone remote name and path $targetUploadPath = "" if ($BuildEnv -eq "Prod") { Write-Output "[INFO] Build environment is Production. Preparing to upload to: $prodUploadFolder" # 1. Ensure the Production folder exists Write-Output "[ACTION] Ensuring production folder '$prodUploadFolder' exists..." rclone mkdir "$prodUploadFolder" if ($LASTEXITCODE -ne 0) { Write-Output "[ERROR] Failed to create or confirm existence of production folder with exit code $LASTEXITCODE. Aborting upload." exit 1 } else { Write-Output "[SUCCESS] Production folder confirmed/created." } $targetUploadPath = $prodUploadFolder } else { Write-Output "[INFO] Build environment is Stage. Uploading to: $stageUploadFolder" $targetUploadPath = $stageUploadFolder } # Updated filter for finding the ZIP file $filter = "AzaionSuite.Full.$BuildEnv.*.zip" Write-Output "[INFO] Looking for latest .zip matching '$filter' in folder: $Path..." $fullZip = Get-ChildItem -Path $Path -Filter $filter | Sort-Object LastWriteTime -Descending | Select-Object -First 1 if ($fullZip) { Write-Output "[FOUND] Full installer ZIP: $($fullZip.Name)" Write-Output "[INFO] Full path: $($fullZip.FullName)" Write-Output "[ACTION] Uploading file to Google Drive using rclone..." # 2. Upload the new build rclone copy "$($fullZip.FullName)" "$targetUploadPath" --progress if ($LASTEXITCODE -eq 0) { Write-Output "[SUCCESS] Full installer ZIP uploaded successfully." # 3. Delete all other builds except the newly uploaded one (only for Prod) if ($BuildEnv -eq "Prod") { Write-Output "[ACTION] Deleting old production builds, keeping only the newly uploaded file..." $uploadedFileName = $fullZip.Name # Get list of files in the target folder using rclone lsf and filter out directories # rclone lsf lists files and directories, with directories having a '/' suffix $existingFiles = (rclone lsf "$prodUploadFolder") | ForEach-Object { $_.Trim() } | Where-Object { -not $_.EndsWith('/') } if ($LASTEXITCODE -eq 0) { foreach ($fileName in $existingFiles) { if ($fileName -ne $uploadedFileName) { Write-Output "[INFO] Deleting old file: $fileName" rclone delete "$prodUploadFolder/$fileName" if ($LASTEXITCODE -ne 0) { Write-Output "[WARNING] Failed to delete '$fileName' with exit code $LASTEXITCODE." } } } Write-Output "[SUCCESS] Old production builds cleanup complete." } else { Write-Output "[ERROR] Failed to list files in '$prodUploadFolder' for cleanup with exit code $LASTEXITCODE." } } } else { Write-Output "[ERROR] Upload failed with exit code $LASTEXITCODE." } } else { Write-Output "[WARNING] No matching Full installer ZIP found in folder: $Path." } Write-Output "=== Upload Complete: Full Installer ZIP ===`n"