From 64c99d88ecb4320244015d99f6199d69321e8241 Mon Sep 17 00:00:00 2001 From: dzaitsev Date: Wed, 7 May 2025 19:20:27 +0300 Subject: [PATCH] pipelines zip and gdrive upload updated --- build/jenkins/GDriveUpload | 168 ++++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 41 deletions(-) diff --git a/build/jenkins/GDriveUpload b/build/jenkins/GDriveUpload index b0958e3..db9fcb3 100644 --- a/build/jenkins/GDriveUpload +++ b/build/jenkins/GDriveUpload @@ -1,68 +1,154 @@ pipeline { agent { label 'Win10-BuildMachine' } + tools { + git 'Default' + } + parameters { - string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/Azaion/suite', description: 'Path where the zip file is located') + string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/AzaionSuite/suite', description: 'Path to folder containing zip builds.') } environment { + RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' } stages { - stage('Upload to Google Drive') { + 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') { steps { script { - echo "Looking for ZIP in: ${params.buildPath}" - echo "Target Google Drive folder: ${env.GOOGLE_DRIVE_FOLDER}" + echo "Finding latest zip file in: ${params.buildPath}" dir("${params.buildPath}") { - def zipFilename = readFile('created_zip.txt').trim() - - if (!fileExists(zipFilename)) { - error "Zip file '${zipFilename}' not found in ${params.buildPath}" - } - - // Replace 'your-working-remote' with your actual working rclone remote name - def remoteName = 'your-working-remote' - - // Check if file already exists - def alreadyExists = powershell(script: """ - \$result = rclone ls ${remoteName}:${env.GOOGLE_DRIVE_FOLDER} | Select-String "${zipFilename}" - if (\$result) { return 1 } else { return 0 } - """, returnStatus: true) == 1 - - if (alreadyExists) { - echo "Zip file '${zipFilename}' already exists on Google Drive. Skipping upload." - } else { - echo "Uploading '${zipFilename}' to Google Drive folder '${env.GOOGLE_DRIVE_FOLDER}'..." - powershell """ - rclone copy \"${zipFilename}\" ${remoteName}:${env.GOOGLE_DRIVE_FOLDER} - """ - } - - // Retention: keep only latest 5 - echo "Cleaning up old ZIP files in Google Drive..." - powershell """ - \$files = rclone lsf --files-only ${remoteName}:${env.GOOGLE_DRIVE_FOLDER} | Sort-Object - if (\$files.Count -gt 5) { - \$filesToDelete = \$files | Select-Object -First (\$files.Count - 5) - foreach (\$file in \$filesToDelete) { - rclone delete ${remoteName}:${env.GOOGLE_DRIVE_FOLDER}/\$file - } + def output = powershell(returnStdout: true, script: ''' + $pattern = "AzaionSuite*-*-*.zip" + $zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending + if ($zipFiles.Count -gt 0) { + $latestZip = $zipFiles[0].Name + Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$latestZip" + } else { + Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=" } - """ + ''').trim() + + def match = output =~ /::SET-ENV::LATEST_ZIP_FILENAME=(.+)/ + if (!match || !match[0][1]?.trim()) { + echo "⚠️ No ZIP files matching the pattern were found. Skipping upload and cleanup stages." + env.LATEST_ZIP_FILENAME = '' + } else { + def zipFileName = match[0][1] + echo "✅ Latest zip file selected: ${zipFileName}" + env.LATEST_ZIP_FILENAME = zipFileName + } } } } } + + stage('Upload If Not Exists & Always Remove Local') { + when { + expression { return env.LATEST_ZIP_FILENAME?.trim() } + } + steps { + echo "Checking Google Drive for existing ZIP and uploading if needed..." + powershell """ + \$fileName = "${env.LATEST_ZIP_FILENAME}" + \$folder = "${env.GOOGLE_DRIVE_FOLDER}" + \$rcloneRemote = "AzaionGoogleDrive:\$folder" + \$localFilePath = "${params.buildPath}/\$fileName" + + Write-Output "Checking for existing files in: \$rcloneRemote" + \$existingFiles = rclone lsf --files-only \$rcloneRemote + Write-Output "Existing files:" + Write-Output \$existingFiles + + if (\$existingFiles -match "^\$fileName\$") { + Write-Output "File '\$fileName' already exists on Google Drive. Skipping upload." + } else { + Write-Output "Uploading '\$fileName' to Google Drive..." + rclone copy "\$localFilePath" \$rcloneRemote --progress --drive-chunk-size 64M + Write-Output "Upload complete." + } + + if (Test-Path \$localFilePath) { + Remove-Item -Force \$localFilePath + Write-Output "Local file deleted: \$localFilePath" + } else { + Write-Output "Local file already deleted or not found." + } + """ + } + } + + stage('Cleanup Older Files on Google Drive') { + when { + expression { return env.LATEST_ZIP_FILENAME?.trim() } + } + steps { + echo "Cleaning up older files on Google Drive..." + powershell """ + Write-Output "Listing all files in the folder ${env.GOOGLE_DRIVE_FOLDER} on Google Drive..." + \$files = rclone lsf --files-only AzaionGoogleDrive:${env.GOOGLE_DRIVE_FOLDER} + Write-Output "Files found on Google Drive:" + Write-Output \$files + + \$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" } + 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) { + \$tempFile = [System.IO.Path]::GetTempFileName() + \$filesToDelete | Set-Content -Path \$tempFile -Encoding utf8 + + Write-Output "Contents of temporary delete list file (\$tempFile):" + Get-Content \$tempFile + + foreach (\$file in \$filesToDelete) { + Write-Output "Deleting \$file..." + rclone deletefile AzaionGoogleDrive:${env.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 { - success { - echo "Upload completed." + always { + echo 'Executing post-build cleanup...' + powershell ''' + $uploadDir = "temp_upload" + if (Test-Path $uploadDir) { + Remove-Item -Recurse -Force $uploadDir + } + ''' } + failure { - echo "Upload failed." + echo 'Pipeline failed. Check logs for details.' } } }