Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 0136fd1

Browse files
committed
fix: make plugin work on nodes
The workspace is not accessible directly inside plugins, the plugin must use a FileCallable that is serialized and called on the node having the workspace.
1 parent 65ae51c commit 0136fd1

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ Configure as follow where
1717
pipeline {
1818
agent any
1919
stages {
20+
stage('prepare file ') {
21+
steps {
22+
sh "echo 'someContent' > toUpload.txt"
23+
}
24+
}
2025
stage("upload to gdrive") {
2126
steps {
2227
withCredentials([string(credentialsId: 'gdrive', variable: 'GDRIVE_CREDENTIALS')]) {
2328
gdriveUpload(
2429
googleCredentials: GDRIVE_CREDENTIALS,
25-
source: "/Users/baptiste/work/test_upload",
26-
destinationId: "14_Xpzuld0lGyg7HgZojwNsJsITW5jTh9"
30+
source: "toUpload.txt",
31+
destinationId: "14_Xpzuld0lGyg7HgZojwNsJsITW5jTh9",
32+
renameTo: "uploaded.txt"
2733
)
2834
}
2935
}

gdrive-core/src/main/kotlin/org/bonitasoft/gdrive/core/GDriveUploadTask.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ class GDriveUploadTask(private val googleCredentials: String,
4343
private fun copy(drive: Drive, file: java.io.File, destinationFolder: File, renameTo: String = "") {
4444
logger.debug("Processing ${file.absolutePath}")
4545
val fileName = renameTo.trim().ifEmpty { file.name }
46+
if (fileName != file.name) {
47+
logger.info("'${file.name}' will be renamed to '$fileName'")
48+
}
49+
if (!file.exists()) {
50+
throw RuntimeException("The source file does not exists: ${file.absolutePath}");
51+
}
4652
if (file.isDirectory) {
4753
val newFolder = createFolder(drive, fileName, destinationFolder)
48-
logger.debug("created folder ${newFolder.name} with id ${newFolder.id}")
54+
logger.debug("Created folder $fileName with id ${newFolder.id}")
4955
file.listFiles().forEach { child ->
5056
copy(drive, child, newFolder)
5157
}
5258
} else {
5359
val uploadFile = uploadFile(drive, file, fileName, destinationFolder)
54-
logger.info("Uploaded file folder ${uploadFile.name} with id ${uploadFile.id}")
60+
logger.info("Uploaded file ${uploadFile.name} with id ${uploadFile.id}")
5561
}
5662
}
5763

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package org.bonitasoft.gdrive.jenkins
22

33
import hudson.AbortException
4+
import hudson.FilePath
5+
import hudson.FilePath.FileCallable
46
import hudson.model.TaskListener
7+
import hudson.remoting.VirtualChannel
58
import org.bonitasoft.gdrive.core.GDriveUploadTask
69
import org.bonitasoft.gdrive.core.Logger
710
import org.jenkinsci.plugins.workflow.steps.StepContext
811
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution
12+
import org.jenkinsci.remoting.RoleChecker
13+
import java.io.File
14+
import java.io.Serializable
915

1016

1117
class GDriveUploadStepExecution(
@@ -18,14 +24,10 @@ class GDriveUploadStepExecution(
1824
override fun run(): Void? {
1925
val logger = context.get(TaskListener::class.java)?.logger!!
2026
try {
21-
val gdriveLogger = object : Logger {
22-
override fun debug(message: String) = logger.println("DEBUG: $message")
23-
override fun info(message: String) = logger.println("INFO: $message")
24-
override fun warn(message: String) = logger.println("WARN: $message")
25-
override fun error(message: String) = logger.println("ERROR: $message")
26-
}
27-
val task = GDriveUploadTask(googleCredentials, source, destinationId, gdriveLogger, renameTo)
28-
task.execute()
27+
28+
val workspace = context.get(FilePath::class.java)!!
29+
val logs = workspace.child(source).act(UploadFile(googleCredentials, destinationId, renameTo))
30+
logs.forEach { logger.println(it) }
2931
return null
3032
} catch (e: Throwable) {
3133
e.printStackTrace()
@@ -35,3 +37,33 @@ class GDriveUploadStepExecution(
3537

3638

3739
}
40+
41+
class UploadFile(val googleCredentials: String, val destinationId: String, val renameTo: String) : FileCallable<List<String>>, Serializable {
42+
43+
override fun checkRoles(p0: RoleChecker?) = Unit
44+
override fun invoke(file: File?, p1: VirtualChannel?): List<String> {
45+
if (file == null) {
46+
return listOf("File argument is null")
47+
}
48+
val logs = mutableListOf<String>()
49+
val gdriveLogger = object : Logger {
50+
override fun debug(message: String) {
51+
// logs.add("DEBUG: $message")
52+
}
53+
54+
override fun info(message: String) {
55+
logs.add("INFO: $message")
56+
}
57+
58+
override fun warn(message: String) {
59+
logs.add("WARN: $message")
60+
}
61+
62+
override fun error(message: String) {
63+
logs.add("ERROR: $message")
64+
}
65+
}
66+
GDriveUploadTask(googleCredentials, file.absolutePath, destinationId, gdriveLogger, renameTo).execute()
67+
return logs
68+
}
69+
}

0 commit comments

Comments
 (0)