// 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' // dotnetsdk is not needed here as we only process artifacts } 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}" // Add 7-Zip to 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 // 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 // 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 reading a temp file } 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, 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()}" // 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" $defaultVersion = "1.0.0" # Default version if no exe is found $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)" # --- 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." } Write-Host "DEBUG: End of found zip listing." $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 "Found existing zip file: '$zipFilename'. Skipping creation." } else { # No existing zip files, proceed with creation Write-Host "No existing zip files found. Proceeding with creation." Write-Host "Searching for files matching $exePattern and $binPattern in the current directory for zipping..." # 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 base filename $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'" } else { Write-Warning "No executable found matching $exePattern. Using default base filename: '$zipBaseFilename'" } # --- Zipping Logic --- # Get current date and time inYYYYMMDD-HHmmss format $timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") # Construct the zip filename using the base filename and timestamp $zipFilename = "$zipBaseFilename-$timestamp.zip" Write-Host "Creating zip archive: $zipFilename using 7-Zip." # Build the 7z command arguments # Start with command, type, and quoted zip filename $sevenZipArgs = @("a", "-tzip", "$zipFilename") # 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 by 7-Zip: $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 // Read the zip filename from the temporary file def createdZipFilename = "" // Removed the duplicate declaration of tempFilenameFile here. 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 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 } } } 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 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 } } } stage('Trigger Google Drive Upload') { steps { script { // This stage still requires a script block for the build step 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