小编car*_*rmo的帖子

在声明性 Jenkins 管道中捕获 sh 命令的输出

在下面的代码中,我试图将 'ls -l /' 结果分配给b全局变量,但是,当我尝试打印其中的内容时,结果是null.

如何设置全局变量?

def b = [:]
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script{
                    b = sh 'ls -l /'
                    println "b:"+b
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是结果:

[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ ls -l /
total 24
drwxr-xr-x   2 root root 4096 Jan 18 11:49 bin
drwxr-xr-x   2 root root    6 Oct 20 10:40 boot …
Run Code Online (Sandbox Code Playgroud)

groovy jenkins-pipeline jenkins-declarative-pipeline

5
推荐指数
1
解决办法
3148
查看次数

在声明性 Jenkins 管道中的环境变量中存储值列表

我有以下管道,但我不知道为什么它在第一行代码中失败:

pipeline {
    agent any
    environment {
        def mypods = []
    }
    stages {
        stage('Getting pods') {
            steps {
                script {
                   withKubeConfig(caCertificate: '.....', credentialsId: '.....', serverUrl: '.....') {
                       env.mypods = sh "kubectl get pod | grep Running | awk '{print \$1}'"
                    }
                }
            }
        }
        stage('Print pods') {
            steps {
                script {
                    mypods.each {
                        println "Item: $it"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要使用一个列表,因为 kubectl get pods 命令返回一个 pod 列表,所以我必须在阶段中保存和使用它们。如何在声明性管道上创建列表?提前致谢。

这是错误:

Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: …
Run Code Online (Sandbox Code Playgroud)

groovy kubernetes devops jenkins-pipeline

4
推荐指数
2
解决办法
1万
查看次数