如何使用命令的结果来决定詹金斯阶段是否应该执行?

发布于 2025-02-06 09:40:23 字数 277 浏览 2 评论 0原文

我有一个非常简单的声明性詹金斯文件,该文件使用curl从API检索配置文件,然后使用diff命令查看它们是否与存储库中的同一配置文件不同。如果检索配置文件不同,我想替换旧文件并提交新文件。

我似乎无法弄清楚如何存储一个值(例如$ config_changed = yes)并在下一阶段/步骤中使用它。理想情况下,如果不更改配置,我想跳过几个阶段,但是我不知道如何在整个管道上重复使用变量。我已经搜索了很多谷歌,但是似乎环境变量是不变的,并且在管道中无法更改。也许有一种非常简单的方法我没有看到?我将感谢一些朝着正确方向的指示。

I have a very simple declarative Jenkins file which uses cURL to retrieve config files from an API and then uses the diff command to see if they are different from the same config files in the repository. If the retrieves config files are different, I would like to replace the old files and commit the new ones.

I can't seem to figure out how to store a value (for example $CONFIG_CHANGED = YES) and use it in the next stage/step. Ideally I would like to skip a couple of stages if the config is not changed, but I don't know how to re-use variables across the pipeline. I have googled quite a bit but it seems that environment variables are immutable and can't be changed in the pipeline. Maybe there is a really simple approach to this that I am not seeing? I would appreciate some pointers in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2025-02-13 09:40:23

第一部分将说明如何在Shell脚本中提取设置的值。然后,我将解释如何有条件运行一个阶段。

以下是可以从外壳执行中提取值的几种方法。

  1. 执行脚本并阅读标准。请注意,您写信给Stdout的内容将被附加并返回。
                res = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                echo "1234"
                echo $CONFIG_SET''', returnStdout: true).trim()
                echo "$res"
  1. 返回退出状态。在这里,您可以返回出口代码,而不是返回Stdout。您可以以检查参数并返回正确的退出状态的方式创建一个Shell脚本。
                res2 = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                if [ $CONFIG_SET == "YES" ]
                then
                   exit 0
                else
                    echo "1111"    
                    exit 1
                fi
                ''', returnStatus: true) == 0
                echo "$res2"
  1. 写入文件并读取文件。
                sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                echo $CONFIG_SET > output
                ''')
                res3 = readFile('output').trim()
                echo "$res3"

提取值后,您可以定义一个新阶段,并在{}时添加条件检查。以下是完整的管道。

def res2 = false

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                res = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                echo "1234"
                echo $CONFIG_SET''', returnStdout: true).trim()
                echo "$res"
                
                res2 = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                if [ $CONFIG_SET == "YES" ]
                then
                   exit 0
                else
                    echo "1111"    
                    exit 1
                fi
                ''', returnStatus: true) == 0
                echo "$res2"
                
                
                sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                echo $CONFIG_SET > output
                ''')
                res3 = readFile('output').trim()
                echo "$res3"
                
                def type = res.class
            }
        }
    }
        stage('IFYES') {
           when { expression { return res2} }
            steps {
                echo "Executing"
        }
    }
}
}

The first part will explain how to extract the value set in the shell script. Then I'll explain how to conditionally run a stage.

Here are a few ways you can extract the value from shell execution.

  1. Executing a script and reading the standard out. Note that what ever you write to STDOUT will be appended and returned.
                res = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                echo "1234"
                echo $CONFIG_SET''', returnStdout: true).trim()
                echo "$res"
  1. Returning the exit status. Here instead of returning the STDOUT you can return the exit code. YOu can create a shell script in such a way it check the parameter and return the correct exit status.
                res2 = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                if [ $CONFIG_SET == "YES" ]
                then
                   exit 0
                else
                    echo "1111"    
                    exit 1
                fi
                ''', returnStatus: true) == 0
                echo "$res2"
  1. Writing to a file and reading the file.
                sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                echo $CONFIG_SET > output
                ''')
                res3 = readFile('output').trim()
                echo "$res3"

After extracting the value you can define a new stage and add a conditional check with when{}. Following is the full Pipeline.

def res2 = false

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                res = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                echo "1234"
                echo $CONFIG_SET''', returnStdout: true).trim()
                echo "$res"
                
                res2 = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                if [ $CONFIG_SET == "YES" ]
                then
                   exit 0
                else
                    echo "1111"    
                    exit 1
                fi
                ''', returnStatus: true) == 0
                echo "$res2"
                
                
                sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                echo $CONFIG_SET > output
                ''')
                res3 = readFile('output').trim()
                echo "$res3"
                
                def type = res.class
            }
        }
    }
        stage('IFYES') {
           when { expression { return res2} }
            steps {
                echo "Executing"
        }
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文