pipelines zip and gdrive upload

updated
This commit is contained in:
dzaitsev
2025-05-07 20:11:54 +03:00
parent cea1c69fec
commit 16c264ec9a
+85 -49
View File
@@ -10,9 +10,8 @@ pipeline {
} }
environment { environment {
GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder on Google Drive
RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf'
LATEST_ZIP_FILENAME = '' // Initial value
} }
stages { stages {
@@ -31,32 +30,36 @@ pipeline {
stage('Find Latest Zip') { stage('Find Latest Zip') {
steps { steps {
script { script {
echo "Using build path: ${params.buildPath}" echo "Using build path: ${params.buildPath}" // Debug output for build path
def latestZip = '' echo "Finding latest zip file in: ${params.buildPath}"
def latestZipFilename = ''
dir("${params.buildPath}") { dir("${params.buildPath}") {
def output = powershell(returnStdout: true, script: ''' def output = powershell(returnStdout: true, script: '''
$pattern = "AzaionSuite*.zip" $pattern = "AzaionSuite*.zip"
$zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending $zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending
if ($zipFiles.Count -gt 0) { if ($zipFiles.Count -gt 0) {
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$($zipFiles[0].Name)" $latestZip = $zipFiles[0].Name
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$latestZip"
} else { } else {
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=" Write-Output "::SET-ENV::LATEST_ZIP_FILENAME="
} }
''').trim() ''').trim()
// Debug output to verify the response from the powershell script
echo "PowerShell output: ${output}" echo "PowerShell output: ${output}"
def match = (output =~ /::SET-ENV::LATEST_ZIP_FILENAME=(.+)/) def match = output =~ /::SET-ENV::LATEST_ZIP_FILENAME=(.+)/
if (match) { if (!match || !match[0][1]?.trim()) {
latestZip = match[0][1] echo "⚠️ No ZIP files matching the pattern were found. Skipping upload and cleanup stages."
echo "✅ Latest zip file selected: ${latestZip}"
} else { } else {
echo "⚠️ No zip found." latestZipFilename = match[0][1]
latestZip = 'none' // Handle case where no file is found echo "✅ Latest zip file selected: ${latestZipFilename}"
} }
} }
// Directly assign the value to the env variable for use in other stages
env.LATEST_ZIP_FILENAME = latestZip // Pass the filename to the next stages directly
env.LATEST_ZIP_FILENAME = latestZipFilename
echo "LATEST_ZIP_FILENAME after processing: ${env.LATEST_ZIP_FILENAME}"
} }
} }
} }
@@ -64,31 +67,36 @@ pipeline {
stage('Upload If Not Exists & Always Remove Local') { stage('Upload If Not Exists & Always Remove Local') {
when { when {
expression { expression {
return env.LATEST_ZIP_FILENAME != 'none' echo "LATEST_ZIP_FILENAME is: '${env.LATEST_ZIP_FILENAME}'" // Debug output
return env.LATEST_ZIP_FILENAME?.trim()
} }
} }
steps { steps {
echo "Uploading ${env.LATEST_ZIP_FILENAME} if needed..." echo "Checking Google Drive for existing ZIP and uploading if needed..."
powershell """ powershell """
\$fileName = "${env.LATEST_ZIP_FILENAME}" \$fileName = "${env.LATEST_ZIP_FILENAME}"
\$folder = "${GOOGLE_DRIVE_FOLDER}" \$folder = "${GOOGLE_DRIVE_FOLDER}"
\$rcloneRemote = "AzaionGoogleDrive:\$folder" \$rcloneRemote = "AzaionGoogleDrive:\$folder"
\$localPath = "${params.buildPath}/\$fileName" \$localFilePath = "${params.buildPath}/\$fileName"
Write-Output "Existing on drive:" Write-Output "Checking for existing files in: \$rcloneRemote"
\$existing = rclone lsf --files-only \$rcloneRemote \$existingFiles = rclone lsf --files-only \$rcloneRemote
Write-Output \$existing Write-Output "Existing files:"
Write-Output \$existingFiles
if (\$existing -match "^\$fileName\$") { if (\$existingFiles -match "^\$fileName\$") {
Write-Output "Already exists; skipping upload." Write-Output "File '\$fileName' already exists on Google Drive. Skipping upload."
} else { } else {
Write-Output "Uploading..." Write-Output "Uploading '\$fileName' to Google Drive..."
rclone copy \$localPath \$rcloneRemote --drive-chunk-size 64M --progress rclone copy "\$localFilePath" \$rcloneRemote --progress --drive-chunk-size 64M
Write-Output "Upload complete."
} }
if (Test-Path \$localPath) { if (Test-Path \$localFilePath) {
Remove-Item -Force \$localPath Remove-Item -Force \$localFilePath
Write-Output "Local deleted." Write-Output "Local file deleted: \$localFilePath"
} else {
Write-Output "Local file already deleted or not found."
} }
""" """
} }
@@ -97,35 +105,59 @@ pipeline {
stage('Cleanup Older Files on Google Drive') { stage('Cleanup Older Files on Google Drive') {
when { when {
expression { expression {
return env.LATEST_ZIP_FILENAME != 'none' echo "LATEST_ZIP_FILENAME is: '${env.LATEST_ZIP_FILENAME}'" // Debug output
return env.LATEST_ZIP_FILENAME?.trim()
} }
} }
steps { steps {
echo "Cleaning up older files..." echo "Cleaning up older files on Google Drive..."
powershell """ powershell """
\$folder = "${GOOGLE_DRIVE_FOLDER}" Write-Output "Listing all files in the folder ${GOOGLE_DRIVE_FOLDER} on Google Drive..."
\$files = rclone lsf --files-only AzaionGoogleDrive:\$folder | Where-Object { \$_ -match '^AzaionSuite.*\\.zip\$' } \$files = rclone lsf --files-only AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
Write-Output "Files found on Google Drive:"
Write-Output \$files
# Build an array of objects with parsed DateTime # Extract date-time from filenames and sort them
\$meta = foreach (\$f in \$files) { \$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" }
if (\$f -match 'AzaionSuite\\.Iterative\\.(\\d{8})-(\\d{6})\\.zip') { \$filesWithDateTime = @()
\$dt = [datetime]::ParseExact(\$matches[1] + \$matches[2], 'yyyyMMddHHmmss', \$null)
} elseif (\$f -match 'AzaionSuite1\\.\\d+\\.\\d+\\.\\d+-(\\d{8})-(\\d{6})\\.zip') { foreach (\$file in \$filesArray) {
\$dt = [datetime]::ParseExact(\$matches[1] + \$matches[2], 'yyyyMMddHHmmss', \$null) if (\$file -match "AzaionSuite.Iterative.(\d{8})-(\d{6}).zip") {
} else { \$date = [datetime]::ParseExact(\$matches[1], "yyyyMMdd", $null)
# if it doesn't match expected formats, skip \$time = [datetime]::ParseExact(\$matches[2], "HHmmss", $null)
continue \$dateTime = \$date.Add(\$time.TimeOfDay)
\$filesWithDateTime += [PSCustomObject]@{
FileName = \$file
DateTime = \$dateTime
}
} }
[PSCustomObject]@{ Name = \$f; Date = \$dt }
} }
# Sort by Date descending, skip the first 3 (newest), delete the rest # Sort files based on DateTime in descending order
\$toDelete = \$meta | Sort-Object Date -Descending | Select-Object -Skip 3 \$sortedFiles = \$filesWithDateTime | Sort-Object DateTime -Descending
if (\$toDelete) { Write-Output "Sorted files (newest to oldest):"
Write-Output "Deleting:\" \$sortedFiles | ForEach-Object { Write-Output "\$($_.FileName)" }
\$toDelete | ForEach-Object { Write-Output \$_ .Name; rclone deletefile AzaionGoogleDrive:\$folder/\$($_.Name) }
# Keep the latest 3 files and delete the rest
\$filesToDelete = \$sortedFiles | Select-Object -Skip 3
Write-Output "Files to delete (older than 3 latest):"
\$filesToDelete | ForEach-Object { Write-Output "\$($_.FileName)" }
if (\$filesToDelete.Count -gt 0) {
\$tempFile = [System.IO.Path]::GetTempFileName()
\$filesToDelete.FileName | Set-Content -Path \$tempFile -Encoding utf8
Write-Output "Contents of temporary delete list file (\$tempFile):"
Get-Content \$tempFile
foreach (\$file in \$filesToDelete.FileName) {
Write-Output "Deleting \$file..."
rclone deletefile AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}/\$file
}
Remove-Item -Path \$tempFile
} else { } else {
Write-Output "Nothing to delete." Write-Output "No files to delete."
} }
""" """
} }
@@ -134,13 +166,17 @@ pipeline {
post { post {
always { always {
echo 'Cleaning up temp files...' echo 'Executing post-build cleanup...'
powershell ''' powershell '''
if (Test-Path temp_upload) { Remove-Item -Recurse -Force temp_upload } $uploadDir = "temp_upload"
if (Test-Path $uploadDir) {
Remove-Item -Recurse -Force $uploadDir
}
''' '''
} }
failure { failure {
echo 'Pipeline failed; check the logs above.' echo 'Pipeline failed. Check logs for details.'
} }
} }
} }