From 6a336e90c5b82b795c089df18bc428e0fc56e9af Mon Sep 17 00:00:00 2001 From: dzaitsev Date: Sun, 4 May 2025 13:24:13 +0300 Subject: [PATCH] added zip --- build/jenkins/zip | 201 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 build/jenkins/zip diff --git a/build/jenkins/zip b/build/jenkins/zip new file mode 100644 index 0000000..5a673f3 --- /dev/null +++ b/build/jenkins/zip @@ -0,0 +1,201 @@ +// 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. +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 (Batch/7-Zip)') { + steps { + script { // Need script block for dir and bat step + echo "Starting 'Archive Build Artifacts (Batch/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) + def exePattern = 'AzaionSuite*.exe' + def binPattern = 'AzaionSuite*.bin' + // Define defaultVersion here, as it's used within the bat script + def defaultVersion = '1.0.0' + + + // Use a bat step to perform file finding, version extraction, timestamp generation, and zipping + bat """ + @echo off + setlocal enabledelayedexpansion + + set "SEVEN_ZIP_EXE=%SEVEN_ZIP_PATH%\\7z.exe" + set "DEFAULT_VERSION=${defaultVersion}" + set "EXE_PATTERN=${exePattern}" + set "BIN_PATTERN=${binPattern}" + + echo Searching for files matching %EXE_PATTERN% and %BIN_PATTERN% in the current directory for zipping... + + 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 Add the quoted full path to FOUND_FILES + set "FOUND_FILES=!FOUND_FILES! "%%F"" + rem Check if it's an exe for version extraction and store the first one found + echo "%%F" | findstr /I /E "\\\\.exe" >nul && if not defined EXE_FILE set "EXE_FILE=%%F" + ) + + if not defined FOUND_FILES ( + echo No files matching patterns %EXE_PATTERN% or %BIN_PATTERN% found in %CD%. + exit /b 1 + ) + + 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. + + + rem --- Version Extraction (only from .exe if present) --- + set "VERSION=%DEFAULT_VERSION%" + if defined EXE_FILE ( + echo Attempting to extract version from "%EXE_FILE%"... + rem Extract filename from full path + for %%I in ("%EXE_FILE%") do set "EXE_FILENAME=%%~nxI" + rem Extract part after AzaionSuite and before .exe + rem Assumes format AzaionSuite.exe + set "TEMP_VERSION=!EXE_FILENAME:AzaionSuite=!" + set "VERSION=!TEMP_VERSION:.exe=!" + rem Basic check if extraction was successful + if "!VERSION!" == "!TEMP_VERSION!" ( + echo Warning: Could not extract version from "%EXE_FILENAME%". Using default: %VERSION% + set "VERSION=%DEFAULT_VERSION%" + ) else ( + echo Found version for archive: %VERSION% + ) + ) else ( + echo Warning: No executable found to extract version for archive. Using default: %VERSION% + ) + + + rem --- Zipping Logic --- + + rem Get current date and time inMMDD-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%" + + set "ZIP_FILENAME=AzaionSuite.%VERSION%-%timestamp%.zip" + + echo Creating zip archive: %ZIP_FILENAME% using 7-Zip. + + 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%"" + + rem Append the list of found files (which are already quoted and space-separated) + set "SEVEN_ZIP_COMMAND=%SEVEN_ZIP_COMMAND%%FOUND_FILES%" + + + 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 + } // End dir block + } + } + } + stage('Archive Created Zip') { + steps { + script { // Need script block for dir and accessing environment variables set by Batch + 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