Jenkinsfile windows cmd output variable reference

十年热恋 提交于 2021-01-27 23:45:30

问题


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\"\""
            }
        }
    }
}

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

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 ?


回答1:


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.

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/\""
            }
        }
    }
}


来源:https://stackoverflow.com/questions/55329493/jenkinsfile-windows-cmd-output-variable-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!