mirror of
https://github.com/azaion/annotations.git
synced 2026-04-23 00:46:31 +00:00
zip update
This commit is contained in:
+40
-68
@@ -30,7 +30,7 @@ pipeline {
|
||||
|
||||
// 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
|
||||
CREATED_ZIP_FILENAME = '' // This will be set dynamically by capturing PowerShell output
|
||||
}
|
||||
|
||||
stages {
|
||||
@@ -39,17 +39,15 @@ pipeline {
|
||||
|
||||
stage('Archive Build Artifacts (PowerShell/7-Zip)') {
|
||||
steps {
|
||||
script { // Need script block for dir, powershell, and file operations
|
||||
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 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 '''
|
||||
// Use a powershell step to check for existing zip, or create a new one, then output the filename
|
||||
// Capture the output of the powershell script
|
||||
def zipFilenameOutput = powershell returnStdout: true, script: '''
|
||||
$ErrorActionPreference = "Stop" # Stop the script on any error
|
||||
|
||||
$sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe"
|
||||
@@ -57,7 +55,6 @@ pipeline {
|
||||
$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)"
|
||||
|
||||
@@ -90,112 +87,87 @@ pipeline {
|
||||
$newestZip = $existingZips | Select-Object -First 1
|
||||
$zipFilename = $newestZip.Name
|
||||
$zipFound = $true
|
||||
Write-Host "DEBUG: Using newest existing zip file: '$zipFilename'. Skipping creation process."
|
||||
Write-Host "Using newest existing zip file: '$zipFilename'. Skipping creation process."
|
||||
# Skip the rest of the script that creates a new zip
|
||||
} else {
|
||||
# No existing zip files, proceed with creation
|
||||
Write-Host "DEBUG: No existing zip files found. Proceeding with file finding and zipping process."
|
||||
Write-Host "No existing zip files found. Proceeding with file finding and zipping process."
|
||||
|
||||
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
|
||||
$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)."
|
||||
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."
|
||||
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
|
||||
\$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)'"
|
||||
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'"
|
||||
\$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'"
|
||||
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")
|
||||
\$timestamp = (Get-Date -Format "yyyyMMdd-HHmmss")
|
||||
|
||||
# Construct the zip filename using the base filename and timestamp
|
||||
$zipFilename = "$zipBaseFilename-$timestamp.zip"
|
||||
\$zipFilename = "\$zipBaseFilename-\$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
|
||||
# Start with command, type, and quoted zip filename
|
||||
$sevenZipArgs = @("a", "-tzip", "$zipFilename")
|
||||
\$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
|
||||
\$foundFilesQuoted = \$foundFiles | ForEach-Object { "\`"$_`"" }
|
||||
\$sevenZipArgs += \$foundFilesQuoted
|
||||
|
||||
# Construct the full command string for logging
|
||||
$commandString = "$sevenZipExe $($sevenZipArgs -join ' ')"
|
||||
Write-Host "Executing command: $commandString"
|
||||
\$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
|
||||
\$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
|
||||
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-Host "Zip archive created successfully by 7-Zip: \$zipFilename"
|
||||
}
|
||||
|
||||
# Write the determined zip filename to a temporary file
|
||||
Write-Host "Writing determined zip filename '$zipFilename' to temporary file '$tempFilenameFile'..."
|
||||
$zipFilename | Out-File -FilePath $tempFilenameFile -Encoding UTF8 -Force
|
||||
# Output the determined zip filename to standard output for the Groovy script to capture
|
||||
# Ensure this is the very last thing written to the standard output stream
|
||||
# This output will be captured by returnStdout: true
|
||||
Write-Output \$zipFilename
|
||||
|
||||
exit 0
|
||||
''' // End powershell script
|
||||
|
||||
// Read the zip filename from the temporary file
|
||||
def createdZipFilename = ""
|
||||
// The tempFilenameFile variable is already defined above
|
||||
|
||||
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 using a powershell step
|
||||
try {
|
||||
echo "Cleaning up temporary filename file: ${tempFilenameFile} using powershell"
|
||||
powershell "Remove-Item -Path '${tempFilenameFile}' -Force"
|
||||
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
|
||||
// Capture the output and set the environment variable
|
||||
// The PowerShell script is designed to output ONLY the zip filename to standard output
|
||||
env.CREATED_ZIP_FILENAME = zipFilenameOutput.trim()
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user