Hello Everyone !!
Harness Next Gen is a very powerful Platform Suite which allows you to now create, complete Integrated Pipelines which contain both CI & CD as part of it and more. In this Article we will we be discussing the CI section of the pipeline and how we can use variables and values in the pipeline to work with the codebase branch we plan to use.
Lets Get Started.
Let’s take an example of a CI pipeline which pulls the code from Github which we plan to run the build with. Here when creating the Github Repo Connector and Setting up the Codebase on the pipeline level, we can notice that there are no fields where we can specify the branch of the Repository we plan to pull the Code from.
First we will be creating the Connector :
Once the Connector is setup we can then configure the Codebase at the Pipeline level
When we have completed building the Pipeline after configuring the resources and click on “Run” to Deploy it, you will notice that the prompt will now ask us to provide the Branch for the Repo where we plan to pull the Codebase from. This will only pickup the value for the branch when manually provided at runtime.
In order to deploy the Pipeline you would need to provide this branch at runtime every-time for the Codebase clone to take place.
We can try and automate the process of providing the branch at runtime by using Triggers.
Here is an example of a Trigger which will run the CIE Pipeline based on “Push” condition for the Branch we can provide
At the final page we can see that the CI Codebase is now using the variable
<+trigger.brach> which will be the brach from which the Codebase will be picked up.
However, in our Trigger config you can see that we are tracking a branch which lets assume does not contain the Codebase we plan to build our Artifact with. The branch which would be picked up here would be “test-branch” and not the master branch we plan to deploy with.
This brings us to a position where we have below requirements :
- We plan to clone the Codebase independent of the Trigger condition from a Branch of our choosing.
- The branch we plan to clone from should be picked up automatically without needing to provide it at runtime always.
We can achieve our Requirement in the below ways :
Explicitly Defining the Branch Name in the YAML
The default YAML for your Pipeline would contain the below config for the CI Codebase, here you can see that the
build attribute contains the runtime variable
<+input> which means the value needs to be provided at runtime.
properties: ci: codebase: connectorRef: Zee-CI-Code build: <+input>
In order to automate this so that the same Brach is always used to clone from, you will need to edit the content of your Pipeline YAML, primarily the CI Codebase chunk as below :
properties: ci: codebase: connectorRef: Zee-CI-Code build: type: branch spec: branch: master
Here you can see that we have added some extra lines in the YAML format under the “build” attribute, this allows us to explicitly mention which branch we want the codebase clone to happen from when executed, we have specified “master” here which is not our default branch.
Using a User Defined Variable for the Branch
This case is similar to the above one with one key difference which is the use of User Declared variables in the Pipeline. As an example we have declared a custom variable called “branch” in the Pipeline stage and provided its value to be Master.
Instead of directly specifying the branch name explicitly in the YAML, we can reference it to use the User Defined variable we have created, and the YAML would then look like below :
properties: ci: codebase: connectorRef: cieconnector build: type: branch spec: branch: <+pipeline.variables.branch>