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

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

问题描述

我有一个 Jenkins 声明式管道,我一直在 Jenkins 主机上运行,​​它运行良好.但是,现在我已经开始尝试在从节点上执行此操作,在管道中调用的 groovy 脚本无法访问工作区中的文件.

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.

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

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 命令被认为是不安全的,虽然它们会在 master 上运行,但它们不会在 slave 上运行.如果您从将代理设置到另一个节点的脚本中调用它们,它仍然会很好地执行命令,只是在主节点上,而不是代理上.这是一篇文章的摘录 https://support.cloudbees.com/hc/en-us/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 上运行的,所以只有在 master 上运行 build 时才有效,在这个例子中,我创建一个文件并检查我是否可以在具有方法的节点上访问它,它不存在,因为new File(file) 在 master 上执行,为了检查这一点,我搜索存在于我的 master 但不在节点中的文件夹 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天全站免登陆