Jenkins共享库上执行的函数在错误的节点上执行

huangapple go评论46阅读模式
英文:

Jenkins function of shared library executed on wrong node

问题

以下是您要翻译的内容:

我有一个简单的共享库,想要调用一个创建文件的函数。
我的流水线脚本如下所示:

@Library('jenkins-shared-library')_

pipeline {
    agent {
        node {
             label 'node1'
        }
    }
 
    stages {
        stage('test') {
            
            steps {
                script {
                    def host = sh(script: 'hostname', returnStdout: true).trim()
                    echo "Hostname is: ${host}"
                    
                    def testlib = new TestLibrary(script:this)
                    testlib.createFile("/tmp/file1")
                }
            }
        }
    }
}

这个流水线作业由另一个作业触发,该作业在内置主节点上运行。
阶段'test'在'node1'上正确执行。

问题:创建的文件"/tmp/file1"在jenkins主节点上创建,而不是"node1"上。

我还尝试过没有共享库,并在步骤中直接加载一个Groovy脚本:

pipeline {
    agent {
         node {
             label 'node1'
         }
    }
 
    stages {
        stage('test') {
            steps {
                script {
                    
                    def script = load "/path/to/script.groovy"
                    script.createFile("/tmp/file1")
                }
            }
        }
    }
}

这也会在主节点上创建文件,而不是"node1"上。

是否没有加载外部库或类并在阶段运行的节点上执行它们的方法?我不想直接将所有代码放入步骤中。
英文:

I have a simple shared library and want to call a function which creates a file.
My pipeline script looks like this:

@Library('jenkins-shared-library')_

pipeline {
    agent {
        node {
             label 'node1'
        }
    }
 
    stages {
        stage('test') {
            
            steps {
                script {
                    def host = sh(script: 'hostname', returnStdout: true).trim()
                    echo "Hostname is: ${host}"
                    
                    def testlib = new TestLibrary(script:this)
                    testlib.createFile("/tmp/file1")
                }
            }
        }
    }
}

This pipeline job is triggered by another job which runs on the built-in master node.
The stage 'test' is correctly executed on the 'node1'.

Problem: The created file "/tmp/file1" is created on the jenkins master, instead of "node1"

I also tried it without the shared library and load a groovy script
directly in a step:

pipeline {
    agent {
        node {
             label 'node1'
        }
    }
 
    stages {
        stage('test') {
            steps {
                script {
                    
                    def script = load "/path/to/script.groovy"
                    script.createFile("/tmp/file1")
                }
            }
        }
    }
}

This also creates the file on the master node, instead on "node1".

Is there no way of loading external libs or classes and execute them on the node where the stage is running on ? I dont want to place all the code directly into the step.

答案1

得分: 0

我自己找到了,Groovy脚本根据定义始终在主节点上运行。无法在主节点之外的节点上运行脚本或共享库代码。

英文:

Ok I found it myself, groovy scripts run by definition always on the master node. No way to run scripts or shared-library code on a node other than master.

huangapple
  • 本文由 发表于 2023年2月19日 17:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定