// 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 } 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 and powershell step 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 expected artifact patterns relative to the current directory (MAIN_BUILD_ARTIFACTS_DIR) // These are now defined as literals within the PowerShell script string // def exePattern = 'AzaionSuite*.exe' // def binPattern = 'AzaionSuite*.bin' // Define defaultVersion here, used if version extraction fails // def defaultVersion = '1.0.0' // Use a powershell step to perform file finding, version extraction, timestamp generation, and zipping powershell ''' $ErrorActionPreference = "Stop" # Stop the script on any error $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe" $defaultVersion = "1.0.0" # Default version if extraction fails $exePattern = "AzaionSuite*.exe" $binPattern = "AzaionSuite*.bin" 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." # --- Version Extraction (from .exe if present) --- $version = $defaultVersion $exeFile = Get-ChildItem -Recurse -Path . -Filter $exePattern | Select-Object -First 1 if ($exeFile) { Write-Host "Attempting to extract version from '$($exeFile.FullName)'..." try { # Get file version info $versionInfo = Get-ItemProperty -Path $exeFile.FullName -Name VersionInfo # Prefer ProductVersion, fallback to FileVersion if ($versionInfo.ProductVersion) { $version = $versionInfo.ProductVersion Write-Host "Extracted ProductVersion: $version" } elseif ($versionInfo.FileVersion) { $version = $versionInfo.FileVersion Write-Host "Extracted FileVersion: $version" } else { Write-Warning "Could not extract ProductVersion or FileVersion from '$($exeFile.Name)'. Using default: $version" } } catch { Write-Warning "Error extracting version from '$($exeFile.Name)': $_`nUsing default: $version" } } else { Write-Warning "No executable found matching $exePattern to extract version. Using default: $version" } # --- Zipping Logic --- # Get current date and time in YYYYMMDD-HHmmss format $timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") # Construct the zip filename using extracted version and timestamp $zipFilename = "AzaionSuite.$version-$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" # Output the zip filename and set it as an environment variable for Jenkins # This uses the Jenkins 'set context' feature Write-Host "::SET-ZIP-FILENAME::$zipFilename" Write-Host "::SET-ENV::CREATED_ZIP_FILENAME=$zipFilename" exit 0 ''' // End powershell script } // 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) { 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 { error "CREATED_ZIP_FILENAME environment variable was not set. 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