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

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

问题描述

我来自这篇文章定义变量在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天全站免登陆