diff --git a/build/jenkins/zip b/build/jenkins/zip index acdad9d..8d8a938 100644 --- a/build/jenkins/zip +++ b/build/jenkins/zip @@ -1,31 +1,15 @@ -// 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 + SEVEN_ZIP_PATH = "C:/Program Files/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 { @@ -36,12 +20,8 @@ pipeline { 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 + $ErrorActionPreference = "Stop" $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe" $defaultVersion = "1.0.0" $exePattern = "AzaionSuite*.exe" @@ -50,100 +30,73 @@ pipeline { 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 | + $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)." + Write-Error "No files found to zip." 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 - + $zipBaseFilename = "AzaionSuite.$defaultVersion" + $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 + if ($process.ExitCode -ne 0) { + Write-Error "7-Zip failed with code $($process.ExitCode)" + exit $process.ExitCode } - - Write-Host "Zip archive created successfully: $zipFilename" } catch { - Write-Error "Exception occurred during zip creation: $_" + Write-Error "Zip creation failed: $_" 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" + # Write zip filename WITHOUT BOM (ASCII encoding) + Set-Content -Path "zipfilename.txt" -Value $zipFilename -Encoding ASCII + Write-Host "Zip filename written to zipfilename.txt: $zipFilename" exit 0 ''' - // Now read the file directly using readFile step - def zipFilename = readFile(file: 'zipfilename.txt').trim() + // Read and clean the filename + def zipFilename = readFile('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 + // Save to file for next stage + writeFile file: 'created_zip.txt', text: zipFilename } } } @@ -154,22 +107,19 @@ pipeline { script { echo "Starting 'Archive Created Zip' stage." dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") { + def zipFilename = readFile('created_zip.txt').trim() 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." + if (!zipFilename) { + error "Zip filename not found or is empty." } - // 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()}." + if (!fileExists(zipFilename)) { + error "File ${zipFilename} does not exist." } - echo "Archiving zip file: ${env.CREATED_ZIP_FILENAME}" - archiveArtifacts artifacts: "${env.CREATED_ZIP_FILENAME}", fingerprint: true - echo "Archive step completed." + echo "Archiving zip file: ${zipFilename}" + archiveArtifacts artifacts: "${zipFilename}", fingerprint: true } } } @@ -184,7 +134,7 @@ pipeline { 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." + error "Trigger failed. See logs." } } } @@ -193,10 +143,13 @@ pipeline { post { success { - echo "Pipeline completed successfully. Created and archived zip: ${env.CREATED_ZIP_FILENAME}" + script { + def zipFilename = readFile("${env.MAIN_BUILD_ARTIFACTS_DIR}/created_zip.txt").trim() + echo "Pipeline completed successfully. Created and archived zip: ${zipFilename}" + } } failure { echo "Pipeline failed. See logs for details." } } -} \ No newline at end of file +}