mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 21:46:30 +00:00
pipelines zip and gdrive upload
updated
This commit is contained in:
+77
-58
@@ -1,97 +1,111 @@
|
||||
pipeline {
|
||||
agent any
|
||||
agent { label 'Win10-BuildMachine' }
|
||||
|
||||
tools {
|
||||
git 'Default'
|
||||
}
|
||||
|
||||
parameters {
|
||||
string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/AzaionSuite/suite', description: 'Path to folder containing zip builds.')
|
||||
}
|
||||
|
||||
environment {
|
||||
GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder name
|
||||
LATEST_ZIP_FILENAME = '' // Will be set during the "Find Latest Zip" stage
|
||||
GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder on Google Drive
|
||||
RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf'
|
||||
LATEST_ZIP_FILENAME = '' // Will be set during the "Find Latest Zip" stage
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('Declarative: Checkout SCM') {
|
||||
steps {
|
||||
checkout scm
|
||||
}
|
||||
}
|
||||
|
||||
stage('Declarative: Tool Install') {
|
||||
steps {
|
||||
echo "Installing tools if necessary"
|
||||
}
|
||||
}
|
||||
|
||||
stage('Initialize Workspace') {
|
||||
steps {
|
||||
echo "Initializing workspace on Windows agent..."
|
||||
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 {
|
||||
script {
|
||||
echo "Finding latest zip file in: C:/Jenkins/workspace/Azaion/suite"
|
||||
dir('C:/Jenkins/workspace/Azaion/suite') {
|
||||
// List all .zip files starting with "AzaionSuite"
|
||||
def zipFiles = bat(script: 'dir /b AzaionSuite*.zip', returnStdout: true).trim().split("\n")
|
||||
echo "Finding latest zip file in: ${params.buildPath}"
|
||||
dir("${params.buildPath}") {
|
||||
def output = powershell(returnStdout: true, script: '''
|
||||
$pattern = "AzaionSuite*.zip"
|
||||
$zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending
|
||||
if ($zipFiles.Count -gt 0) {
|
||||
$latestZip = $zipFiles[0].Name
|
||||
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$latestZip"
|
||||
} else {
|
||||
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME="
|
||||
}
|
||||
''').trim()
|
||||
|
||||
if (zipFiles.isEmpty()) {
|
||||
error "No zip files found matching the pattern 'AzaionSuite*.zip'!"
|
||||
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."
|
||||
env.LATEST_ZIP_FILENAME = ''
|
||||
} else {
|
||||
def zipFileName = match[0][1]
|
||||
echo "✅ Latest zip file selected: ${zipFileName}"
|
||||
env.LATEST_ZIP_FILENAME = zipFileName
|
||||
}
|
||||
|
||||
// Sort the files by date (timestamp) in the filename
|
||||
def latestZip = zipFiles.sort { a, b ->
|
||||
def dateA = a.split('-')[1]
|
||||
def dateB = b.split('-')[1]
|
||||
return dateB.compareTo(dateA)
|
||||
}.first()
|
||||
|
||||
env.LATEST_ZIP_FILENAME = latestZip
|
||||
echo "✅ Latest zip file selected: ${env.LATEST_ZIP_FILENAME}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Upload If Not Exists & Always Remove Local') {
|
||||
when {
|
||||
expression { return env.LATEST_ZIP_FILENAME?.trim() }
|
||||
}
|
||||
steps {
|
||||
echo "Checking Google Drive for existing ZIP and uploading if needed..."
|
||||
powershell """
|
||||
# Check if file exists on Google Drive
|
||||
\$existingFiles = rclone lsf --files-only AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
|
||||
if (\$existingFiles -contains "${LATEST_ZIP_FILENAME}") {
|
||||
Write-Output "File ${LATEST_ZIP_FILENAME} already exists on Google Drive, skipping upload."
|
||||
\$fileName = "${env.LATEST_ZIP_FILENAME}"
|
||||
\$folder = "${GOOGLE_DRIVE_FOLDER}"
|
||||
\$rcloneRemote = "AzaionGoogleDrive:\$folder"
|
||||
\$localFilePath = "${params.buildPath}/\$fileName"
|
||||
|
||||
Write-Output "Checking for existing files in: \$rcloneRemote"
|
||||
\$existingFiles = rclone lsf --files-only \$rcloneRemote
|
||||
Write-Output "Existing files:"
|
||||
Write-Output \$existingFiles
|
||||
|
||||
if (\$existingFiles -match "^\$fileName\$") {
|
||||
Write-Output "File '\$fileName' already exists on Google Drive. Skipping upload."
|
||||
} else {
|
||||
Write-Output "Uploading ${LATEST_ZIP_FILENAME} to Google Drive..."
|
||||
rclone copy C:/Jenkins/workspace/Azaion/suite/${LATEST_ZIP_FILENAME} AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
|
||||
Write-Output "Uploading '\$fileName' to Google Drive..."
|
||||
rclone copy "\$localFilePath" \$rcloneRemote --progress --drive-chunk-size 64M
|
||||
Write-Output "Upload complete."
|
||||
}
|
||||
|
||||
# Always remove the local file after uploading
|
||||
Remove-Item "C:/Jenkins/workspace/Azaion/suite/${LATEST_ZIP_FILENAME}"
|
||||
if (Test-Path \$localFilePath) {
|
||||
Remove-Item -Force \$localFilePath
|
||||
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 { return env.LATEST_ZIP_FILENAME?.trim() }
|
||||
}
|
||||
steps {
|
||||
echo "Cleaning up older files on Google Drive..."
|
||||
powershell """
|
||||
# Check if folder exists
|
||||
if (-not (Test-Path "AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}")) {
|
||||
Write-Error "Google Drive folder ${GOOGLE_DRIVE_FOLDER} not found!"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# List all files in the Google Drive folder
|
||||
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
|
||||
|
||||
# Split files by line and exclude the latest zip file
|
||||
\$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" }
|
||||
\$latestFile = "${LATEST_ZIP_FILENAME}"
|
||||
\$filesArray = \$filesArray | Where-Object { \$_ -ne \$latestFile }
|
||||
|
||||
# If more than 3 files exist, delete the oldest ones
|
||||
if (\$filesArray.Count -gt 3) {
|
||||
\$filesSorted = \$filesArray | Sort-Object -Descending
|
||||
\$filesToDelete = \$filesSorted | Select-Object -Skip 3
|
||||
@@ -100,14 +114,12 @@ pipeline {
|
||||
Write-Output \$filesToDelete
|
||||
|
||||
if (\$filesToDelete.Count -gt 0) {
|
||||
# Create temporary delete list file
|
||||
\$tempFile = [System.IO.Path]::GetTempFileName()
|
||||
\$filesToDelete | Set-Content -Path \$tempFile -Encoding utf8
|
||||
|
||||
Write-Output "Contents of temporary delete list file (\$tempFile):"
|
||||
Get-Content \$tempFile
|
||||
|
||||
# Delete files listed for removal
|
||||
foreach (\$file in \$filesToDelete) {
|
||||
Write-Output "Deleting \$file..."
|
||||
rclone deletefile AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}/\$file
|
||||
@@ -126,11 +138,18 @@ pipeline {
|
||||
}
|
||||
|
||||
post {
|
||||
failure {
|
||||
echo "Pipeline failed. Check logs for details."
|
||||
always {
|
||||
echo 'Executing post-build cleanup...'
|
||||
powershell '''
|
||||
$uploadDir = "temp_upload"
|
||||
if (Test-Path $uploadDir) {
|
||||
Remove-Item -Recurse -Force $uploadDir
|
||||
}
|
||||
'''
|
||||
}
|
||||
success {
|
||||
echo "Pipeline completed successfully."
|
||||
|
||||
failure {
|
||||
echo 'Pipeline failed. Check logs for details.'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user