问题
Is it possible to see if a pipeline job is the first run of a newly discovered branch from inside the pipeline itself? If you could get a list of all the runs of a job maybe you could see if there are 0 previous runs and thereby know its the first run?
回答1:
Use env.BUILD_NUMBER. env.BUILD_NUMBER == '1' if this is the first build.
For newly discovered branches in Multibranch Pipeline (which I assume you use) this will work.
回答2:
In case you mean to identify the build number from within the Jenkins pipeline, then yes, it can be identified using the pipeline Global Variable currentBuild. This variable may be used to discover information about the currently executing pipeline, with properties such as currentBuild.result, currentBuild.displayName, etc. You may use the property currentBuild.id to know the build number and hence whether it is the first build or not.
You may consult the built-in Global Variable Reference at the URL http://[your-Jenkins-host]/pipeline-syntax/globals#currentBuildfor for a complete and up-to-date list of properties available on currentBuild.
Hope, it helps.
来源:https://stackoverflow.com/questions/54151175/can-you-identify-from-inside-a-jenkins-pipeline-if-its-the-first-run-of-a-new-br