mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 11:06:30 +00:00
Merge remote-tracking branch 'origin/pipeline' into dev
This commit is contained in:
+139
-107
@@ -1,135 +1,167 @@
|
|||||||
// This pipeline job is triggered by the main AzaionSuite build pipeline.
|
|
||||||
// It copies build artifacts, zips them, and then triggers the Google Drive upload pipeline.
|
|
||||||
pipeline {
|
pipeline {
|
||||||
|
|
||||||
agent { label 'Win10-BuildMachine' }
|
agent { label 'Win10-BuildMachine' }
|
||||||
|
|
||||||
tools {
|
tools {
|
||||||
// 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
|
}
|
||||||
|
|
||||||
|
parameters {
|
||||||
|
string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/Azaion/suite', description: 'Path to folder containing zip builds.')
|
||||||
}
|
}
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
// 7-Zip path (assuming default installation)
|
GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder on Google Drive
|
||||||
SEVEN_ZIP_PATH = "C:/Program Files/7-Zip" // Adjust if 7-Zip is installed elsewhere
|
RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf'
|
||||||
|
|
||||||
// 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 = 'YourGoogleDriveUploadPipelineName' // <<== UPDATE THIS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Copy Build Artifacts') {
|
stage('Initialize Workspace') {
|
||||||
|
steps {
|
||||||
|
echo 'Initializing workspace on Windows agent...'
|
||||||
|
powershell '''
|
||||||
|
$uploadDir = "temp_upload"
|
||||||
|
if (-Not (Test-Path $uploadDir)) {
|
||||||
|
New-Item -ItemType Directory -Path $uploadDir | Out-Null
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Find Latest Zip') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo "Starting 'Copy Build Artifacts' stage."
|
echo "Using build path: ${params.buildPath}" // Debug output for build path
|
||||||
// Copy artifacts from the upstream build job
|
echo "Finding latest zip file in: ${params.buildPath}"
|
||||||
// Replace 'AzaionSuite' with the actual name of your main build pipeline job
|
def latestZipFilename = ''
|
||||||
copyArtifacts(
|
dir("${params.buildPath}") {
|
||||||
projectName: 'AzaionSuite', // <<== UPDATE THIS if your main build job name is different
|
def output = powershell(returnStdout: true, script: '''
|
||||||
selector: lastSuccessful(), // Copy from the last successful build
|
$pattern = "AzaionSuite*.zip"
|
||||||
// Specify which files to copy. These should match the files produced by the build.
|
$zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending
|
||||||
// Assuming artifacts are in the 'suite' directory relative to the upstream workspace root.
|
if ($zipFiles.Count -gt 0) {
|
||||||
filter: 'suite/AzaionSuite*.exe, suite/AzaionSuite*.bin'
|
$latestZip = $zipFiles[0].Name
|
||||||
// By default, artifacts are copied to the current job's workspace root.
|
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$latestZip"
|
||||||
)
|
} else {
|
||||||
echo "Artifacts copied successfully."
|
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME="
|
||||||
|
}
|
||||||
|
''').trim()
|
||||||
|
|
||||||
|
// Debug output to verify the response from the powershell script
|
||||||
|
echo "PowerShell output: ${output}"
|
||||||
|
|
||||||
|
def match = output =~ /::SET-ENV::LATEST_ZIP_FILENAME=(.+)/
|
||||||
|
if (!match || !match[0][1]?.trim()) {
|
||||||
|
echo "⚠️ No ZIP files matching the pattern were found. Skipping upload and cleanup stages."
|
||||||
|
} else {
|
||||||
|
latestZipFilename = match[0][1]
|
||||||
|
echo "✅ Latest zip file selected: ${latestZipFilename}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass the filename to the next stages directly
|
||||||
|
env.LATEST_ZIP_FILENAME = latestZipFilename
|
||||||
|
echo "LATEST_ZIP_FILENAME after processing: ${env.LATEST_ZIP_FILENAME}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Archive Copied Artifacts (7-Zip)') {
|
stage('Upload If Not Exists & Always Remove Local') {
|
||||||
|
when {
|
||||||
|
expression {
|
||||||
|
echo "LATEST_ZIP_FILENAME is: '${env.LATEST_ZIP_FILENAME}'" // Debug output
|
||||||
|
return env.LATEST_ZIP_FILENAME?.trim()
|
||||||
|
}
|
||||||
|
}
|
||||||
steps {
|
steps {
|
||||||
script {
|
echo "Checking Google Drive for existing ZIP and uploading if needed..."
|
||||||
echo "Starting 'Archive Copied Artifacts (7-Zip)' stage."
|
powershell """
|
||||||
// The copied artifacts are now in the root of this job's workspace.
|
\$fileName = "${env.LATEST_ZIP_FILENAME}"
|
||||||
def artifactsDirectory = '.' // Artifacts are in the current workspace root
|
\$folder = "${GOOGLE_DRIVE_FOLDER}"
|
||||||
def version = '1.0.0' // Default version
|
\$rcloneRemote = "AzaionGoogleDrive:\$folder"
|
||||||
def filesToArchive = []
|
\$localFilePath = "${params.buildPath}/\$fileName"
|
||||||
def exeFound = false
|
|
||||||
|
|
||||||
// Find all relevant files in the current workspace root
|
Write-Output "Checking for existing files in: \$rcloneRemote"
|
||||||
def foundFiles = findFiles(glob: 'AzaionSuite*.exe') + findFiles(glob: 'AzaionSuite*.bin')
|
\$existingFiles = rclone lsf --files-only \$rcloneRemote
|
||||||
filesToArchive = foundFiles.collect { it.path } // Get list of paths relative to the workspace root
|
Write-Output "Existing files:"
|
||||||
|
Write-Output \$existingFiles
|
||||||
|
|
||||||
// --- Version Extraction (only from .exe if present) ---
|
if (\$existingFiles -match "^\$fileName\$") {
|
||||||
def exeFiles = findFiles(glob: 'AzaionSuite*.exe')
|
Write-Output "File '\$fileName' already exists on Google Drive. Skipping upload."
|
||||||
if (exeFiles.size() > 0) {
|
} else {
|
||||||
exeFound = true
|
Write-Output "Uploading '\$fileName' to Google Drive..."
|
||||||
def exeFilePath = exeFiles[0].path // Use path from the first found exe
|
rclone copy "\$localFilePath" \$rcloneRemote --progress --drive-chunk-size 64M
|
||||||
// Regex to find version like 1.2.3 or 1.2.3-4 followed by .exe
|
Write-Output "Upload complete."
|
||||||
// Match against the full path (which is relative to workspace root here)
|
}
|
||||||
def matcher = (exeFilePath =~ /AzaionSuite(\d+\.\d+\.\d+(-\d+)?)\.exe/)
|
|
||||||
if (matcher.find()) {
|
if (Test-Path \$localFilePath) {
|
||||||
version = matcher.group(1)
|
Remove-Item -Force \$localFilePath
|
||||||
echo "Found version for archive: ${version}"
|
Write-Output "Local file deleted: \$localFilePath"
|
||||||
|
} else {
|
||||||
|
Write-Output "Local file already deleted or not found."
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Cleanup Older Files on Google Drive') {
|
||||||
|
when {
|
||||||
|
expression {
|
||||||
|
echo "LATEST_ZIP_FILENAME is: '${env.LATEST_ZIP_FILENAME}'" // Debug output
|
||||||
|
return env.LATEST_ZIP_FILENAME?.trim()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
steps {
|
||||||
|
echo "Cleaning up older files on Google Drive..."
|
||||||
|
powershell """
|
||||||
|
Write-Output "Listing all files in the folder ${GOOGLE_DRIVE_FOLDER} on Google Drive..."
|
||||||
|
\$files = rclone lsf --files-only AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
|
||||||
|
Write-Output "Files found on Google Drive:"
|
||||||
|
Write-Output \$files
|
||||||
|
|
||||||
|
\$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" }
|
||||||
|
if (\$filesArray.Count -gt 3) {
|
||||||
|
\$filesSorted = \$filesArray | Sort-Object -Descending
|
||||||
|
\$filesToDelete = \$filesSorted | Select-Object -Skip 3
|
||||||
|
|
||||||
|
Write-Output "Files to delete (older than 3 latest):"
|
||||||
|
Write-Output \$filesToDelete
|
||||||
|
|
||||||
|
if (\$filesToDelete.Count -gt 0) {
|
||||||
|
\$tempFile = [System.IO.Path]::GetTempFileName()
|
||||||
|
\$filesToDelete | Set-Content -Path \$tempFile -Encoding utf8
|
||||||
|
|
||||||
|
Write-Output "Contents of temporary delete list file (\$tempFile):"
|
||||||
|
Get-Content \$tempFile
|
||||||
|
|
||||||
|
foreach (\$file in \$filesToDelete) {
|
||||||
|
Write-Output "Deleting \$file..."
|
||||||
|
rclone deletefile AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}/\$file
|
||||||
|
}
|
||||||
|
|
||||||
|
Remove-Item -Path \$tempFile
|
||||||
} else {
|
} else {
|
||||||
echo "Warning: Could not extract version from '${exeFiles[0].name}'. Using default: ${version}"
|
Write-Output "No files to delete."
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
echo "Warning: No executable found to extract version for archive. Using default: ${version}"
|
Write-Output "No files found on Google Drive to clean up."
|
||||||
}
|
}
|
||||||
|
"""
|
||||||
// --- Zipping Logic ---
|
|
||||||
if (filesToArchive.size() > 0) {
|
|
||||||
// Get current date and time in YYYYMMdd-HHMMSS format using PowerShell
|
|
||||||
// Using a separate bat call to ensure output is captured cleanly
|
|
||||||
def timestamp = bat(
|
|
||||||
script: 'powershell -Command "Get-Date -Format YYYYMMdd-HHmmss"',
|
|
||||||
returnStdout: true
|
|
||||||
).trim() // Trim to remove potential newline characters
|
|
||||||
|
|
||||||
def zipFilename = "AzaionSuite.${version}-${timestamp}.zip"
|
|
||||||
// 7-Zip command requires quoting paths with spaces.
|
|
||||||
// We provide full paths relative to the workspace root.
|
|
||||||
def filesListString = filesToArchive.collect { "\"${it}\"" }.join(' ') // Quote each file path and join
|
|
||||||
|
|
||||||
echo "Creating zip archive: ${zipFilename} using 7-Zip."
|
|
||||||
echo "Files to include (full paths): ${filesListString}"
|
|
||||||
|
|
||||||
// Construct the full 7z command string in Groovy
|
|
||||||
def sevenZipCommand = "7z a -tzip \"${zipFilename}\" ${filesListString}"
|
|
||||||
|
|
||||||
// Execute the constructed command string using a single bat step
|
|
||||||
bat """
|
|
||||||
@echo off
|
|
||||||
echo Zipping files with 7-Zip...
|
|
||||||
${sevenZipCommand}
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
|
|
||||||
// Archive the created zip file using Jenkins built-in step
|
|
||||||
// This makes the zip available for the downstream Google Drive upload job
|
|
||||||
archiveArtifacts artifacts: "${zipFilename}", fingerprint: true
|
|
||||||
echo "Archive step completed."
|
|
||||||
|
|
||||||
} else {
|
|
||||||
error "No files (.exe or .bin) found in the copied artifacts to archive. Cannot create zip."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Trigger Google Drive Upload') {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
echo "Triggering Google Drive Upload pipeline: ${env.GOOGLE_DRIVE_UPLOAD_JOB_NAME}"
|
|
||||||
// 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).
|
|
||||||
build job: env.GOOGLE_DRIVE_UPLOAD_JOB_NAME
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// No post section needed for this job, post actions will be handled by the triggered job
|
|
||||||
|
post {
|
||||||
|
always {
|
||||||
|
echo 'Executing post-build cleanup...'
|
||||||
|
powershell '''
|
||||||
|
$uploadDir = "temp_upload"
|
||||||
|
if (Test-Path $uploadDir) {
|
||||||
|
Remove-Item -Recurse -Force $uploadDir
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
}
|
||||||
|
|
||||||
|
failure {
|
||||||
|
echo 'Pipeline failed. Check logs for details.'
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//test
|
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
pipeline {
|
||||||
|
agent { label 'Win10-BuildMachine' }
|
||||||
|
|
||||||
|
parameters {
|
||||||
|
string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/Azaion/suite', description: 'Build directory to zip from')
|
||||||
|
}
|
||||||
|
|
||||||
|
tools {
|
||||||
|
git 'Default'
|
||||||
|
}
|
||||||
|
|
||||||
|
environment {
|
||||||
|
SEVEN_ZIP_PATH = "C:/Program Files/7-Zip"
|
||||||
|
PATH = "${SEVEN_ZIP_PATH};${env.PATH}"
|
||||||
|
GOOGLE_DRIVE_UPLOAD_JOB_NAME = 'GDrive Upload'
|
||||||
|
}
|
||||||
|
|
||||||
|
stages {
|
||||||
|
stage('Detect or Create Zip') {
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
echo "Starting 'Detect or Create Zip' stage."
|
||||||
|
echo "Using build path: ${params.buildPath}"
|
||||||
|
dir("${params.buildPath}") {
|
||||||
|
powershell '''
|
||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
$sevenZipExe = "$env:SEVEN_ZIP_PATH\\7z.exe"
|
||||||
|
$exePattern = "AzaionSuite*.exe"
|
||||||
|
$binPattern = "AzaionSuite*.bin"
|
||||||
|
$zipPattern = "AzaionSuite*.zip"
|
||||||
|
|
||||||
|
if (-not (Test-Path $sevenZipExe)) {
|
||||||
|
Write-Error "7-Zip not found at $sevenZipExe"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
$existingZips = Get-ChildItem -Path . -Filter $zipPattern | Sort-Object LastWriteTime -Descending
|
||||||
|
$zipFilename = ""
|
||||||
|
|
||||||
|
if ($existingZips.Count -gt 0) {
|
||||||
|
$zipFilename = $existingZips[0].Name
|
||||||
|
Write-Host "Using existing zip file: $zipFilename"
|
||||||
|
$wasCreated = $false
|
||||||
|
} else {
|
||||||
|
$exeFile = Get-ChildItem -Recurse -Filter $exePattern | Select-Object -First 1
|
||||||
|
if (-not $exeFile) {
|
||||||
|
Write-Error "No EXE file found to create zip"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
$zipBaseFilename = $exeFile.BaseName
|
||||||
|
$timestamp = (Get-Date -Format "yyyyMMdd-HHmmss")
|
||||||
|
$zipFilename = "$zipBaseFilename-$timestamp.zip"
|
||||||
|
|
||||||
|
$filesToZip = Get-ChildItem -Recurse -Include $exePattern, $binPattern | Select-Object -ExpandProperty FullName
|
||||||
|
if ($filesToZip.Count -eq 0) {
|
||||||
|
Write-Error "No files found to zip."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
$args = @("a", "-tzip", "$zipFilename") + ($filesToZip | ForEach-Object { "`"$_`"" })
|
||||||
|
$process = Start-Process -FilePath $sevenZipExe -ArgumentList $args -Wait -NoNewWindow -PassThru
|
||||||
|
if ($process.ExitCode -ne 0) {
|
||||||
|
Write-Error "7-Zip failed with code $($process.ExitCode)"
|
||||||
|
exit $process.ExitCode
|
||||||
|
}
|
||||||
|
|
||||||
|
$wasCreated = $true
|
||||||
|
Write-Host "Created new zip file: $zipFilename"
|
||||||
|
}
|
||||||
|
|
||||||
|
Set-Content -Path "zipfilename.txt" -Value $zipFilename -Encoding ASCII
|
||||||
|
Set-Content -Path "zip_was_created.txt" -Value $wasCreated -Encoding ASCII
|
||||||
|
'''
|
||||||
|
|
||||||
|
def zipFilename = readFile('zipfilename.txt').trim()
|
||||||
|
def zipCreated = readFile('zip_was_created.txt').trim().toBoolean()
|
||||||
|
|
||||||
|
echo "Zip filename: ${zipFilename}"
|
||||||
|
echo "Was zip created this run? ${zipCreated}"
|
||||||
|
|
||||||
|
writeFile file: 'created_zip.txt', text: zipFilename
|
||||||
|
writeFile file: 'zip_was_created.txt', text: zipCreated.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Archive Zip File (if new)') {
|
||||||
|
when {
|
||||||
|
expression {
|
||||||
|
def zipCreated = readFile("${params.buildPath}/zip_was_created.txt").trim().toBoolean()
|
||||||
|
return zipCreated
|
||||||
|
}
|
||||||
|
}
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
echo "Archiving newly created zip file..."
|
||||||
|
dir("${params.buildPath}") {
|
||||||
|
def zipFilename = readFile('created_zip.txt').trim()
|
||||||
|
if (!fileExists(zipFilename)) {
|
||||||
|
error "Zip file '${zipFilename}' not found!"
|
||||||
|
}
|
||||||
|
archiveArtifacts artifacts: "${zipFilename}", fingerprint: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Trigger Google Drive Upload') {
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
echo "Triggering Google Drive Upload pipeline: ${env.GOOGLE_DRIVE_UPLOAD_JOB_NAME}"
|
||||||
|
build job: env.GOOGLE_DRIVE_UPLOAD_JOB_NAME, parameters: [
|
||||||
|
string(name: 'buildPath', value: params.buildPath)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
success {
|
||||||
|
script {
|
||||||
|
def zipFilename = readFile("${params.buildPath}/created_zip.txt").trim()
|
||||||
|
echo "Pipeline completed successfully. Final zip: ${zipFilename}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
failure {
|
||||||
|
echo "Pipeline failed."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user