Building prerequisite artifact only when changed -- one pipeline job or two separate jobs?


I have two repositories, repo1 and repo2. The build output of repo1, artifact1, is a prerequisite for building repo2, producing artifact2. If repo1 changes, both artifact1 and artifact2 need to be rebuilt. If only repo2 changes, then only artifact2 needs to be rebuilt (using the most recently built artifact1). Ultimately, it’s artifact2 that gets deployed.

I can create one pipeline job which builds artifact1 in one stage, stashes it (and also archives it), then the next stage unstashes artifact1 and uses it along with repo2 to build artifact2.

This is simple and works, however repo1/artifact1 takes a long time to build. If a change to repo2 triggers the build, not a change to repo1, there is no need to re-build repo1/artifact1.

Can I create a pipeline that only rebuilds artifact1 when necessary, otherwise re-use (e.g. with the copy artifact plugin) the archived artifact from the last successful build where artifact1 was built?

I could do this with two separate jobs: job1 to build artifact1, triggered by changes to repo1, and when job1 builds successfully, it could trigger job2, which builds artifact2. job2 could also be triggered by a change to repo2. No matter how job2 is triggered (by job1 success or by repo2 change), it would always use copy artifact plugin to grab the most recent artifact1 from job1.

Is the two-separate-jobs approach recommended, or is there a better way I can define this all with pipeline and define it in a single Jenkinsfile (which I think I’d prefer)?