patternMinor
Is it possible to use a JenkinsFile in one repo to build and deploy a related repo?
Viewed 0 times
relateddeployjenkinsfileonepossibleandusebuildrepo
Problem
Our team has two separate repositories for a frontend/backend system. They would like to have these two repositories deployed to the shared team environment together.
My understanding of the JenkinsFile is that it will only work on the repository it committed to and only run when the SCM system sends a request to Jenkins informing it of new changes.
Is it possible to have the JenkinsFile in the two separate repositories communicate with each other so that when one is built the other is built as well and only after both are built, they will then be deployed?
My main goal here is to avoid creating a separate Jenkins job within the Jenkins UI.
My understanding of the JenkinsFile is that it will only work on the repository it committed to and only run when the SCM system sends a request to Jenkins informing it of new changes.
Is it possible to have the JenkinsFile in the two separate repositories communicate with each other so that when one is built the other is built as well and only after both are built, they will then be deployed?
My main goal here is to avoid creating a separate Jenkins job within the Jenkins UI.
Solution
Yes, this is pretty easy with Jenkinsfiles with no need for any third-party plugins or anything along those lines: use the built-in Pipeline build step.
I use this to trigger builds of projects in a dependency chain, so that after one project builds successfully, other projects that depend on it will pull in the updated dependency and build against it.
Here is what this looks like in a Jenkinsfile:
where
Things can get a little more complicated, such as if you're using folders or Multibranch Pipelines:
This example builds a job within a Multibranch project where the variables are:
Or this example builds a job with parameters and also triggers the build asynchronously (parent job won't wait for child job to complete before moving on to the next step):
As you can see, the invocation of
I use this to trigger builds of projects in a dependency chain, so that after one project builds successfully, other projects that depend on it will pull in the updated dependency and build against it.
Here is what this looks like in a Jenkinsfile:
build repoNamewhere
repoName is a variable containing the name of the repository you wish to build.Things can get a little more complicated, such as if you're using folders or Multibranch Pipelines:
build "${projectName}/${repoName}/${branchName}"This example builds a job within a Multibranch project where the variables are:
projectName- the name of my Multibranch project
repoName- the name of the repository
branchName- the name of the branch to build, usuallymaster
Or this example builds a job with parameters and also triggers the build asynchronously (parent job won't wait for child job to complete before moving on to the next step):
build(
jobName,
wait: false,
parameters: [
[
$class: 'BooleanParameterValue',
name: 'someBooleanParameter',
value: true,
],
[
$class: 'StringParameterValue',
name: 'someStringParameter',
value: 'some string value',
],
],
)As you can see, the invocation of
build can get relatively complex. See the documentation linked above for more complete information.Code Snippets
build repoNamebuild "${projectName}/${repoName}/${branchName}"build(
jobName,
wait: false,
parameters: [
[
$class: 'BooleanParameterValue',
name: 'someBooleanParameter',
value: true,
],
[
$class: 'StringParameterValue',
name: 'someStringParameter',
value: 'some string value',
],
],
)Context
StackExchange DevOps Q#2941, answer score: 3
Revisions (0)
No revisions yet.