pipeline { agent { label 'Win10-BuildMachine' } tools { git 'Default' } parameters { string(name: 'GOOGLE_DRIVE_FOLDER', defaultValue: 'AzaionSuiteBuilds', description: 'The folder on Google Drive where the build will be uploaded.') } environment { RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' } stages { stage('Initialize Workspace') { steps { echo 'Initializing workspace on Windows agent...' powershell ''' $uploadDir = "temp_upload" if (-Not (Test-Path $uploadDir)) { New-Item -ItemType Directory -Path $uploadDir | Out-Null } ''' } } stage('Find Latest Zip and Upload') { steps { script { echo "Starting 'Find Latest Zip and Upload' stage." dir("C:/Jenkins/workspace/AzaionSuite/suite") { def output = powershell(returnStdout: true, script: ''' $pattern = "AzaionSuite*-*-*.zip" $zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending if ($zipFiles.Count -eq 0) { Write-Error "No ZIP files matching pattern '$pattern' were found." exit 1 } $latestZip = $zipFiles[0].Name Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$latestZip" ''').trim() def match = output =~ /::SET-ENV::LATEST_ZIP_FILENAME=(.+)/ if (!match) { error("Could not find the latest zip filename in the PowerShell output using marker.") } def zipFileName = match[0][1] echo "Latest zip file selected: ${zipFileName}" env.LATEST_ZIP_FILENAME = zipFileName } } } } stage('Upload to Google Drive using rclone') { steps { echo "Uploading ${env.LATEST_ZIP_FILENAME} to Google Drive..." powershell """ \$filePath = "C:/Jenkins/workspace/AzaionSuite/suite/${env.LATEST_ZIP_FILENAME}" Write-Output "Preparing to upload: \$filePath" Get-Content 'C:/Program Files/rclone/rclone.conf' \$env:RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' rclone copy "\$filePath" AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --progress --drive-chunk-size 64M """ } } stage('Cleanup Older Files on Google Drive') { steps { echo "Cleaning up older files on Google Drive..." powershell """ Write-Output "Listing all files in the folder ${params.GOOGLE_DRIVE_FOLDER} on Google Drive..." \$files = rclone lsf --files-only AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} Write-Output "Files found on Google Drive:" Write-Output \$files \$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" } | Sort-Object -Descending \$filesToDelete = \$filesArray | Select-Object -Skip 3 Write-Output "Files to delete (older than 3 latest):" Write-Output \$filesToDelete if (\$filesToDelete.Count -gt 0) { \$tempFile = [System.IO.Path]::GetTempFileName() \$filesToDelete | Out-File -FilePath \$tempFile -Encoding utf8 W rite-Output "Contents of temporary delete list file (\$tempFile):" Get-Content \$tempFile rclone delete AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --files-from \$tempFile Remove-Item -Path \$tempFile } else { Write-Output "No files to delete." } """ } } } post { always { echo 'Executing post-build cleanup...' powershell ''' $uploadDir = "temp_upload" if (Test-Path $uploadDir) { Remove-Item -Recurse -Force $uploadDir } ''' } failure { echo 'Pipeline failed. Check logs for details.' } } }