简体   繁体   中英

Jenkinsfile update a file with a line

I am looking for a way to update the properties file in my git repo with "LIB_VERSION=$(env.BUILD_NUMBER)" , but not able to do it.

environment {
    BUILD_NUMBER = "${env.BUILD_NUMBER}"
}

stages {
   stage('Update Version') {
       steps {
          script {
               sh(script: 'echo "LIB_VERSION=$(env.BUILD_NUMBER)" > version.properties', returnStdout: true).trim()  
                }
              }
            }
        }

Current Output:

env.BUILD_NUMBER: not found

echo LIB_VERSION=

How can get the build number? Even after getting the build number, will it be update the properties file in jenkins workspace or will it update the actual properties file in git repo?

I am looking for a way to update the properties file in my git repo

Currently you are resolving the BUILD_NUMBER environment variable within a shell interpreter with a literal string. This means you must resolve it with shell syntax like:

sh(script: 'echo "LIB_VERSION=$BUILD_NUMBER" > version.properties', returnStdout: true).trim()

Alternatively, if you wanted to resolve the environment variable within Groovy, then you could use Groovy syntax and access it within the env map:

sh(script: "echo 'LIB_VERSION=${BUILD_NUMBER}' > version.properties", returnStdout: true).trim()

Regardless of your decision, mixing the syntax of the two will not work.

Also note that:

environment {
  BUILD_NUMBER = "${env.BUILD_NUMBER}"
}

is unnecessary as it essentially sets the BUILD_NUMBER environment variable equal to itself, so it can be removed if you wish.

Finally, also note that there will probably be no stdout to return from your method, so you can likely omit those method chains from your method. This also means you can potentially simplify with the writeFile step method:

writeFile(file: 'version.properties', text: "LIB_VERSION=${BUILD_NUMBER}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM