From 926345efafa4da6f0fa422e5a9f3d1c0cc2c4a4c Mon Sep 17 00:00:00 2001 From: dzaitsev Date: Sun, 4 May 2025 13:37:18 +0300 Subject: [PATCH] zip update --- build/jenkins/zip | 173 +++++++++++++++++++++------------------------- 1 file changed, 79 insertions(+), 94 deletions(-) diff --git a/build/jenkins/zip b/build/jenkins/zip index 7cfb170..9886a48 100644 --- a/build/jenkins/zip +++ b/build/jenkins/zip @@ -1,6 +1,6 @@ // 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 Batch and 7-Zip, and then triggers the Google Drive upload pipeline. +// 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 @@ -37,10 +37,10 @@ pipeline { // Removed the 'Copy Build Artifacts' stage. // This pipeline will now operate directly in the MAIN_BUILD_ARTIFACTS_DIR. - stage('Archive Build Artifacts (Batch/7-Zip)') { + stage('Archive Build Artifacts (PowerShell/7-Zip)') { steps { - script { // Need script block for dir and bat step - echo "Starting 'Archive Build Artifacts (Batch/7-Zip)' stage." + 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()}" @@ -48,124 +48,109 @@ pipeline { // Define the expected artifact patterns relative to the current directory (MAIN_BUILD_ARTIFACTS_DIR) def exePattern = 'AzaionSuite*.exe' def binPattern = 'AzaionSuite*.bin' - // Define defaultVersion here, as it's used within the bat script if no exe is found + // 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 - // Use a bat step to perform file finding, version extraction, timestamp generation, and zipping - bat """ - @echo off - setlocal enabledelayedexpansion + \$sevenZipExe = "\$env:SEVEN_ZIP_PATH\\7z.exe" + \$defaultVersion = "${defaultVersion}" + \$exePattern = "${exePattern}" + \$binPattern = "${binPattern}" - set "SEVEN_ZIP_EXE=%SEVEN_ZIP_PATH%\\7z.exe" - set "DEFAULT_VERSION=${defaultVersion}" - set "EXE_PATTERN=${exePattern}" - set "BIN_PATTERN=${binPattern}" + Write-Host "Searching for files matching \$exePattern and \$binPattern in the current directory for zipping..." - echo Searching for files matching %EXE_PATTERN% and %BIN_PATTERN% in the current directory for zipping... + # Find all files matching the patterns + \$foundFiles = Get-ChildItem -Recurse -Path . -Include \$exePattern, \$binPattern | Select-Object -ExpandProperty FullName - set "FOUND_FILES=" - set "EXE_FILE=" - rem Find .exe and .bin files and build a list of quoted paths - for /R . %%F in (%EXE_PATTERN% %BIN_PATTERN%) do ( - rem DEBUG: Show file found - echo DEBUG: Found file: "%%F" - rem Add the quoted full path to FOUND_FILES - set "FOUND_FILES=!FOUND_FILES! "%%F"" - rem Check if it's an exe and store the first one found - rem Using findstr to check if the full path ends with .exe (case-insensitive) - rem Corrected findstr pattern - echo "%%F" | findstr /I /E ".exe" >nul && if not defined EXE_FILE set "EXE_FILE=%%F" - ) + if (\$foundFiles.Count -eq 0) { + Write-Error "No files matching patterns \$exePattern or \$binPattern found in \$(Get-Location)." + exit 1 + } - rem DEBUG: Show EXE_FILE after loop - if defined EXE_FILE ( - echo DEBUG: EXE_FILE set to: "%EXE_FILE%" - ) else ( - echo DEBUG: EXE_FILE remains undefined. - ) + 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 not defined FOUND_FILES ( - echo No files matching patterns %EXE_PATTERN% or %BIN_PATTERN% found in %CD%. - exit /b 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 - rem Count files (basic count based on spaces, might be inaccurate with complex paths) - set "file_count=0" - for %%F in (%FOUND_FILES%) do set /a file_count+=1 - echo Found !file_count! file(s) to archive. + # 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 --- - rem --- Determine Base Filename for Zip --- - set "ZIP_BASE_FILENAME=" - if defined EXE_FILE ( - echo Using executable filename as base for archive name: "%EXE_FILE%"... - rem Extract filename without extension from full path - for %%I in ("%EXE_FILE%") do set "ZIP_BASE_FILENAME=%%~nI" - echo Extracted base filename: !ZIP_BASE_FILENAME! - ) else ( - echo Warning: No executable found. Using default base filename: AzaionSuite.!DEFAULT_VERSION! - set "ZIP_BASE_FILENAME=AzaionSuite.!DEFAULT_VERSION!" - ) + # Get current date and time in YYYYMMDD-HHmmss format + \$timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") - rem DEBUG: Show ZIP_BASE_FILENAME before constructing zip name - echo DEBUG: ZIP_BASE_FILENAME is: !ZIP_BASE_FILENAME! + # Construct the zip filename using extracted version and timestamp + \$zipFilename = "AzaionSuite.\$version-\$timestamp.zip" + Write-Host "Creating zip archive: \$zipFilename using 7-Zip." - rem --- Zipping Logic --- + # Build the 7z command arguments + # Start with command, type, and quoted zip filename + \$sevenZipArgs = @("a", "-tzip", "\$zipFilename") - rem Get current date and time inYYYYMMDD-HHmmss format using wmic - rem wmic output format is LocalDateTime=YYYYMMDDHHMMSS.milliseconds+UTCoffset - for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set "dt=%%a" - set "YYYY=%dt:~0,4%" - set "MM=%dt:~4,2%" - set "DD=%dt:~6,2%" - set "HH=%dt:~8,2%" - set "mm=%dt:~10,2%" - set "SS=%dt:~12,2%" - set "timestamp=%YYYY%%MM%%DD%-%HH%%mm%%SS%" + # Add the list of found files (which are already quoted and space-separated in the Batch script, + # but Get-ChildItem returns objects, so we need to handle quoting for the command) + # Joining the array elements with spaces, ensuring each path is quoted + \$foundFilesQuoted = \$foundFiles | ForEach-Object { "\`"\$_\`"" } + \$sevenZipArgs += \$foundFilesQuoted - rem Construct the final zip filename using the base filename and timestamp - set "ZIP_FILENAME=!ZIP_BASE_FILENAME!.%timestamp%.zip" + # Construct the full command string for logging + \$commandString = "\$sevenZipExe \$(\$sevenZipArgs -join ' ')" + Write-Host "Executing command: \$commandString" - echo Creating zip archive: %ZIP_FILENAME% using 7-Zip. + # 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 - rem Build the 7z command string correctly - rem Start with quoted executable path, command, type, and quoted zip filename - set "SEVEN_ZIP_COMMAND="%SEVEN_ZIP_EXE%" a -tzip "%ZIP_FILENAME%"" + # 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 + } - rem Append the list of found files (which are already quoted and space-separated) - set "SEVEN_ZIP_COMMAND=%SEVEN_ZIP_COMMAND%%FOUND_FILES%" + 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" - echo Executing command: %SEVEN_ZIP_COMMAND% - - rem Execute the constructed command string - %SEVEN_ZIP_COMMAND% - - rem Check the last exit code from the external command - if %ERRORLEVEL% neq 0 ( - echo Error creating zip archive with 7-Zip. 7z exit code: %ERRORLEVEL% - exit /b %ERRORLEVEL% - ) - - echo Zip archive created successfully by 7-Zip: %ZIP_FILENAME% - - rem Output the zip filename and set it as an environment variable for Jenkins - rem This uses the Jenkins 'set context' feature - echo ::SET-ZIP-FILENAME::%ZIP_FILENAME% - echo ::SET-ENV::CREATED_ZIP_FILENAME=%ZIP_FILENAME% - - exit /b 0 - """ // End bat script + 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 Batch + 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}") {