From 7fc51e19eef389b27e374d979423f0cae06d9217 Mon Sep 17 00:00:00 2001 From: dzaitsev Date: Sun, 4 May 2025 15:31:12 +0300 Subject: [PATCH] latest fixes for Gdrive upload --- build/jenkins/GDriveUpload | 58 ++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/build/jenkins/GDriveUpload b/build/jenkins/GDriveUpload index b3699d2..8c6fa37 100644 --- a/build/jenkins/GDriveUpload +++ b/build/jenkins/GDriveUpload @@ -10,7 +10,7 @@ pipeline { } environment { - RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' // Ensure this points to the correct location + RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' } stages { @@ -57,18 +57,25 @@ pipeline { stage('Upload to Google Drive using rclone') { steps { - echo "Uploading ${env.LATEST_ZIP_FILENAME} to Google Drive..." + echo "Checking if ${env.LATEST_ZIP_FILENAME} already exists on Google Drive..." powershell """ - \$filePath = "C:/Jenkins/workspace/AzaionSuite/suite/${env.LATEST_ZIP_FILENAME}" - Write-Output "Preparing to upload: \$filePath" + \$fileName = "${env.LATEST_ZIP_FILENAME}" + \$folder = "${params.GOOGLE_DRIVE_FOLDER}" + \$rcloneRemote = "AzaionGoogleDrive:\$folder" - # Display the contents of the rclone configuration file to ensure it's being read correctly - Get-Content 'C:/Program Files/rclone/rclone.conf' + Write-Output "Checking for existing files in: \$rcloneRemote" + \$existingFiles = rclone lsf --files-only \$rcloneRemote + Write-Output "Existing files:" + Write-Output \$existingFiles - \$env:RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' - - # Use rclone to upload the file to Google Drive - rclone copy "\$filePath" AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --progress --drive-chunk-size 64M + if (\$existingFiles -match "^\$fileName\$") { + Write-Output "File '\$fileName' already exists on Google Drive. Skipping upload." + } else { + Write-Output "Uploading '\$fileName' to Google Drive..." + \$filePath = "C:/Jenkins/workspace/AzaionSuite/suite/\$fileName" + \$env:RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' + rclone copy "\$filePath" \$rcloneRemote --progress --drive-chunk-size 64M + } """ } } @@ -77,25 +84,34 @@ pipeline { steps { echo "Cleaning up older files on Google Drive..." powershell """ - \$driveFolder = '${params.GOOGLE_DRIVE_FOLDER}' - - # List all files in the Google Drive folder - Write-Output "Listing all files in the folder \$driveFolder on Google Drive..." - \$files = rclone lsf AzaionGoogleDrive:\$driveFolder --files-only --max-depth 1 + 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 - if (\$files.Count -gt 0) { - \$filesSorted = \$files | Sort-Object -Descending + \$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 - foreach (\$file in \$filesToDelete) { - \$trimmedFile = \$file.Trim() - Write-Output "Deleting file: \$trimmedFile" - rclone deletefile AzaionGoogleDrive:\$driveFolder/\$trimmedFile + 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:${params.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."