Hmmm ... your answer tells me that we don't have a common understanding of the situation. Probably me because I haven't used multi branch so far. I try to depict my understanding based on the documentation:
- given is a multi branch job specified to look for streams
- let us assume that there are e.g. three streams //streams/A, //streams/B, and //streams/C
- each stream has its own Jenkinsfile
- //streams/A/Jenkinsfile
- //streams/B/Jenkinsfile
- //streams/C/Jenkinsfile
Now from structure to behaviour:
- as part of the multi branch job Jenkins probes for streams and find the streams given above
- three pipeline jobs are created:
- for //streams/A based on //streams/A/Jenkinsfile
- for //streams/B based on //streams/B/Jenkinsfile
- for //streams/C based on //streams/C/Jenkinsfile
- based on these jobs three builds are triggered
- a build A based on //streams/A/Jenkinsfile
- a build B based on //streams/B/Jenkinsfile
- a build C based on //streams/C/Jenkinsfile
Now my assumption is that each of these builds only knows about its own Jenkinsfile. So ... with these builds running in parallel gives us
- build A returns //streams/A/Jenkinsfile when echoing environment variable JENKINSFILE_PATH
- build B returns //streams/B/Jenkinsfile when echoing environment variable JENKINSFILE_PATH
- build "C" returns //streams/C/Jenkinsfile when echoing environment variable JENKINSFILE_PATH
Same for some variable P4_STREAM (which needs to be introduced):
- build A returns //streams/A when echoing environment variable P4_STREAM
- build B returns //streams/B when echoing environment variable P4_STREAM
- build C returns //streams/C when echoing environment variable P4_STREAM
In this scenario there is nothing which I would call a "latest stream".
Could you please check where my understanding is wrong?
Okay, maybe ... my example code uses the configuration of a non multi branch job. I see that there might be some race condition if a user changes configuration right after a build has been triggered. I ignore this race condition as this case is far too rare for me. Maybe I have another look at APIs to see whether I can access the necessary information in a more dynamic way - via build object.
And yes, a multi branch job always
dynamically determines all of this. So accessing the (static) configuration does not make sense at all. This also needs to be handled dynamically.
Or does
Jenkins will now probe each child stream associated with the Include streams path //streams/... (effectively all the streams returned by the command p4 streams //streams/...), if the probe finds a Jenkinsfile in the stream then it creates a Pipeline Job and schedules a build.
mean that really a new job is created persistently? With its own configuration? As already stated ... no experience with multi branch setups.
Multi branch seems to make sense if you have a single component built by multiple branches/streams. In our case with a quite large application consisting out of many components we have a lot of Jenkinsfiles inside a single branch/stream which all need to be built. So ...
- multi branch:
component to branch: 1:n
- my scenario:
component to branch: n:1
So whenever we branch from mainline/trunk we also "branch" the corresponding Jenkins pipeline jobs.
Maybe the reason for this different approach is that we host multiple large applications with shared components inside a large mono repo/mainline?
Could someone review the following code? It seems to work fine for me:
Based on what I've provided in my previous post there might be a better/cleaner way to get hands on the stream name for the Jenkinsfile. But for now this workaround seems to be okay.