// 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 // First, create a PowerShell script to find the zip file using standard output powershell ''' $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 } # CRITICAL: Write the filename to a temporary file that we can read in Jenkins $zipFilename | Out-File -FilePath "zipfilename.txt" -Encoding utf8 -NoNewline Write-Host "Wrote zip filename to zipfilename.txt: $zipFilename" exit 0 ''' // Now read the file directly using readFile step def zipFilename = readFile(file: 'zipfilename.txt').trim() echo "Read zip filename from file: ${zipFilename}" // Set the environment variable env.CREATED_ZIP_FILENAME = zipFilename echo "Set CREATED_ZIP_FILENAME to: ${env.CREATED_ZIP_FILENAME}" // Nothing needed here since we now write the filename to a file and read it directly } } } } 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." } } }