This post explains two of the ways to create an OpenShift project—inside the Jenkinsfile and using a template.
While working on a demo, I came across a use case where I need to create an OpenShift project via a Jenkinsfile. So, I started exploring various ways to create a project. This post explains two of the ways—one inside the Jenkinsfile and other using a template.
The easiest way to create a project is by using the
Creating an OpenShift project in a Jenkinsfile
Jenkinsfiles are written in Groovy Domain Specific Language (DSL) syntax. The Jenkins Pipeline execution engine supports two DSL syntaxes: Declarative and Scripted. A part of the declarative pipeline syntax looks as shown below:
As you can see, the
sh' ' line in the syntax above is an indication that shell commands can be executed inside the Jenkinsfile. You can also execute a shell script file.
Before using the above syntax, the Jenkins service account needs to have self-provisioner permission:
This is the simple and straightforward. The other way to create a project is using a template, which I'll detail in the following section.
Creating an OpenShift project using templates
A template describes a set of objects that can be parameterized and processed to produce a list of objects for creation by the OpenShift Container Platform. A template can be processed to create anything you have permission to create within a project, such as services, build configurations, and deployment configurations. A template can also define a set of labels to apply to every object defined in the template.
The OpenShift cluster comes with built-in templates. To check all of them, run the following command:
Let's start by creating a bootstrap template file:
template.yaml should look like this:
f you observe closely, there are few parameters that needs to be passed, and you can do that with the following command. This command created a project called
Now, when you run the below command you should see a new project
dev in the list of projects: