Jenkins声明式管道,在从属代理上运行groovy脚本 [英] Jenkins Declarative Pipeline, run groovy script on slave agent

查看:178
本文介绍了Jenkins声明式管道,在从属代理上运行groovy脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jenkins声明式管道,我一直在Jenkins主数据库上运行,并且工作正常.但是,既然我已经尝试在从属节点上执行此操作,则在管道中调用的常规脚本无法访问工作空间中的文件.

I have a Jenkins declarative pipeline I have been running on the Jenkins master and it works fine. However, now that I have moved to trying to execute this on a slave node, the groovy scripts which are called in the pipeline can not access the files in the workspace.

我的jenkinsfile看起来像这样...

My jenkinsfile looks like this...

pipeline {

agent {
  label {
        label "windows"
        customWorkspace "WS-${env.BRANCH_NAME}"
  }
}

stages {
  stage('InitialSetup') {
   steps {
     "${env.WORKSPACE}/JenkinsScripts/myScript.groovy"
    }
  }
}

我可以在从属服务器上看到它正在创建工作区,从git进行检出并正确执行脚本.但是,如果脚本中的某些内容尝试与工作空间中的文件进行交互,则它将失败.

I can see on the slave that it is creating the workspace, doing the checkout from git, and executing the script correctly. However, if something in the script try's to interact with the files in the workspace it fails.

如果我有类似这样的简单内容...

If I have something simple like this...

def updateFile(String filename) {
  echo env.NODE_NAME
  filename = "${env.WORKSPACE}/path/to/file"
  def myFile = new File(filename)
  <do other things with the file>
}

...它说找不到指定的文件.它为我提供了它正在寻找的路径,并且我可以确认该文件是否存在,并且该代码在仅在主服务器上构建时就可以运行.

...it says it can not find the file specified. It gives me the path it is looking for and I can confirm the file exists, and that the code runs when just building on the master.

为什么当脚本只能在主节点上运行时,脚本无法通过这种方式找到文件?我在groovy文件中添加了"echo env.NODE_NAME"命令,它说脚本正在正确的节点上执行.

Why can the script not find the files this way when in can just running on the master node? I added the "echo env.NODE_NAME" command into my groovy file and it says the script is executing on the correct node.

谢谢.

推荐答案

结果显示Groovy File命令被认为是不安全的,尽管它们将在主服务器上运行,但不会在从属服务器上运行.如果您从将代理设置为另一个节点的脚本中调用它们,它将仍然可以在主节点上而不是在代理上执行命令.这是文章帖子的摘录 https://support .cloudbees.com/hc/zh-CN/articles/230922508-Pipeline-Files-manipulation

Turns out Groovy File commands are considered insecure, and although they will run on the master, they will not run on the slave. If you call them from a script that has the agent set to another node, it will still execute the command just fine, just on the master node, not the agent. Here's an excerpt of an article post https://support.cloudbees.com/hc/en-us/articles/230922508-Pipeline-Files-manipulation

File类的操作在master上运行,因此仅当build在master上运行时才起作用,在此示例中,我创建了一个文件,并检查是否可以在存在方法的节点上访问它,因为该方法不存在new File(file)在主服务器上执行,要进行检查,我搜索存在于主服务器上但不在节点中的文件夹Users.

The operation with File class are run on master, so only works if build is run on master, in this example I create a file and check if I can access it on a node with method exists, it does not exist because the new File(file) is executed on master, to check this I search for folder Users that exist on my master but not in the node.

stage 'file move wrong way'

  //it only works on master
  node('slave') {

    def ws = pwd()
    def context  = ws + "/testArtifact"
    def file = ws + '/file'
    sh 'touch ' + file
    sh 'ls ' + ws

    echo 'File on node : ' + new File(file).exists()
    echo 'Users : ' + new File('/Users').exists()

    sh 'mv ' + file + ' ' + context
    sh 'ls ' + ws
  }

要执行文件操作命令,我们建议使用本机命令.

To execute file manipulation command we recommend to use native commands.

这是在shell中进行操作的简单示例

This is a simple example of operations in shell

stage 'Create file'
  sh 'touch test.txt'

stage 'download file'
  def out='$(pwd)/download/maven.tgz'
  sh 'mkdir -p ./download'
  sh 'curl -L http://ftp.cixug.es/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz -o ' + out

stage 'move/rename'
  def newName = 'mvn.tgz'
  sh 'mkdir -p $(pwd)/other'
  sh 'mv ' + out + ' ' + newName
  sh 'cp ' + newName + ' ' + out
}

这篇关于Jenkins声明式管道,在从属代理上运行groovy脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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