Files
annotations/build/jenkins/GDriveUpload
T
2025-05-07 19:33:44 +03:00

136 lines
5.6 KiB
Plaintext

pipeline {
agent any
environment {
GOOGLE_DRIVE_FOLDER = 'AzaionSuiteBuilds' // Hardcoded folder name
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 {
// Ensure required tools are available, like Git, PowerShell, etc.
echo "Installing tools if necessary"
}
}
stage('Initialize Workspace') {
steps {
echo "Initializing workspace on Windows agent..."
// Initialization steps for workspace
}
}
stage('Find Latest Zip') {
steps {
script {
echo "Finding latest zip file in: C:/Jenkins/workspace/Azaion/suite"
dir('C:/Jenkins/workspace/Azaion/suite') {
// Debug output to confirm current directory contents
echo "Listing .zip files in the directory:"
bat(script: 'dir /b *.zip', returnStdout: true).trim()
// Get the most recent .zip file based on modification date
def latestZip = bat(script: 'dir /b /od *.zip | head -n 1', returnStdout: true).trim()
if (latestZip) {
env.LATEST_ZIP_FILENAME = latestZip
echo "✅ Latest zip file selected: ${env.LATEST_ZIP_FILENAME}"
} else {
error "No valid zip files found in the directory!"
}
}
}
}
}
stage('Upload If Not Exists & Always Remove Local') {
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."
} else {
Write-Output "Uploading ${LATEST_ZIP_FILENAME} to Google Drive..."
rclone copy C:/Jenkins/workspace/Azaion/suite/${LATEST_ZIP_FILENAME} AzaionGoogleDrive:${GOOGLE_DRIVE_FOLDER}
}
# Always remove the local file after uploading
Remove-Item "C:/Jenkins/workspace/Azaion/suite/${LATEST_ZIP_FILENAME}"
"""
}
}
stage('Cleanup Older Files on Google Drive') {
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
Write-Output "Files to delete (older than 3 latest):"
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
}
Remove-Item -Path \$tempFile
} else {
Write-Output "No files to delete."
}
} else {
Write-Output "No files found on Google Drive to clean up."
}
"""
}
}
}
post {
failure {
echo "Pipeline failed. Check logs for details."
}
success {
echo "Pipeline completed successfully."
}
}
}