diff --git a/build/jenkins/zip b/build/jenkins/zip index 3b67a36..f59fe6e 100644 --- a/build/jenkins/zip +++ b/build/jenkins/zip @@ -30,7 +30,7 @@ pipeline { // 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 = '' // This will be set dynamically by reading a temp file } stages { @@ -39,15 +39,17 @@ pipeline { stage('Archive Build Artifacts (PowerShell/7-Zip)') { steps { - script { // Need script block for dir and powershell step + script { // Need script block for dir, powershell, and file operations 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 - def zipFilenameOutput = powershell returnStdout: true, script: ''' + // Define the name for the temporary file to store the zip filename + def tempFilenameFile = "zip_filename.txt" + + // Use a powershell step to check for existing zip, or create a new one, then write the filename to a temp file + powershell ''' $ErrorActionPreference = "Stop" # Stop the script on any error $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe" @@ -55,6 +57,7 @@ pipeline { $exePattern = "AzaionSuite*.exe" $binPattern = "AzaionSuite*.bin" $zipPattern = "AzaionSuite*.zip" # Pattern for existing zip files + $tempFilenameFile = "''' + tempFilenameFile + '''" # Pass temp filename to PowerShell Write-Host "Operating in directory: $(Get-Location)" @@ -154,18 +157,52 @@ pipeline { Write-Host "Zip archive created successfully by 7-Zip: $zipFilename" } - # Output the zip filename to standard output for the Groovy script to capture - # Ensure this is the very last thing written to the standard output stream - Write-Output $zipFilename + # Write the determined zip filename to a temporary file + Write-Host "Writing zip filename '$zipFilename' to temporary file '$tempFilenameFile'..." + $zipFilename | Out-File -Path $tempFilenameFile -Encoding UTF8 -Force exit 0 ''' // End powershell script - // Capture the output and set the environment variable - // The PowerShell script outputs the zip filename as the last line - // Trim any leading/trailing whitespace or newlines from the captured output - env.CREATED_ZIP_FILENAME = zipFilenameOutput.trim() - echo "Set CREATED_ZIP_FILENAME environment variable to: ${env.CREATED_ZIP_FILENAME}" + // Read the zip filename from the temporary file + def createdZipFilename = "" + def tempFilenameFile = "zip_filename.txt" // Must match the name used in PowerShell + + try { + echo "Attempting to read zip filename from temporary file: ${tempFilenameFile}" + createdZipFilename = readFile(tempFilenameFile).trim() + echo "Successfully read zip filename: ${createdZipFilename}" + } catch (FileNotFoundException e) { + // This might happen if the PowerShell script failed before writing the file + echo "Warning: Temporary filename file '${tempFilenameFile}' not found. Zip creation might have failed." + } finally { + // Clean up the temporary file + echo "Cleaning up temporary filename file: ${tempFilenameFile}" + deleteDir() // Use deleteDir() to remove files/dirs in the current dir block + // Or more specifically: fileOperations([genSandboxConfig: true, operations: [[excludes: null, includes: tempFilenameFile, pattern: null, type: 'DELETE']]]) + // Let's use deleteDir() within the dir block for simplicity here. Note: deleteDir() removes everything in the current dir block. + // A more targeted approach might be needed if other files must persist. + // For now, let's assume it's safe to clean the entire dir block. + // Wait, deleteDir() is too aggressive. Let's use the fileOperations step. + } + + // Re-implementing cleanup using fileOperations + try { + echo "Cleaning up temporary filename file: ${tempFilenameFile} using fileOperations" + fileOperations([genSandboxConfig: true, operations: [[excludes: null, includes: tempFilenameFile, pattern: null, type: 'DELETE']]]) + echo "Temporary filename file cleaned up." + } catch (Exception e) { + echo "Warning: Failed to clean up temporary filename file '${tempFilenameFile}': ${e.getMessage()}" + } + + + // Set the environment variable for subsequent stages + if (createdZipFilename && !createdZipFilename.trim().isEmpty()) { + env.CREATED_ZIP_FILENAME = createdZipFilename + echo "Set CREATED_ZIP_FILENAME environment variable to: ${env.CREATED_ZIP_FILENAME}" + } else { + error "Captured zip filename was empty or could not be read from the temporary file. Cannot archive." + } } // End dir block