From 366aab294e13a2464243c1577c116117f8c0bdeb Mon Sep 17 00:00:00 2001 From: dzaitsev Date: Sun, 4 May 2025 14:11:15 +0300 Subject: [PATCH] zip update --- build/jenkins/zip | 241 +++++++++++++++++++++------------------------- 1 file changed, 108 insertions(+), 133 deletions(-) diff --git a/build/jenkins/zip b/build/jenkins/zip index 48661c2..b3c27a5 100644 --- a/build/jenkins/zip +++ b/build/jenkins/zip @@ -9,7 +9,6 @@ pipeline { tools { // Git tool might be needed if this Jenkinsfile is in SCM git 'Default' - // dotnetsdk is not needed here as we only process artifacts } environment { @@ -17,203 +16,179 @@ pipeline { SEVEN_ZIP_PATH = "C:/Program Files/7-Zip" // Adjust if 7-Zip is installed elsewhere // Set the PATH environment variable including 7-Zip - PATH = "${SEVEN_ZIP_PATH};${env.PATH}" // Add 7-Zip to PATH + PATH = "${SEVEN_ZIP_PATH};${env.PATH}" // Define the name of your existing Google Drive upload pipeline job - // ** IMPORTANT: Replace 'YourGoogleDriveUploadPipelineName' with the actual name ** - GOOGLE_DRIVE_UPLOAD_JOB_NAME = 'GDrive Upload' // <<== UPDATE THIS + GOOGLE_DRIVE_UPLOAD_JOB_NAME = 'GDrive Upload' // Define the source directory for artifacts from the main build job's workspace - // This is the directory this pipeline will change into to operate. - // ** IMPORTANT: Replace 'C:/Jenkins/workspace/AzaionSuite/suite' with the actual path ** - MAIN_BUILD_ARTIFACTS_DIR = 'C:/Jenkins/workspace/AzaionSuite/suite' // <<== UPDATE THIS + MAIN_BUILD_ARTIFACTS_DIR = 'C:/Jenkins/workspace/AzaionSuite/suite' // Define the name of the created zip file as an environment variable - // This makes it easier to reference in later stages - CREATED_ZIP_FILENAME = '' // This will be set dynamically by capturing PowerShell output + CREATED_ZIP_FILENAME = '' } stages { - // Removed the 'Copy Build Artifacts' stage. - // This pipeline will now operate directly in the MAIN_BUILD_ARTIFACTS_DIR. - stage('Archive Build Artifacts (PowerShell/7-Zip)') { steps { - script { // Need script block for dir and powershell step + script { echo "Starting 'Archive Build Artifacts (PowerShell/7-Zip)' stage." - // Change directory to the main build job's artifacts folder dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { echo "Operating in directory: ${pwd()}" - // Use a powershell step to check for existing zip, or create a new one, then output the filename - // Capture the output of the powershell script + // Use a powershell step with improved error handling and robustness def zipFilenameOutput = powershell returnStdout: true, script: ''' $ErrorActionPreference = "Stop" # Stop the script on any error + # Define key variables $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe" - $defaultVersion = "1.0.0" # Default version if no exe is found + $defaultVersion = "1.0.0" $exePattern = "AzaionSuite*.exe" $binPattern = "AzaionSuite*.bin" - $zipPattern = "AzaionSuite*.zip" # Pattern for existing zip files + $zipPattern = "AzaionSuite*.zip" Write-Host "Operating in directory: $(Get-Location)" - # --- Debugging: List all items in the directory --- - Write-Host "DEBUG: Listing all items in the current directory:" - Get-ChildItem -Path . | ForEach-Object { Write-Host "DEBUG: Item: $($_.FullName)" } - Write-Host "DEBUG: End of item listing." - - # --- Check for existing zip files --- - Write-Host "Checking for existing zip files matching '$zipPattern' using -Filter..." - # Corrected: Using -Filter instead of -Include with -Path . - $existingZips = Get-ChildItem -Path . -Filter $zipPattern | Sort-Object LastWriteTime -Descending - - # --- Debugging: Show found zip files --- - Write-Host "DEBUG: Files found by Get-ChildItem -Path . -Filter '$zipPattern':" - if ($existingZips.Count -gt 0) { - $existingZips | ForEach-Object { Write-Host "DEBUG: Found zip: $($_.Name)" } - } else { - Write-Host "DEBUG: No zip files found by Get-ChildItem with pattern '$zipPattern' using -Filter." + # Check if 7-Zip exists + if (-not (Test-Path $sevenZipExe)) { + Write-Error "7-Zip executable not found at $sevenZipExe" + exit 1 } - Write-Host "DEBUG: End of found zip listing." - Write-Host "DEBUG: Existing zip files found count: $($existingZips.Count)" + # Check for existing zip files + $existingZips = Get-ChildItem -Path . -Filter $zipPattern | + Sort-Object LastWriteTime -Descending $zipFilename = "" - $zipFound = $false if ($existingZips.Count -gt 0) { # Found existing zip files, use the newest one $newestZip = $existingZips | Select-Object -First 1 $zipFilename = $newestZip.Name - $zipFound = $true - Write-Host "Using newest existing zip file: '$zipFilename'. Skipping creation process." - # Skip the rest of the script that creates a new zip + Write-Host "Using newest existing zip file: '$zipFilename'." } else { # No existing zip files, proceed with creation - Write-Host "No existing zip files found. Proceeding with file finding and zipping process." - - Write-Host "Searching for files matching $exePattern and $binPattern in the current directory for zipping..." + Write-Host "No existing zip files found. Creating new zip file." # Find all files matching the patterns - $foundFiles = Get-ChildItem -Recurse -Path . -Include \$exePattern, \$binPattern | Select-Object -ExpandProperty FullName + $foundFiles = Get-ChildItem -Recurse -Path . -Include $exePattern, $binPattern | + Select-Object -ExpandProperty FullName - if (\$foundFiles.Count -eq 0) { - Write-Error "No files matching patterns \$exePattern or \$binPattern found in \$(Get-Location)." + if ($foundFiles.Count -eq 0) { + Write-Error "No files matching patterns $exePattern or $binPattern found in $(Get-Location)." exit 1 } - Write-Host "Found \$(\$foundFiles.Count) file(s) to archive." + Write-Host "Found $($foundFiles.Count) file(s) to archive." - # --- Determine Base Filename for Zip (from .exe if present) --- - \$zipBaseFilename = "AzaionSuite.\$defaultVersion" # Default base filename - \$exeFile = Get-ChildItem -Recurse -Path . -Filter \$exePattern | Select-Object -First 1 + # Determine Base Filename for Zip (from .exe if present) + $zipBaseFilename = "AzaionSuite.$defaultVersion" # Default + $exeFile = Get-ChildItem -Recurse -Path . -Filter $exePattern | + Select-Object -First 1 - if (\$exeFile) { - Write-Host "Executable file found: '\$(\$exeFile.FullName)'" - # Extract filename without extension - \$zipBaseFilename = \$exeFile.BaseName - Write-Host "Using executable base filename for archive name: '\$zipBaseFilename'" + if ($exeFile) { + $zipBaseFilename = $exeFile.BaseName + Write-Host "Using executable base filename: '$zipBaseFilename'" } else { - Write-Warning "No executable found matching \$exePattern. Using default base filename: '\$zipBaseFilename'" + Write-Host "No executable found. Using default: '$zipBaseFilename'" } - # --- Zipping Logic --- + # Get timestamp for filename + $timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") + $zipFilename = "$zipBaseFilename-$timestamp.zip" - # Get current date and time inYYYYMMDD-HHmmss format - \$timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") + Write-Host "Creating zip archive: $zipFilename" - # Construct the zip filename using the base filename and timestamp - \$zipFilename = "\$zipBaseFilename-\$timestamp.zip" + try { + # Build the 7z command arguments + $sevenZipArgs = @("a", "-tzip", "$zipFilename") + $foundFilesQuoted = $foundFiles | ForEach-Object { "`"$_`"" } + $sevenZipArgs += $foundFilesQuoted - Write-Host "Creating zip archive: \$zipFilename using 7-Zip." + # Execute the 7z command + $process = Start-Process -FilePath $sevenZipExe -ArgumentList $sevenZipArgs -Wait -NoNewWindow -PassThru + $exitCode = $process.ExitCode - # Build the 7z command arguments - # Start with command, type, and quoted zip filename - \$sevenZipArgs = @("a", "-tzip", "\$zipFilename") + if ($exitCode -ne 0) { + Write-Error "Error creating zip archive. 7z exit code: $exitCode" + exit $exitCode + } - # Add the list of found files, ensuring each path is quoted - # Using backticks to escape quotes within the PowerShell string - \$foundFilesQuoted = \$foundFiles | ForEach-Object { "\`"$_`"" } - \$sevenZipArgs += \$foundFilesQuoted - - # Construct the full command string for logging - \$commandString = "\$sevenZipExe \$(\$sevenZipArgs -join ' ')" - Write-Host "Executing command: \$commandString" - - # Execute the 7z command - # Using Start-Process with -Wait to ensure the script waits for 7z to finish - # and capturing the exit code - \$process = Start-Process -FilePath \$sevenZipExe -ArgumentList \$sevenZipArgs -Wait -PassThru - \$exitCode = \$process.ExitCode - - # Check the last exit code from the external command - if (\$exitCode -ne 0) { - Write-Error "Error creating zip archive with 7-Zip. 7z exit code: \$exitCode" - exit \$exitCode + Write-Host "Zip archive created successfully: $zipFilename" + } + catch { + Write-Error "Exception occurred during zip creation: $_" + exit 1 } - - Write-Host "Zip archive created successfully by 7-Zip: \$zipFilename" } - # Output the determined zip filename to standard output for the Groovy script to capture - # Ensure this is the very last thing written to the standard output stream - # This output will be captured by returnStdout: true - Write-Output \$zipFilename + # Verify the zip file exists before returning + if (-not (Test-Path $zipFilename)) { + Write-Error "Expected zip file $zipFilename does not exist" + exit 1 + } + # Output the final zip filename + Write-Output $zipFilename exit 0 - ''' // End powershell script + ''' - // Capture the output and set the environment variable - // The PowerShell script is designed to output ONLY the zip filename to standard output + // Trim the output and set the environment variable env.CREATED_ZIP_FILENAME = zipFilenameOutput.trim() - echo "Set CREATED_ZIP_FILENAME environment variable to: ${env.CREATED_ZIP_FILENAME}" - - } // End dir block + echo "Zip filename: ${env.CREATED_ZIP_FILENAME}" + } } } } + stage('Archive Created Zip') { - steps { - script { // Need script block for dir and accessing environment variables set by PowerShell - echo "Starting 'Archive Created Zip' stage." - // Change directory back to the main build job's artifacts folder to archive the zip - dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { - echo "Operating in directory: ${pwd()}" - // The zip filename was set as an environment variable in the previous stage - def createdZipFilename = env.CREATED_ZIP_FILENAME + steps { + script { + echo "Starting 'Archive Created Zip' stage." + dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { + echo "Operating in directory: ${pwd()}" - if (createdZipFilename && !createdZipFilename.trim().isEmpty()) { - echo "Identified created zip file for archiving: ${createdZipFilename}" - // Archive the created zip file using Jenkins built-in step - // The zip file is created in the MAIN_BUILD_ARTIFACTS_DIR by the Batch script - archiveArtifacts artifacts: "${createdZipFilename}", fingerprint: true - echo "Archive step completed." - } else { - // This error should now be less likely with improved output capturing - error "CREATED_ZIP_FILENAME environment variable was not set or was empty. Cannot archive." - } - } // End dir block - } - } - } + // Verify zip filename was set properly + if (!env.CREATED_ZIP_FILENAME?.trim()) { + error "CREATED_ZIP_FILENAME environment variable was not set properly." + } + // Verify the file exists before attempting to archive + def fileExists = fileExists env.CREATED_ZIP_FILENAME + if (!fileExists) { + error "File ${env.CREATED_ZIP_FILENAME} does not exist at ${pwd()}." + } + + echo "Archiving zip file: ${env.CREATED_ZIP_FILENAME}" + archiveArtifacts artifacts: "${env.CREATED_ZIP_FILENAME}", fingerprint: true + echo "Archive step completed." + } + } + } + } stage('Trigger Google Drive Upload') { steps { - script { // This stage still requires a script block for the build step + script { echo "Triggering Google Drive Upload pipeline: ${env.GOOGLE_DRIVE_UPLOAD_JOB_NAME}" - // build job is a Jenkins Pipeline step, cannot be replaced by Batch directly. - // Trigger the Google Drive upload pipeline - // This assumes the Google Drive job is configured to copy artifacts - // from THIS job (the one creating the zip and archiving it). - // The 'build' step executes from the current directory, which is inside the dir block. - dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { - echo "Operating in directory: ${pwd()}" - build job: env.GOOGLE_DRIVE_UPLOAD_JOB_NAME - } // End dir block - } // End script block - } // End steps block - } // End stage block - } // End of stages block -} // End of pipeline block + try { + build job: env.GOOGLE_DRIVE_UPLOAD_JOB_NAME + echo "Google Drive Upload pipeline triggered successfully." + } catch (Exception e) { + echo "Failed to trigger Google Drive Upload pipeline: ${e.message}" + error "Failed to trigger Google Drive Upload pipeline. See console log for details." + } + } + } + } + } + + post { + success { + echo "Pipeline completed successfully. Created and archived zip: ${env.CREATED_ZIP_FILENAME}" + } + failure { + echo "Pipeline failed. See logs for details." + } + } +} \ No newline at end of file