zip update

This commit is contained in:
dzaitsev
2025-05-04 14:11:15 +03:00
parent 154b5cdcf6
commit 366aab294e
+100 -125
View File
@@ -9,7 +9,6 @@ pipeline {
tools { tools {
// Git tool might be needed if this Jenkinsfile is in SCM // Git tool might be needed if this Jenkinsfile is in SCM
git 'Default' git 'Default'
// dotnetsdk is not needed here as we only process artifacts
} }
environment { environment {
@@ -17,203 +16,179 @@ pipeline {
SEVEN_ZIP_PATH = "C:/Program Files/7-Zip" // Adjust if 7-Zip is installed elsewhere SEVEN_ZIP_PATH = "C:/Program Files/7-Zip" // Adjust if 7-Zip is installed elsewhere
// Set the PATH environment variable including 7-Zip // Set the PATH environment variable including 7-Zip
PATH = "${SEVEN_ZIP_PATH};${env.PATH}" // Add 7-Zip to PATH PATH = "${SEVEN_ZIP_PATH};${env.PATH}"
// Define the name of your existing Google Drive upload pipeline job // 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'
GOOGLE_DRIVE_UPLOAD_JOB_NAME = 'GDrive Upload' // <<== UPDATE THIS
// Define the source directory for artifacts from the main build job's workspace // Define the source directory for artifacts from the main build job's workspace
// This is the directory this pipeline will change into to operate. MAIN_BUILD_ARTIFACTS_DIR = 'C:/Jenkins/workspace/AzaionSuite/suite'
// ** 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 // Define the name of the created zip file as an environment variable
// This makes it easier to reference in later stages CREATED_ZIP_FILENAME = ''
CREATED_ZIP_FILENAME = '' // This will be set dynamically by capturing PowerShell output
} }
stages { 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)') { stage('Archive Build Artifacts (PowerShell/7-Zip)') {
steps { steps {
script { // Need script block for dir and powershell step script {
echo "Starting 'Archive Build Artifacts (PowerShell/7-Zip)' stage." 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}") { dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") {
echo "Operating in directory: ${pwd()}" echo "Operating in directory: ${pwd()}"
// Use a powershell step to check for existing zip, or create a new one, then output the filename // Use a powershell step with improved error handling and robustness
// Capture the output of the powershell script
def zipFilenameOutput = powershell returnStdout: true, script: ''' def zipFilenameOutput = powershell returnStdout: true, script: '''
$ErrorActionPreference = "Stop" # Stop the script on any error $ErrorActionPreference = "Stop" # Stop the script on any error
# Define key variables
$sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe" $sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe"
$defaultVersion = "1.0.0" # Default version if no exe is found $defaultVersion = "1.0.0"
$exePattern = "AzaionSuite*.exe" $exePattern = "AzaionSuite*.exe"
$binPattern = "AzaionSuite*.bin" $binPattern = "AzaionSuite*.bin"
$zipPattern = "AzaionSuite*.zip" # Pattern for existing zip files $zipPattern = "AzaionSuite*.zip"
Write-Host "Operating in directory: $(Get-Location)" Write-Host "Operating in directory: $(Get-Location)"
# --- Debugging: List all items in the directory --- # Check if 7-Zip exists
Write-Host "DEBUG: Listing all items in the current directory:" if (-not (Test-Path $sevenZipExe)) {
Get-ChildItem -Path . | ForEach-Object { Write-Host "DEBUG: Item: $($_.FullName)" } Write-Error "7-Zip executable not found at $sevenZipExe"
Write-Host "DEBUG: End of item listing." exit 1
# --- 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."
Write-Host "DEBUG: Existing zip files found count: $($existingZips.Count)" # Check for existing zip files
$existingZips = Get-ChildItem -Path . -Filter $zipPattern |
Sort-Object LastWriteTime -Descending
$zipFilename = "" $zipFilename = ""
$zipFound = $false
if ($existingZips.Count -gt 0) { if ($existingZips.Count -gt 0) {
# Found existing zip files, use the newest one # Found existing zip files, use the newest one
$newestZip = $existingZips | Select-Object -First 1 $newestZip = $existingZips | Select-Object -First 1
$zipFilename = $newestZip.Name $zipFilename = $newestZip.Name
$zipFound = $true Write-Host "Using newest existing zip file: '$zipFilename'."
Write-Host "Using newest existing zip file: '$zipFilename'. Skipping creation process."
# Skip the rest of the script that creates a new zip
} else { } else {
# No existing zip files, proceed with creation # No existing zip files, proceed with creation
Write-Host "No existing zip files found. Proceeding with file finding and zipping process." Write-Host "No existing zip files found. Creating new zip file."
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."
# --- Determine Base Filename for Zip (from .exe if present) --- # Determine Base Filename for Zip (from .exe if present)
\$zipBaseFilename = "AzaionSuite.\$defaultVersion" # Default base filename $zipBaseFilename = "AzaionSuite.$defaultVersion" # Default
\$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 "Executable file found: '\$(\$exeFile.FullName)'" $zipBaseFilename = $exeFile.BaseName
# Extract filename without extension Write-Host "Using executable base filename: '$zipBaseFilename'"
\$zipBaseFilename = \$exeFile.BaseName
Write-Host "Using executable base filename for archive name: '\$zipBaseFilename'"
} else { } else {
Write-Warning "No executable found matching \$exePattern. Using default base filename: '\$zipBaseFilename'" Write-Host "No executable found. Using default: '$zipBaseFilename'"
} }
# --- Zipping Logic --- # Get timestamp for filename
$timestamp = (Get-Date -Format "yyyyMMdd-HHmmss")
$zipFilename = "$zipBaseFilename-$timestamp.zip"
# Get current date and time inYYYYMMDD-HHmmss format Write-Host "Creating zip archive: $zipFilename"
\$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."
try {
# Build the 7z command arguments # Build the 7z command arguments
# Start with command, type, and quoted zip filename $sevenZipArgs = @("a", "-tzip", "$zipFilename")
\$sevenZipArgs = @("a", "-tzip", "\$zipFilename") $foundFilesQuoted = $foundFiles | ForEach-Object { "`"$_`"" }
$sevenZipArgs += $foundFilesQuoted
# 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 # Execute the 7z command
# Using Start-Process with -Wait to ensure the script waits for 7z to finish $process = Start-Process -FilePath $sevenZipExe -ArgumentList $sevenZipArgs -Wait -NoNewWindow -PassThru
# and capturing the exit code $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) {
if (\$exitCode -ne 0) { Write-Error "Error creating zip archive. 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: $zipFilename"
}
catch {
Write-Error "Exception occurred during zip creation: $_"
exit 1
}
} }
# Output the determined zip filename to standard output for the Groovy script to capture # Verify the zip file exists before returning
# Ensure this is the very last thing written to the standard output stream if (-not (Test-Path $zipFilename)) {
# This output will be captured by returnStdout: true Write-Error "Expected zip file $zipFilename does not exist"
Write-Output \$zipFilename exit 1
}
# Output the final zip filename
Write-Output $zipFilename
exit 0 exit 0
''' // End powershell script '''
// Capture the output and set the environment variable // Trim 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() env.CREATED_ZIP_FILENAME = zipFilenameOutput.trim()
echo "Set CREATED_ZIP_FILENAME environment variable to: ${env.CREATED_ZIP_FILENAME}" echo "Zip filename: ${env.CREATED_ZIP_FILENAME}"
}
}
}
}
} // End dir block
}
}
}
stage('Archive Created Zip') { stage('Archive Created Zip') {
steps { steps {
script { // Need script block for dir and accessing environment variables set by PowerShell script {
echo "Starting 'Archive Created Zip' stage." 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}") { dir("${env.MAIN_BUILD_ARTIFACTS_DIR}") {
echo "Operating in directory: ${pwd()}" 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()) { // Verify zip filename was set properly
echo "Identified created zip file for archiving: ${createdZipFilename}" if (!env.CREATED_ZIP_FILENAME?.trim()) {
// Archive the created zip file using Jenkins built-in step error "CREATED_ZIP_FILENAME environment variable was not set properly."
// The zip file is created in the MAIN_BUILD_ARTIFACTS_DIR by the Batch script }
archiveArtifacts artifacts: "${createdZipFilename}", fingerprint: true
// Verify the file exists before attempting to archive
def fileExists = fileExists env.CREATED_ZIP_FILENAME
if (!fileExists) {
error "File ${env.CREATED_ZIP_FILENAME} does not exist at ${pwd()}."
}
echo "Archiving zip file: ${env.CREATED_ZIP_FILENAME}"
archiveArtifacts artifacts: "${env.CREATED_ZIP_FILENAME}", fingerprint: true
echo "Archive step completed." 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') { stage('Trigger Google Drive Upload') {
steps { steps {
script { // This stage still requires a script block for the build step script {
echo "Triggering Google Drive Upload pipeline: ${env.GOOGLE_DRIVE_UPLOAD_JOB_NAME}" 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. try {
// 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 build job: env.GOOGLE_DRIVE_UPLOAD_JOB_NAME
} // End dir block echo "Google Drive Upload pipeline triggered successfully."
} // End script block } catch (Exception e) {
} // End steps block echo "Failed to trigger Google Drive Upload pipeline: ${e.message}"
} // End stage block error "Failed to trigger Google Drive Upload pipeline. See console log for details."
} // End of stages block }
} // End of pipeline block }
}
}
}
post {
success {
echo "Pipeline completed successfully. Created and archived zip: ${env.CREATED_ZIP_FILENAME}"
}
failure {
echo "Pipeline failed. See logs for details."
}
}
}