// This pipeline job is triggered by the main AzaionSuite build pipeline. // It operates directly within the main build job's artifact directory, // zips the artifacts using PowerShell and 7-Zip, and then triggers the Google Drive upload pipeline. pipeline { // Agent should be your Windows VM agent // This job MUST run on the SAME agent as the main build job agent { label 'Win10-BuildMachine' } tools { // Git tool might be needed if this Jenkinsfile is in SCM git 'Default' } environment { // 7-Zip path (assuming default installation) 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}" // Define the name of your existing Google Drive upload pipeline job GOOGLE_DRIVE_UPLOAD_JOB_NAME = 'GDrive Upload' // Define the source directory for artifacts from the main build job's workspace MAIN_BUILD_ARTIFACTS_DIR = 'C:/Jenkins/workspace/AzaionSuite/suite' // Define the name of the created zip file as an environment variable CREATED_ZIP_FILENAME = '' } stages { stage('Archive Build Artifacts (PowerShell/7-Zip)') { steps { script { echo "Starting 'Archive Build Artifacts (PowerShell/7-Zip)' stage." dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { echo "Operating in directory: ${pwd()}" // 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" $exePattern = "AzaionSuite*.exe" $binPattern = "AzaionSuite*.bin" $zipPattern = "AzaionSuite*.zip" Write-Host "Operating in directory: $(Get-Location)" # Check if 7-Zip exists if (-not (Test-Path $sevenZipExe)) { Write-Error "7-Zip executable not found at $sevenZipExe" exit 1 } # Check for existing zip files $existingZips = Get-ChildItem -Path . -Filter $zipPattern | Sort-Object LastWriteTime -Descending $zipFilename = "" if ($existingZips.Count -gt 0) { # Found existing zip files, use the newest one $newestZip = $existingZips | Select-Object -First 1 $zipFilename = $newestZip.Name Write-Host "Using newest existing zip file: '$zipFilename'." } else { # No existing zip files, proceed with creation 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 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." # 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) { $zipBaseFilename = $exeFile.BaseName Write-Host "Using executable base filename: '$zipBaseFilename'" } else { Write-Host "No executable found. Using default: '$zipBaseFilename'" } # Get timestamp for filename $timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") $zipFilename = "$zipBaseFilename-$timestamp.zip" Write-Host "Creating zip archive: $zipFilename" try { # Build the 7z command arguments $sevenZipArgs = @("a", "-tzip", "$zipFilename") $foundFilesQuoted = $foundFiles | ForEach-Object { "`"$_`"" } $sevenZipArgs += $foundFilesQuoted # Execute the 7z command $process = Start-Process -FilePath $sevenZipExe -ArgumentList $sevenZipArgs -Wait -NoNewWindow -PassThru $exitCode = $process.ExitCode if ($exitCode -ne 0) { Write-Error "Error creating zip archive. 7z exit code: $exitCode" exit $exitCode } Write-Host "Zip archive created successfully: $zipFilename" } catch { Write-Error "Exception occurred during zip creation: $_" exit 1 } } # Verify the zip file exists before returning if (-not (Test-Path $zipFilename)) { Write-Error "Expected zip file $zipFilename does not exist" exit 1 } # This is the critical fix - we need to make sure this is the last thing output # And ensure it's on its own line with no other text Write-Host "ZIPFILENAME=$zipFilename" exit 0 ''' // Debug: Print raw PowerShell output to see what we're getting echo "Raw PowerShell output: ${zipFilenameOutput}" // The output may have Windows line endings, split by any common line ending def outputLines = zipFilenameOutput.split('(?:\\r\\n|\\n|\\r)') echo "Found ${outputLines.size()} lines in output" def zipFilename = null // Debug: Print each line of output for (int i = 0; i < outputLines.size(); i++) { echo "Line ${i}: ${outputLines[i]}" if (outputLines[i].trim().contains("ZIPFILENAME=")) { zipFilename = outputLines[i].trim().substring(outputLines[i].trim().indexOf("ZIPFILENAME=") + 11) echo "Found zip filename in line ${i}: ${zipFilename}" break } } if (zipFilename) { env.CREATED_ZIP_FILENAME = zipFilename.trim() echo "Setting CREATED_ZIP_FILENAME to: ${env.CREATED_ZIP_FILENAME}" } else { // If we can't find the ZIPFILENAME marker, try a fallback approach // Look for the line containing "Using newest existing zip file:" or "Zip archive created successfully:" for (int i = 0; i < outputLines.size(); i++) { def line = outputLines[i].trim() if (line.contains("Using newest existing zip file: '") && line.contains(".zip'")) { def start = line.indexOf("'") + 1 def end = line.lastIndexOf("'") if (start > 0 && end > start) { zipFilename = line.substring(start, end) echo "Extracted zip filename from 'Using newest' line: ${zipFilename}" break } } else if (line.contains("Zip archive created successfully: ") && line.contains(".zip")) { def start = line.indexOf("Zip archive created successfully: ") + 30 zipFilename = line.substring(start).trim() echo "Extracted zip filename from 'created successfully' line: ${zipFilename}" break } } if (zipFilename) { env.CREATED_ZIP_FILENAME = zipFilename.trim() echo "Setting CREATED_ZIP_FILENAME using fallback to: ${env.CREATED_ZIP_FILENAME}" } else { error "Failed to extract zip filename from PowerShell output after multiple attempts. Check PowerShell script." } } } } } } stage('Archive Created Zip') { steps { script { echo "Starting 'Archive Created Zip' stage." dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { echo "Operating in directory: ${pwd()}" // 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 { echo "Triggering Google Drive Upload pipeline: ${env.GOOGLE_DRIVE_UPLOAD_JOB_NAME}" 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." } } }