本文介绍了Jenkinsfile Windows cmd输出变量参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对groovy很陌生.在我的Jenkinsfile中,我试图将Windows cmd输出存储在变量中,在下一个命令中使用它,但似乎没有任何效果.这是我得到的最接近的东西:

I'm pretty new to groovy.In my Jenkinsfile i am trying to store a windows cmd output in a variable, use it in the next command, but nothing seems to be working. This is the closest i got:

    pipeline {
    agent any    
    stages {        
        stage('package-windows') {
            when {
                expression { isUnix() == false}
            }
            steps {             
                script {
                FILENAME = bat(label: 'Get file name', returnStdout: true, script:"dir \".\\archive\\%MY_FOLDER%\\www\\main.*.js\" /s /b")              
                }
                bat label: 'replace content', script: "powershell -Command \"(gc \"$FILENAME\") -replace \"https://my-qa.domain.com/api/\", \"https://my-prod.domain.com/api/\" | Out-File \"$FILENAME\"\""
            }
        }
    }
}

当我执行ECHO "$FILENAME"时,这是我得到的输出:

When i do ECHO "$FILENAME" this is the output i am getting:

C:\Program Files (x86)\Jenkins\workspace\my-ui>dir ".\archive\55131c0d3c28dc69ce39572eaf2f8585996d9108\main.*.js" /s /b 
C:\Program Files (x86)\Jenkins\workspace\my-ui\archive\55131c0d3c28dc69ce39572eaf2f8585996d9108\www\main.16aedaf4c6be4e47266d.js

我所需要的只是在下一个命令中修改内容所使用的文件名main.16aedaf4c6be4e47266d.js.但是在下一个命令中,"$FILENAME"为空.如何将命令输出正确地存储在变量中,并在下一个命令中访问?

All i need is the file name main.16aedaf4c6be4e47266d.js to be used in the next command to modify the contents. But in the next command "$FILENAME" is empty. How can i store the command output in a variable correctly and access in the next commands ?

推荐答案

问题是您捕获了命令的完整输出,其中包括2行.第一行是使用dir命令的当前路径,第二行是所需的输出.首先回显命令,您将看到命令+输出.然后,随后的使用将导致看起来空的结果,但实际上是一条混乱的行,因为FILENAME变量包含2行,每行末尾都有新行.

The problem is that you capture the complete output of the command, this includes 2 lines. First line is the current path with the dir command, the second is the required output. The first you echo the command you will see this, command + output. Then subsequent usage will result in what looks an empty result but is actually a messed up line as the FILENAME variable contains 2 lines, each having a new line at the end.

在批处理命令前添加@可以防止将其回显,这就是您想要的.现在,FILENAME变量将只有一行包含您的文件名.不过,您仍然需要从结果中修剪CRLF,否则它将使您的下一个powershell命令混乱.

Adding a @ in front of a batch command will prevent it to be echoed back and this is what you want. Now the FILENAME variable will only have the one line with your filename in it.Still, you will need to trim the CRLF from the result or else it will mess up your next powershell command.

我认为以下脚本应该更好地工作.

I think the below script should work better.

pipeline {
    agent any
    stages {        
        stage('package-windows') {
            when {
                expression { isUnix() == false}
            }
            steps {             
                script {
                  FILENAME = bat(label: 'Get file name', returnStdout: true, script:"@dir \".\\archive\\%MY_FOLDER%\\www\\main.*.js\" /s /b").trim()
                }
                echo FILENAME
                bat label: 'replace content', script: "powershell -Command (gc \"$FILENAME\") -replace \"https://my-qa.domain.com/api/\", \"https://my-prod.domain.com/api/\""
            }
        }
    }
}

这篇关于Jenkinsfile Windows cmd输出变量参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:21