pipelines zip and gdrive upload

updated
This commit is contained in:
dzaitsev
2025-05-07 19:27:36 +03:00
parent f7dd087fad
commit e82c3f5bcb
+57 -83
View File
@@ -1,114 +1,86 @@
pipeline { pipeline {
agent { label 'Win10-BuildMachine' } agent any
tools {
git 'Default'
}
parameters {
string(name: 'buildPath', defaultValue: 'C:/Jenkins/workspace/AzaionSuite/suite', description: 'Path to folder containing zip builds.')
}
environment { environment {
RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder name
GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' LATEST_ZIP_FILENAME = '' // Will be set during the "Find Latest Zip" stage
} }
stages { stages {
stage('Declarative: Checkout SCM') {
steps {
checkout scm
}
}
stage('Declarative: Tool Install') {
steps {
// Ensure required tools are available, like Git, PowerShell, etc.
echo "Installing tools if necessary"
}
}
stage('Initialize Workspace') { stage('Initialize Workspace') {
steps { steps {
echo 'Initializing workspace on Windows agent...' echo "Initializing workspace on Windows agent..."
powershell ''' // Initialization steps for workspace
$uploadDir = "temp_upload"
if (-Not (Test-Path $uploadDir)) {
New-Item -ItemType Directory -Path $uploadDir | Out-Null
}
'''
} }
} }
stage('Find Latest Zip') { stage('Find Latest Zip') {
steps { steps {
script { script {
echo "Finding latest zip file in: ${params.buildPath}" echo "Finding latest zip file in: C:/Jenkins/workspace/Azaion/suite"
dir("${params.buildPath}") { dir('C:/Jenkins/workspace/Azaion/suite') {
def output = powershell(returnStdout: true, script: ''' def latestZip = sh(script: 'ls -t *.zip | head -n 1', returnStdout: true).trim()
$pattern = "AzaionSuite*-*-*.zip" env.LATEST_ZIP_FILENAME = latestZip
$zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending echo "✅ Latest zip file selected: ${env.LATEST_ZIP_FILENAME}"
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()
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
}
} }
} }
} }
} }
stage('Upload If Not Exists & Always Remove Local') { stage('Upload If Not Exists & Always Remove Local') {
when {
expression { return env.LATEST_ZIP_FILENAME?.trim() }
}
steps { steps {
echo "Checking Google Drive for existing ZIP and uploading if needed..." echo "Checking Google Drive for existing ZIP and uploading if needed..."
powershell """ powershell """
\$fileName = "${env.LATEST_ZIP_FILENAME}" # Check if file exists on Google Drive
\$folder = "${env.GOOGLE_DRIVE_FOLDER}" \$existingFiles = rclone lsf --files-only AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
\$rcloneRemote = "AzaionGoogleDrive:\$folder" if (\$existingFiles -contains "${LATEST_ZIP_FILENAME}") {
\$localFilePath = "${params.buildPath}/\$fileName" Write-Output "File ${LATEST_ZIP_FILENAME} already exists on Google Drive, skipping upload."
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 { } else {
Write-Output "Uploading '\$fileName' to Google Drive..." Write-Output "Uploading ${LATEST_ZIP_FILENAME} to Google Drive..."
rclone copy "\$localFilePath" \$rcloneRemote --progress --drive-chunk-size 64M rclone copy C:/Jenkins/workspace/Azaion/suite/${LATEST_ZIP_FILENAME} AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
Write-Output "Upload complete."
} }
if (Test-Path \$localFilePath) { # Always remove the local file after uploading
Remove-Item -Force \$localFilePath Remove-Item "C:/Jenkins/workspace/Azaion/suite/${LATEST_ZIP_FILENAME}"
Write-Output "Local file deleted: \$localFilePath"
} else {
Write-Output "Local file already deleted or not found."
}
""" """
} }
} }
stage('Cleanup Older Files on Google Drive') { stage('Cleanup Older Files on Google Drive') {
when {
expression { return env.LATEST_ZIP_FILENAME?.trim() }
}
steps { steps {
echo "Cleaning up older files on Google Drive..." echo "Cleaning up older files on Google Drive..."
powershell """ powershell """
Write-Output "Listing all files in the folder ${params.GOOGLE_DRIVE_FOLDER} on Google Drive..." # Check if folder exists
\$files = rclone lsf --files-only AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} 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 found on Google Drive:"
Write-Output \$files Write-Output \$files
# Split files by line and exclude the latest zip file
\$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" } \$filesArray = \$files -split "`n" | Where-Object { \$_ -ne "" }
# Exclude the latest zip file from deletion \$latestFile = "${LATEST_ZIP_FILENAME}"
\$latestFile = "${env.LATEST_ZIP_FILENAME}"
\$filesArray = \$filesArray | Where-Object { \$_ -ne \$latestFile } \$filesArray = \$filesArray | Where-Object { \$_ -ne \$latestFile }
# If more than 3 files exist, delete the oldest ones
if (\$filesArray.Count -gt 3) { if (\$filesArray.Count -gt 3) {
\$filesSorted = \$filesArray | Sort-Object -Descending \$filesSorted = \$filesArray | Sort-Object -Descending
\$filesToDelete = \$filesSorted | Select-Object -Skip 3 \$filesToDelete = \$filesSorted | Select-Object -Skip 3
@@ -117,15 +89,17 @@ stage('Cleanup Older Files on Google Drive') {
Write-Output \$filesToDelete Write-Output \$filesToDelete
if (\$filesToDelete.Count -gt 0) { if (\$filesToDelete.Count -gt 0) {
# Create temporary delete list file
\$tempFile = [System.IO.Path]::GetTempFileName() \$tempFile = [System.IO.Path]::GetTempFileName()
\$filesToDelete | Set-Content -Path \$tempFile -Encoding utf8 \$filesToDelete | Set-Content -Path \$tempFile -Encoding utf8
Write-Output "Contents of temporary delete list file (\$tempFile):" Write-Output "Contents of temporary delete list file (\$tempFile):"
Get-Content \$tempFile Get-Content \$tempFile
# Delete files listed for removal
foreach (\$file in \$filesToDelete) { foreach (\$file in \$filesToDelete) {
Write-Output "Deleting \$file..." Write-Output "Deleting \$file..."
rclone deletefile AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER}/\$file rclone deletefile AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}/\$file
} }
Remove-Item -Path \$tempFile Remove-Item -Path \$tempFile
@@ -138,21 +112,21 @@ stage('Cleanup Older Files on Google Drive') {
""" """
} }
} }
stage('Declarative: Post Actions') {
steps {
echo "Executing post-build cleanup..."
// Clean up any resources after the pipeline run
}
}
} }
post { post {
always {
echo 'Executing post-build cleanup...'
powershell '''
$uploadDir = "temp_upload"
if (Test-Path $uploadDir) {
Remove-Item -Recurse -Force $uploadDir
}
'''
}
failure { failure {
echo 'Pipeline failed. Check logs for details.' echo "Pipeline failed. Check logs for details."
}
success {
echo "Pipeline completed successfully."
} }
} }
} }