mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:26:31 +00:00
gdrive update1
This commit is contained in:
+34
-127
@@ -1,138 +1,45 @@
|
|||||||
pipeline {
|
stage('Cleanup Older Files on Google Drive') {
|
||||||
agent { label 'Win10-BuildMachine' }
|
steps {
|
||||||
|
echo "Cleaning up older files on Google Drive..."
|
||||||
|
powershell """
|
||||||
|
# List all files in the Google Drive folder
|
||||||
|
Write-Output "Listing all files in the folder ${params.GOOGLE_DRIVE_FOLDER} on Google Drive..."
|
||||||
|
\$files = rclone lsf --files-only AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --format "tp" --max-depth 1
|
||||||
|
Write-Output "Files found on Google Drive:"
|
||||||
|
Write-Output \$files
|
||||||
|
|
||||||
tools {
|
# If files were found
|
||||||
git 'Default'
|
if (\$files.Count -gt 0) {
|
||||||
}
|
# Sort the files by date in descending order
|
||||||
|
\$filesSorted = \$files | Sort-Object -Descending
|
||||||
|
|
||||||
parameters {
|
# Keep only the 3 latest files
|
||||||
string(name: 'GOOGLE_DRIVE_FOLDER', defaultValue: 'AzaionSuiteBuilds', description: 'The folder on Google Drive where the build will be uploaded.')
|
\$filesToDelete = \$filesSorted | Select-Object -Skip 3
|
||||||
}
|
|
||||||
|
|
||||||
environment {
|
Write-Output "Files to delete (older than 3 latest):"
|
||||||
RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf' // Ensure this points to the correct location
|
Write-Output \$filesToDelete
|
||||||
}
|
|
||||||
|
|
||||||
stages {
|
# If there are files to delete, remove them
|
||||||
stage('Initialize Workspace') {
|
if (\$filesToDelete.Count -gt 0) {
|
||||||
steps {
|
Write-Output "Deleting files: \$filesToDelete"
|
||||||
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 and Upload') {
|
# Create a temporary file to store the list of files to delete
|
||||||
steps {
|
\$tempFile = [System.IO.Path]::GetTempFileName()
|
||||||
script {
|
\$filesToDelete | Out-File -FilePath \$tempFile -Encoding utf8
|
||||||
echo "Starting 'Find Latest Zip and Upload' stage."
|
Write-Output "Temporary file created: \$tempFile"
|
||||||
dir("C:/Jenkins/workspace/AzaionSuite/suite") {
|
|
||||||
def output = powershell(returnStdout: true, script: '''
|
|
||||||
$pattern = "AzaionSuite*-*-*.zip"
|
|
||||||
$zipFiles = Get-ChildItem -Filter $pattern | Sort-Object Name -Descending
|
|
||||||
if ($zipFiles.Count -eq 0) {
|
|
||||||
Write-Error "No ZIP files matching pattern '$pattern' were found."
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
$latestZip = $zipFiles[0].Name
|
|
||||||
Write-Output "::SET-ENV::LATEST_ZIP_FILENAME=$latestZip"
|
|
||||||
''').trim()
|
|
||||||
|
|
||||||
def match = output =~ /::SET-ENV::LATEST_ZIP_FILENAME=(.+)/
|
# Use rclone to delete the files
|
||||||
if (!match) {
|
Write-Output "Executing rclone delete..."
|
||||||
error("Could not find the latest zip filename in the PowerShell output using marker.")
|
rclone delete AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --files-from \$tempFile --drive-chunk-size 64M --progress
|
||||||
}
|
|
||||||
def zipFileName = match[0][1]
|
|
||||||
echo "Latest zip file selected: ${zipFileName}"
|
|
||||||
|
|
||||||
// Pass the variable into the environment for the next steps if needed
|
# Clean up the temporary file
|
||||||
env.LATEST_ZIP_FILENAME = zipFileName
|
Remove-Item -Path \$tempFile
|
||||||
}
|
} else {
|
||||||
|
Write-Output "No files to delete."
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Write-Output "No files found on Google Drive to clean up."
|
||||||
}
|
}
|
||||||
}
|
"""
|
||||||
|
|
||||||
stage('Upload to Google Drive using rclone') {
|
|
||||||
steps {
|
|
||||||
echo "Uploading ${env.LATEST_ZIP_FILENAME} to Google Drive..."
|
|
||||||
powershell """
|
|
||||||
\$filePath = "C:/Jenkins/workspace/AzaionSuite/suite/${env.LATEST_ZIP_FILENAME}"
|
|
||||||
Write-Output "Preparing to upload: \$filePath"
|
|
||||||
|
|
||||||
# Display the contents of the rclone configuration file to ensure it's being read correctly
|
|
||||||
Get-Content 'C:/Program Files/rclone/rclone.conf'
|
|
||||||
|
|
||||||
# Explicitly set the rclone config path
|
|
||||||
\$env:RCLONE_CONFIG = 'C:/Program Files/rclone/rclone.conf'
|
|
||||||
|
|
||||||
# Use rclone to upload the file to Google Drive
|
|
||||||
rclone copy "\$filePath" AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --progress --drive-chunk-size 64M
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Cleanup Older Files on Google Drive') {
|
|
||||||
steps {
|
|
||||||
echo "Cleaning up older files on Google Drive..."
|
|
||||||
powershell """
|
|
||||||
# List all files in the Google Drive folder
|
|
||||||
Write-Output "Listing all files in the folder ${params.GOOGLE_DRIVE_FOLDER} on Google Drive..."
|
|
||||||
\$files = rclone lsf --files-only AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --format "tp" --max-depth 1
|
|
||||||
Write-Output "Files found on Google Drive:"
|
|
||||||
Write-Output \$files
|
|
||||||
|
|
||||||
# If files were found
|
|
||||||
if (\$files.Count -gt 0) {
|
|
||||||
# Sort the files by date in descending order
|
|
||||||
\$filesSorted = \$files | Sort-Object -Descending
|
|
||||||
|
|
||||||
# Keep only the 3 latest files
|
|
||||||
\$filesToDelete = \$filesSorted | Select-Object -Skip 3
|
|
||||||
|
|
||||||
Write-Output "Files to delete (older than 3 latest):"
|
|
||||||
Write-Output \$filesToDelete
|
|
||||||
|
|
||||||
# If there are files to delete, remove them
|
|
||||||
if (\$filesToDelete.Count -gt 0) {
|
|
||||||
Write-Output "Deleting files: \$filesToDelete"
|
|
||||||
|
|
||||||
# Create a temporary file to store the list of files to delete
|
|
||||||
\$tempFile = [System.IO.Path]::GetTempFileName()
|
|
||||||
\$filesToDelete | Out-File -FilePath \$tempFile -Encoding utf8
|
|
||||||
|
|
||||||
# Use rclone to delete the files
|
|
||||||
rclone delete AzaionGoogleDrive:${params.GOOGLE_DRIVE_FOLDER} --files-from \$tempFile --drive-chunk-size 64M
|
|
||||||
|
|
||||||
# Clean up the temporary 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 {
|
|
||||||
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.'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user