latest fixes for Gdrive upload

This commit is contained in:
dzaitsev
2025-05-04 15:31:12 +03:00
parent 4703c73a24
commit 7fc51e19ee
+35 -19
View File
@@ -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
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'
# Use rclone to upload the file to Google Drive
rclone copy "\$filePath" AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --progress --drive-chunk-size 64M
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
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) {
\$trimmedFile = \$file.Trim()
Write-Output "Deleting file: \$trimmedFile"
rclone deletefile AzaionGoogleDrive:\$driveFolder/\$trimmedFile
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."