将 Jenkins 输入步骤中的文件上传到工作区 [英] Upload file in Jenkins input step to workspace

查看:208
本文介绍了将 Jenkins 输入步骤中的文件上传到工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Jenkins的输入步骤"将二进制文件上传到当前工作区.

但是,下面的代码似乎是将文件上传到 Jenkins 主站,而不是上传到正在运行作业的从站上当前作业的工作区.有什么办法可以解决吗?

最好不必在主盘上添加执行程序或将文件弄乱主盘.

def inFile = input id: 'file1', message: 'Upload a file', parameters: [file(name: 'data.tmp', description: 'Choose a file')]

解决方案

似乎 Jenkins 正式不支持二进制文件的上传,正如您在

3) 在你的工作中访问共享库

  • Jenkinsfile中,添加如下代码:
<小时>

@Library('my-shared-library@master') _节点{//使用任何文件名代替我在下面使用的 *demo-backend-1.0-SNAPSHOT.jar*def file_in_workspace = copy_bin_to_wksp.inputGetFile('demo-backend-1.0-SNAPSHOT.jar')sh "ls -ltR"}

<小时>

您已准备好运行该作业.:)

注意:

  • 确保脚本安全插件始终是最新的
  • 脚本安全如何影响共享库?
  • 全球共享库始终运行在沙箱之外.这些库被认为是受信任的":它们可以运行 Java、Groovy、Jenkins 内部 API、Jenkins 插件或第三方库中的任何方法.这允许您定义库,这些库将单独不安全的 API 封装在更高级别的包装器中,以便从任何管道中安全使用.请注意,任何能够将提交推送到此 SCM 存储库的人都可以获得对 Jenkins 的无限制访问权限.
  • 文件夹级共享库始终在沙箱内运行.基于文件夹的库被视为受信任的":它们在 Groovy 沙箱中运行,就像典型的流水线一样.

代码参考:James Hogarth 的 评论

I would like to use the "input step" of Jenkins to upload a binary file to the current workspace.

However, the code below seems to upload the file to the Jenkins master, not to the workspace of the current job on the slave where the job is running. Is there any way to fix that?

Preferably without having to add an executor on the master or clutter the master disk with files.

def inFile = input id: 'file1', message: 'Upload a file', parameters: [file(name: 'data.tmp', description: 'Choose a file')]

解决方案

Seems Jenkins officially doesn't support upload of binary file yet as you can see in JENKINS-27413. You can still make use of the input step to get binary file in your workspace. We will be using a method to get this working but we will not use it inside the Jenkinsfile otherwise we will encounter errors related to In-process Script Approval. Instead, we will use Global Shared Libraries, which is considered one of Jenkins' best practices.

Please follow these steps:

1) Create a shared library

  • Create a repository test-shared-library
  • Create a directory named vars in above repository. Inside vars directory, create a file copy_bin_to_wksp.groovy with the following content:

def inputGetFile(String savedfile = null) {
    def filedata = null
    def filename = null
    // Get file using input step, will put it in build directory
    // the filename will not be included in the upload data, so optionally allow it to be specified

    if (savedfile == null) {
        def inputFile = input message: 'Upload file', parameters: [file(name: 'library_data_upload'), string(name: 'filename', defaultValue: 'demo-backend-1.0-SNAPSHOT.jar')]
        filedata = inputFile['library_data_upload']
        filename = inputFile['filename']
    } else {
        def inputFile = input message: 'Upload file', parameters: [file(name: 'library_data_upload')]
        filedata = inputFile
        filename = savedfile
    }

    // Read contents and write to workspace
    writeFile(file: filename, encoding: 'Base64', text: filedata.read().getBytes().encodeBase64().toString())
    // Remove the file from the master to avoid stuff like secret leakage
    filedata.delete()
    return filename
}


2) Configure Jenkins for accessing Shared Library in any pipeline job

  • Go to Manage Jenkins » Configure System » Global Pipeline Libraries section
  • Name the library whatever you want (in my case, my-shared-library as shown below)
  • Keep the default to master (this is the branch where i pushed my code)
  • No need to check/uncheck the check-boxes unless you know what you're doing

3) Access shared library in your job

  • In Jenkinsfile, add the following code:

@Library('my-shared-library@master') _

node {
   // Use any file name in place of *demo-backend-1.0-SNAPSHOT.jar* that i have used below
   def file_in_workspace = copy_bin_to_wksp.inputGetFile('demo-backend-1.0-SNAPSHOT.jar')
   sh "ls -ltR"
}


You're all set to run the job. :)

Note:

  • Make sure Script Security plugin is always up-to-date
  • How are Shared Libraries affected by Script Security?
  • Global Shared Libraries always run outside the sandbox. These libraries are considered "trusted:" they can run any methods in Java, Groovy, Jenkins internal APIs, Jenkins plugins, or third-party libraries. This allows you to define libraries which encapsulate individually unsafe APIs in a higher-level wrapper safe for use from any Pipeline. Beware that anyone able to push commits to this SCM repository could obtain unlimited access to Jenkins.
  • Folder-level Shared Libraries always run inside the sandbox. Folder-based libraries are not considered "trusted:" they run in the Groovy sandbox just like typical Pipelines.

Code Reference: James Hogarth's comment

这篇关于将 Jenkins 输入步骤中的文件上传到工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆