From e6ec904657084e6194da97c9dd35c57487ae08c8 Mon Sep 17 00:00:00 2001 From: dzaitsev Date: Wed, 7 May 2025 18:41:44 +0300 Subject: [PATCH] updated pipelines zip and gdrive upload --- build/jenkins/GDriveUpload | 213 +++++++++++++++++-------------------- 1 file changed, 97 insertions(+), 116 deletions(-) diff --git a/build/jenkins/GDriveUpload b/build/jenkins/GDriveUpload index 7f777dc..afa9968 100644 --- a/build/jenkins/GDriveUpload +++ b/build/jenkins/GDriveUpload @@ -1,154 +1,135 @@ pipeline { agent { label 'Win10-BuildMachine' } + parameters { + string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/Azaion/suite', description: 'Build directory to zip from') + string(name: 'GOOGLE_DRIVE_FOLDER', defaultValue: 'AzaionSuiteBuilds', description: 'Target folder on Google Drive') + } + tools { git 'Default' } - parameters { - string(name: 'GOOGLE_DRIVE_FOLDER', defaultValue: 'AzaionSuiteBuilds', description: 'The folder on Google Drive where the build will be uploaded.') - 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' + SEVEN_ZIP_PATH = "C:/Program Files/7-Zip" + PATH = "${SEVEN_ZIP_PATH};${env.PATH}" + GOOGLE_DRIVE_UPLOAD_JOB_NAME = 'GDrive Upload' } stages { - 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') { + stage('Detect or Create Zip') { steps { script { - echo "Finding latest zip file in: ${params.buildPath}" + echo "Starting 'Detect or Create Zip' stage." + echo "Using build path: ${params.buildPath}" dir("${params.buildPath}") { - 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() + powershell ''' + $ErrorActionPreference = "Stop" + $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe" + $exePattern = "AzaionSuite*.exe" + $binPattern = "AzaionSuite*.bin" + $zipPattern = "AzaionSuite*.zip" - 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 - } + if (-not (Test-Path $sevenZipExe)) { + Write-Error "7-Zip not found at $sevenZipExe" + exit 1 + } + + $existingZips = Get-ChildItem -Path . -Filter $zipPattern | Sort-Object LastWriteTime -Descending + $zipFilename = "" + + if ($existingZips.Count -gt 0) { + $zipFilename = $existingZips[0].Name + Write-Host "Using existing zip file: $zipFilename" + $wasCreated = $false + } else { + $exeFile = Get-ChildItem -Recurse -Filter $exePattern | Select-Object -First 1 + if (-not $exeFile) { + Write-Error "No EXE file found to create zip" + exit 1 + } + + $zipBaseFilename = $exeFile.BaseName + $timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") + $zipFilename = "$zipBaseFilename-$timestamp.zip" + + $filesToZip = Get-ChildItem -Recurse -Include $exePattern, $binPattern | Select-Object -ExpandProperty FullName + if ($filesToZip.Count -eq 0) { + Write-Error "No files found to zip." + exit 1 + } + + $args = @("a", "-tzip", "$zipFilename") + ($filesToZip | ForEach-Object { "`"$_`"" }) + $process = Start-Process -FilePath $sevenZipExe -ArgumentList $args -Wait -NoNewWindow -PassThru + if ($process.ExitCode -ne 0) { + Write-Error "7-Zip failed with code $($process.ExitCode)" + exit $process.ExitCode + } + + $wasCreated = $true + Write-Host "Created new zip file: $zipFilename" + } + + Set-Content -Path "zipfilename.txt" -Value $zipFilename -Encoding ASCII + Set-Content -Path "zip_was_created.txt" -Value $wasCreated -Encoding ASCII + ''' + + def zipFilename = readFile('zipfilename.txt').trim() + def zipCreated = readFile('zip_was_created.txt').trim().toBoolean() + + echo "Zip filename: ${zipFilename}" + echo "Was zip created this run? ${zipCreated}" + + writeFile file: 'created_zip.txt', text: zipFilename + writeFile file: 'zip_was_created.txt', text: zipCreated.toString() } } } } - stage('Upload If Not Exists & Always Remove Local') { + stage('Archive Zip File (if new)') { when { - expression { return env.LATEST_ZIP_FILENAME?.trim() } + expression { + def zipCreated = readFile("${params.buildPath}/zip_was_created.txt").trim().toBoolean() + return zipCreated + } } steps { - echo "Checking Google Drive for existing ZIP and uploading if needed..." - powershell """ - \$fileName = "${env.LATEST_ZIP_FILENAME}" - \$folder = "${params.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." + script { + echo "Archiving newly created zip file..." + dir("${params.buildPath}") { + def zipFilename = readFile('created_zip.txt').trim() + if (!fileExists(zipFilename)) { + error "Zip file '${zipFilename}' not found!" + } + archiveArtifacts artifacts: "${zipFilename}", fingerprint: true } - - 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() } - } + stage('Trigger Google Drive Upload') { steps { - echo "Cleaning up older files on Google Drive..." - powershell """ - 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 - - \$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:${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." - } - """ + script { + echo "Triggering Google Drive Upload pipeline: ${env.GOOGLE_DRIVE_UPLOAD_JOB_NAME}" + build job: env.GOOGLE_DRIVE_UPLOAD_JOB_NAME, parameters: [ + string(name: 'buildPath', value: params.buildPath), + string(name: 'GOOGLE_DRIVE_FOLDER', value: params.GOOGLE_DRIVE_FOLDER) + ] + } } } } post { - always { - echo 'Executing post-build cleanup...' - powershell ''' - $uploadDir = "temp_upload" - if (Test-Path $uploadDir) { - Remove-Item -Recurse -Force $uploadDir - } - ''' + success { + script { + def zipFilename = readFile("${params.buildPath}/created_zip.txt").trim() + echo "Pipeline completed successfully. Final zip: ${zipFilename}" + } } - failure { - echo 'Pipeline failed. Check logs for details.' + echo "Pipeline failed." } } }