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

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

问题描述

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

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

但是,下面的代码似乎将文件上载到Jenkins主文件,而不是上载到作业正在运行的从属服务器上的当前作业的工作空间. 有什么办法可以解决这个问题?

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')]

推荐答案

似乎Jenkins官方还不支持二进制文件的上传,正如您在全局共享库,这被视为詹金斯的最佳做法之一.

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.

请按照以下步骤操作:

1)创建一个共享库

  • 创建存储库 test-shared-library
  • 在上述存储库中创建一个名为vars的目录.在vars目录中,创建具有以下内容的文件copy_bin_to_wksp.groovy:
  • 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)配置Jenkins以在任何管道作业中访问共享库


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

  • 转到管理Jenkins»配置系统»全局管道库"部分
  • 根据需要命名库(在我的情况下为my-shared-library,如下所示)
  • 将默认设置保留为master(这是我推送代码的分支)
  • 除非您知道自己在做什么,否则无需选中/取消选中复选框

3)访问您工作中的共享库

3) Access shared library in your job

  • Jenkinsfile中,添加以下代码:
  • 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. :)

注意:

  • 确保脚本安全插件始终是最新的
  • 共享库如何受脚本影响安全吗?
  • 全局共享库始终运行外部沙箱.这些库被认为是受信任的":它们可以在Java,Groovy,Jenkins内部API,Jenkins插件或第三方库中运行任何方法. 这允许您定义将不安全的API分别封装在更安全的高级包装中的库,以供任何管道使用.. 请注意,任何能够将提交推送到此SCM存储库的人都可以无限制地访问Jenkins..
  • 文件夹级共享库始终在沙箱中运行.基于文件夹的库被认为是受信任的":它们像典型的管道一样在Groovy沙箱中运行.
  • 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.

代码参考:James Hogarth的

Code Reference: James Hogarth's comment

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

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