pipeline { agent any environment { GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder name LATEST_ZIP_FILENAME = '' // Will be set during the "Find Latest Zip" stage } stages { stage('Declarative: Checkout SCM') { steps { checkout scm } } stage('Declarative: Tool Install') { steps { // Ensure required tools are available, like Git, PowerShell, etc. echo "Installing tools if necessary" } } stage('Initialize Workspace') { steps { echo "Initializing workspace on Windows agent..." // Initialization steps for workspace } } stage('Find Latest Zip') { steps { script { echo "Finding latest zip file in: C:/Jenkins/workspace/Azaion/suite" dir('C:/Jenkins/workspace/Azaion/suite') { // Find the latest zip file using shell command def latestZip = bat(script: 'dir /b /od *.zip | findstr /r /c:"AzaionSuite.Iterative.*.zip"', returnStdout: true).trim() env.LATEST_ZIP_FILENAME = latestZip echo "✅ Latest zip file selected: ${env.LATEST_ZIP_FILENAME}" } } } } stage('Upload If Not Exists & Always Remove Local') { steps { echo "Checking Google Drive for existing ZIP and uploading if needed..." powershell """ # Check if file exists on Google Drive \$existingFiles = rclone lsf --files-only AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER} if (\$existingFiles -contains "${env.LATEST_ZIP_FILENAME}") { Write-Output "File ${env.LATEST_ZIP_FILENAME} already exists on Google Drive, skipping upload." } else { Write-Output "Uploading ${env.LATEST_ZIP_FILENAME} to Google Drive..." rclone copy C:/Jenkins/workspace/Azaion/suite/${env.LATEST_ZIP_FILENAME} AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER} } # Always remove the local file after uploading Remove-Item "C:/Jenkins/workspace/Azaion/suite/${env.LATEST_ZIP_FILENAME}" """ } } stage('Cleanup Older Files on Google Drive') { steps { echo "Cleaning up older files on Google Drive..." powershell """ # Check if folder exists if (-not (Test-Path "AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}")) { Write-Error "Google Drive folder ${GOOGLE_DRIVE_FOLDER} not found!" exit 1 } # List all files in the Google Drive folder Write-Output "Listing all files in the folder ${GOOGLE_DRIVE_FOLDER} on Google Drive..." \$files = rclone lsf --files-only AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER} Write-Output "Files found on Google Drive:" Write-Output \$files # Split files by line and exclude the latest zip file \$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" } \$latestFile = "${env.LATEST_ZIP_FILENAME}" \$filesArray = \$filesArray | Where-Object { \$_ -ne \$latestFile } # If more than 3 files exist, delete the oldest ones if (\$filesArray.Count -gt 3) { \$filesSorted = \$filesArray | Sort-Object -Descending \$filesToDelete = \$filesSorted | Select-Object -Skip 3 Write-Output "Files to delete (older than 3 latest):" Write-Output \$filesToDelete if (\$filesToDelete.Count -gt 0) { # Create temporary delete list file \$tempFile = [System.IO.Path]::GetTempFileName() \$filesToDelete | Set-Content -Path \$tempFile -Encoding utf8 Write-Output "Contents of temporary delete list file (\$tempFile):" Get-Content \$tempFile # Delete files listed for removal foreach (\$file in \$filesToDelete) { Write-Output "Deleting \$file..." rclone deletefile AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}/\$file } Remove-Item -Path \$tempFile } else { Write-Output "No files to delete." } } else { Write-Output "No files found on Google Drive to clean up." } """ } } } post { failure { echo "Pipeline failed. Check logs for details." } success { echo "Pipeline completed successfully." } } }