Apply to a GitHub Repository
So far we’ve learned:
-
what a pipeline template is (the business logic of your pipeline)
-
how to create some mock pipeline libraries (just groovy files implementing a call method inside a directory in a repository)
-
what the pipeline configuration does (implements the template so it actually does things)
-
how to use the same pipeline template with two different tech stacks by modifying the pipeline configuration
Next, we’re going to learn how to apply a pipeline template to an entire GitHub repository.
This is a more realistic scenario and it has the added benefit of taking the pipeline template and pipeline configuration file out of the Jenkins UI and storing them in a pipeline configuration repository.
Move the Pipeline Template to a Repository
When creating libraries, we created a GitHub repository and stored the libraries in a subdirectory called libraries
. In this example, we can create a new subdirectory at the root of the repositories called pipeline-configuration
.
The actual names of the |
Within this pipeline-configuration
directory create a file called Jenkinsfile
and populate it with the same contents as the Pipeline Template
text box in the Jenkins UI.
build()
static_code_analysis()
The |
Move the Pipeline Configuration to a Repository
In the same pipeline-configuration
directory, create a file called pipeline_config.groovy
.
When the pipeline configuration is stored in a file in a source code repository, it will always be called |
Populate this file with the same contents as the Pipeline Configuration
text box in the Jenkins UI.
libraries{
gradle
sonarqube
}
The file structure in your GitHub repository should now look like this:
.
├── libraries
│ ├── gradle
│ │ └── steps
│ │ └── build.groovy
│ ├── maven
│ │ └── steps
│ │ └── build.groovy
│ └── sonarqube
│ └── steps
│ └── static_code_analysis.groovy
└── pipeline-configuration
├── Jenkinsfile
└── pipeline_config.groovy
Create the Global Governance Tier
Now that we have our template and pipeline configuration externalized into a source code repository, we have to tell Jenkins where to find it.
From the Jenkins home page:
-
In the lefthand navigation menu click
Manage Jenkins
-
Click
Configure System
-
Scroll down to the
Jenkins Templating Engine
configuration section -
Under
Pipeline Configuration
selectFrom SCM
-
Select
Git
for theSource Location
drop down menu -
Under
Repository URL
type the https URL for the GitHub Repository containing the libraries, template, and configuration file -
In the
Credentials
drop down menu, select the github credential created during the prerequisites -
Type
pipeline-configuration
in theConfiguration Base Directory
text box -
Click
Save
You just configured your first Governance Tier! Governance Tiers are the combination of:
When done in Governance Tiers can also be configured on every Folder in Jenkins. When configured, they apply to every Job within that Folder. Through Governance Tiers, you can create a governance hierarchy that matches your organizational hierarchy just by how you organize jobs within Jenkins. |
Create an Application Repository
We’re going to apply the pipeline template and configuration file to every branch in a GitHub repository.
-
Create a GitHub Repository that will serve as our mock application repository named
jte-the-basics-app-gradle
-
Initialize the Repository with a README
-
Modify the README in order to create a branch called test
Create a Multibranch Project
Now that we have a GitHub repository representing our application, we can create a Multibranch Project in Jenkins.
Multibranch Projects are Folders in Jenkins that automatically create pipeline jobs for every branch and Pull Request in the source code repository they represent. Through JTE, we can configure each branch and Pull Request to use the same pipeline template. This removes the Jenkinsfile from the repository. |
-
From the Jenkins home page, select
New Item
in the lefthand navigation menu -
In the
Enter an item name
text box, typegradle-app
-
Select
Multibranch Pipeline
as the job type -
Click
OK
-
Under
Branch Sources > Add Source
selectGitHub
-
Select the github credential under the
Credentials
drop down menu -
Enter the https repository URL under
Repository HTTPS URL
-
Under the
Build Configuration
selectJenkins Templating Engine
from themode
drop down menu -
Click
Save
When the job is created, you will be redirected to a page showing the logs for scanning the repository. In the breadcrumbs at the top of the page, you can select gradle-app
to see the folder overview.
In this overview, you’ll see two jobs in progress once the repository scan has repeated: a job for the master
branch and a job for the test
branch.
When these jobs complete, clicking them will show that each branch executed the pipeline template with the same configuration.