zip update

This commit is contained in:
dzaitsev
2025-05-04 13:38:15 +03:00
parent 926345efaf
commit 1277ebffc6
+49 -48
View File
@@ -46,104 +46,105 @@ pipeline {
echo "Operating in directory: ${pwd()}" echo "Operating in directory: ${pwd()}"
// Define the expected artifact patterns relative to the current directory (MAIN_BUILD_ARTIFACTS_DIR) // Define the expected artifact patterns relative to the current directory (MAIN_BUILD_ARTIFACTS_DIR)
def exePattern = 'AzaionSuite*.exe' // These are now defined as literals within the PowerShell script string
def binPattern = 'AzaionSuite*.bin' // def exePattern = 'AzaionSuite*.exe'
// def binPattern = 'AzaionSuite*.bin'
// Define defaultVersion here, used if version extraction fails // Define defaultVersion here, used if version extraction fails
def defaultVersion = '1.0.0' // def defaultVersion = '1.0.0'
// Use a powershell step to perform file finding, version extraction, timestamp generation, and zipping // Use a powershell step to perform file finding, version extraction, timestamp generation, and zipping
powershell """ powershell '''
\$ErrorActionPreference = "Stop" # Stop the script on any error $ErrorActionPreference = "Stop" # Stop the script on any error
\$sevenZipExe = "\$env:SEVEN_ZIP_PATH\\7z.exe" $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe"
\$defaultVersion = "${defaultVersion}" $defaultVersion = "1.0.0" # Default version if extraction fails
\$exePattern = "${exePattern}" $exePattern = "AzaionSuite*.exe"
\$binPattern = "${binPattern}" $binPattern = "AzaionSuite*.bin"
Write-Host "Searching for files matching \$exePattern and \$binPattern in the current directory for zipping..." Write-Host "Searching for files matching $exePattern and $binPattern in the current directory for zipping..."
# Find all files matching the patterns # Find all files matching the patterns
\$foundFiles = Get-ChildItem -Recurse -Path . -Include \$exePattern, \$binPattern | Select-Object -ExpandProperty FullName $foundFiles = Get-ChildItem -Recurse -Path . -Include $exePattern, $binPattern | Select-Object -ExpandProperty FullName
if (\$foundFiles.Count -eq 0) { if ($foundFiles.Count -eq 0) {
Write-Error "No files matching patterns \$exePattern or \$binPattern found in \$(Get-Location)." Write-Error "No files matching patterns $exePattern or $binPattern found in $(Get-Location)."
exit 1 exit 1
} }
Write-Host "Found \$(\$foundFiles.Count) file(s) to archive." Write-Host "Found $($foundFiles.Count) file(s) to archive."
# --- Version Extraction (from .exe if present) --- # --- Version Extraction (from .exe if present) ---
\$version = \$defaultVersion $version = $defaultVersion
\$exeFile = Get-ChildItem -Recurse -Path . -Filter \$exePattern | Select-Object -First 1 $exeFile = Get-ChildItem -Recurse -Path . -Filter $exePattern | Select-Object -First 1
if (\$exeFile) { if ($exeFile) {
Write-Host "Attempting to extract version from '\$($exeFile.FullName)'..." Write-Host "Attempting to extract version from '$($exeFile.FullName)'..."
try { try {
# Get file version info # Get file version info
\$versionInfo = Get-ItemProperty -Path \$exeFile.FullName -Name VersionInfo $versionInfo = Get-ItemProperty -Path $exeFile.FullName -Name VersionInfo
# Prefer ProductVersion, fallback to FileVersion # Prefer ProductVersion, fallback to FileVersion
if (\$versionInfo.ProductVersion) { if ($versionInfo.ProductVersion) {
\$version = \$versionInfo.ProductVersion $version = $versionInfo.ProductVersion
Write-Host "Extracted ProductVersion: \$version" Write-Host "Extracted ProductVersion: $version"
} elseif (\$versionInfo.FileVersion) { } elseif ($versionInfo.FileVersion) {
\$version = \$versionInfo.FileVersion $version = $versionInfo.FileVersion
Write-Host "Extracted FileVersion: \$version" Write-Host "Extracted FileVersion: $version"
} else { } else {
Write-Warning "Could not extract ProductVersion or FileVersion from '\$($exeFile.Name)'. Using default: \$version" Write-Warning "Could not extract ProductVersion or FileVersion from '$($exeFile.Name)'. Using default: $version"
} }
} catch { } catch {
Write-Warning "Error extracting version from '\$($exeFile.Name)': \$_\nUsing default: \$version" Write-Warning "Error extracting version from '$($exeFile.Name)': $_`nUsing default: $version"
} }
} else { } else {
Write-Warning "No executable found matching \$exePattern to extract version. Using default: \$version" Write-Warning "No executable found matching $exePattern to extract version. Using default: $version"
} }
# --- Zipping Logic --- # --- Zipping Logic ---
# Get current date and time in YYYYMMDD-HHmmss format # Get current date and time in YYYYMMDD-HHmmss format
\$timestamp = (Get-Date -Format "yyyyMMdd-HHmmss") $timestamp = (Get-Date -Format "yyyyMMdd-HHmmss")
# Construct the zip filename using extracted version and timestamp # Construct the zip filename using extracted version and timestamp
\$zipFilename = "AzaionSuite.\$version-\$timestamp.zip" $zipFilename = "AzaionSuite.$version-$timestamp.zip"
Write-Host "Creating zip archive: \$zipFilename using 7-Zip." Write-Host "Creating zip archive: $zipFilename using 7-Zip."
# Build the 7z command arguments # Build the 7z command arguments
# Start with command, type, and quoted zip filename # Start with command, type, and quoted zip filename
\$sevenZipArgs = @("a", "-tzip", "\$zipFilename") $sevenZipArgs = @("a", "-tzip", "$zipFilename")
# Add the list of found files (which are already quoted and space-separated in the Batch script, # Add the list of found files, ensuring each path is quoted
# but Get-ChildItem returns objects, so we need to handle quoting for the command) # Using backticks to escape quotes within the PowerShell string
# Joining the array elements with spaces, ensuring each path is quoted $foundFilesQuoted = $foundFiles | ForEach-Object { "`"$_`"" }
\$foundFilesQuoted = \$foundFiles | ForEach-Object { "\`"\$_\`"" } $sevenZipArgs += $foundFilesQuoted
\$sevenZipArgs += \$foundFilesQuoted
# Construct the full command string for logging # Construct the full command string for logging
\$commandString = "\$sevenZipExe \$(\$sevenZipArgs -join ' ')" $commandString = "$sevenZipExe $($sevenZipArgs -join ' ')"
Write-Host "Executing command: \$commandString" Write-Host "Executing command: $commandString"
# Execute the 7z command # Execute the 7z command
# Using Start-Process with -Wait to ensure the script waits for 7z to finish # Using Start-Process with -Wait to ensure the script waits for 7z to finish
# and capturing the exit code # and capturing the exit code
\$process = Start-Process -FilePath \$sevenZipExe -ArgumentList \$sevenZipArgs -Wait -PassThru $process = Start-Process -FilePath $sevenZipExe -ArgumentList $sevenZipArgs -Wait -PassThru
\$exitCode = \$process.ExitCode $exitCode = $process.ExitCode
# Check the last exit code from the external command # Check the last exit code from the external command
if (\$exitCode -ne 0) { if ($exitCode -ne 0) {
Write-Error "Error creating zip archive with 7-Zip. 7z exit code: \$exitCode" Write-Error "Error creating zip archive with 7-Zip. 7z exit code: $exitCode"
exit \$exitCode exit $exitCode
} }
Write-Host "Zip archive created successfully by 7-Zip: \$zipFilename" Write-Host "Zip archive created successfully by 7-Zip: $zipFilename"
# Output the zip filename and set it as an environment variable for Jenkins # Output the zip filename and set it as an environment variable for Jenkins
# This uses the Jenkins 'set context' feature # This uses the Jenkins 'set context' feature
Write-Host "::SET-ZIP-FILENAME::\$zipFilename" Write-Host "::SET-ZIP-FILENAME::$zipFilename"
Write-Host "::SET-ENV::CREATED_ZIP_FILENAME=\$zipFilename" Write-Host "::SET-ENV::CREATED_ZIP_FILENAME=$zipFilename"
exit 0 exit 0
""" // End powershell script ''' // End powershell script
} // End dir block } // End dir block
} }
} }