在詹金斯管道的多个步骤中定义和访问变量 [英] Define and access a variable in multiple steps of a jenkins pipeline

查看:13
本文介绍了在詹金斯管道的多个步骤中定义和访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自这篇文章 定义变量在 Jenkins Pipeline 的 shell 脚本部分中

我的情况如下,如果生成的文件发生更改(它们每几周或更短时间更改一次),我有一个管道正在更新一些文件并在我的存储库中生成 PR.

My situation is the following I have a pipeline that is updating some files and generating a PR in my repo if there are changes in the generated files (they change every couple of weeks or less).

在我的管道结束时,我有一个发布操作,可以通过电子邮件将结果发送到我们的团队连接器.

At the end of my pipeline I have a post action to send the result by email to our teams connector.

我想知道能否以某种方式生成一个变量并将该变量包含在我的电子邮件中.

I wanted to know if I could somehow generate a variable and include that variable in my email.

它看起来像这样,但当然它不起作用.

It looks something like this but off course it does not work.

#!groovy
String WasThereAnUpdate = '';

pipeline {
    agent any
    environment {
        GRADLE_OPTS = '-Dorg.gradle.java.home=$JAVA11_HOME'
    }
    stages {
        stage('File Update') {
            steps {
                sh './gradlew updateFiles -P updateEnabled'
            }
        }
        stage('Create PR') {
            steps {
                withCredentials(...) {
                    sh '''
                        if [ -n "$(git status --porcelain)" ]; then
                            WasThereAnUpdate="With Updates"
                            ...
                        else
                            WasThereAnUpdate="Without updates"
                        fi
                    '''
                }
            }    
        }
    }
    post {
        success {
            office365ConnectorSend(
                message: "Scheduler finished: " + WasThereAnUpdate,
                status: 'Success',
                color: '#1A5D1C',
                webhookUrl: 'https://outlook.office.com/webhook/1234'
            )
        }
    }
}

我尝试以不同的方式引用我的变量 ${} 等...但我很确定分配不起作用.我知道我可能可以使用脚本块来做到这一点,但我不确定如何将脚本块放入 SH 本身,不确定这是否可能.

I've tried referencing my variable in different ways ${}, etc... but I'm pretty sure that assignment is not working. I know I probably could do it with a script block but I'm not sure how I would put the script block inside the SH itself, not sure this would be possible.

感谢 MaratC https://stackoverflow.com/a/64572833/5685482 的回复和这个文档我会这样做:

Thanks to the response from MaratC https://stackoverflow.com/a/64572833/5685482 and this documentation I'll do it something like this:

#!groovy
def date = new Date()
String newBranchName = 'protoUpdate_'+date.getTime()

pipeline {
    agent any
    stages {
        stage('ensure a diff') {
            steps {
                sh 'touch oneFile.txt'
            }
        }
        stage('AFTER') {
            steps {
                script {
                    env.STATUS2 = sh(script:'git status --porcelain', returnStdout: true).trim()
                }
            }
        }
    }
    post {
        success {
            office365ConnectorSend(
                message: "test ${env.STATUS2}",
                status: 'Success',
                color: '#1A5D1C',
                webhookUrl: 'https://outlook.office.com/webhook/1234'
            )
        }
}

推荐答案

在你的代码中

sh '''
    if [ -n "$(git status --porcelain)" ]; then
        WasThereAnUpdate="With Updates"
        ...
    else
        WasThereAnUpdate="Without updates"
    fi
'''

您的代码会创建一个 sh 会话(很可能是 bash).该会话从启动它的进程(Jenkins)继承环境变量.一旦它运行 git status,它就会设置一个 bash 变量 WasThereAnUpdate (这是一个与可能命名为 Groovy 变量不同的变量.)

Your code creates a sh session (most likely bash). That session inherits the environment variables from the process that started it (Jenkins). Once it runs git status, it then sets a bash variable WasThereAnUpdate (which is a different variable from likely named Groovy variable.)

这个 bash 变量是在您的代码中更新的内容.

This bash variable is what gets updated in your code.

一旦你的 sh 会话结束,bash 进程就会被销毁,它的所有变量也会被销毁.

Once your sh session ends, bash process gets destroyed, and all of its variables get destroyed too.

整个过程对名为 WasThereAnUpdate 的 Groovy 变量没有任何影响,它只是保持以前的状态.

This whole process has no influence whatsoever on Groovy variable named WasThereAnUpdate that just stays what it was before.

这篇关于在詹金斯管道的多个步骤中定义和访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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