Global Variables

A variable is a name associated to a value. A global variable is accessible in any scope within our program and is not bound to any scope, such as a function.

While working with the scripted Jenkins pipeline, we can configure it to work with different branches. There are pre-defined global variables available on Jenkins that allow us to use conditionals to perform different actions. For instance, there is a BRANCH_NAME global variable, which allows you to perform different actions, such as deploying while building the master branch, as opposed to while building a feature or bug branch. In this section, we'll learn how to set up global variables and create a multibranch pipeline in Jenkins.

  1. To view the global variable reference, go to the pipeline project configuration page.
  2. Go to the text area where you entered the pipeline script.
  3. Click on the hyperlink labelled Pipeline Syntax.
  1. This will land you on the Pipeline Syntax page.

This page serves as documentation and a quick reference while working with pipelines. There is a snippet generator that helps you while working with the scripted pipeline to generate different portions of the pipeline. This is very important as a remembering aid since some pipeline steps have complicated syntax that might be difficult to master.

  1. On the left-hand menu, open the Global Variable Reference item. This opens the reference page, entailing a global variable reference that would take a while to go through. In our case, we are looking for the BRANCH_NAME variable. We can find this under the env section. The following output displays a portion of the variables: 

The first variable is the BRANCH_NAME; this is followed by a short description of exactly what it does and how it can be used. We will add a conditional to our pipeline that will just print out a message that our application is being deployed when building the master branch.

Refer to the complete code, which has been placed at https://bit.ly/2KWNgug.
  1. Add the following updated script to your pipeline after the testing stage to create a new multibranch project, and add our Jenkinsfile to version control. The final script looks as follows:
  1. Create this script, add it to the root folder of your project while on the master branch, and name it Jenkinsfile. This Jenkinsfile is located in the Lesson5 folder on the GitHub repository.
  2. Add this script to the staging area, create a commit, and push it to the GitHub repository.
  1. Create a new project on Jenkins and, under the Project type, select Multibranch pipeline and enter an appropriate name for the project.
  1. Under Branch Sources, select Add source and select GitHub, as follows.

This will pop up a configuration form asking us for the origin repository. Configure the form, filling in Owner with your GitHub username or the username of where the repository is hosted, and Repository with the name of the repository. Leave the rest as defaults, just like in the following screenshot:

As we can see from the Credentials section, there is a warning informing us that adding credentials is recommended. We'll need to add GitHub credentials.

  1. Select Add Dropdown and select Jenkins, as shown:
  1. Under Kind, select Username with password.
  1. Enter your GitHub username and password in their respective fields and select Add.
The ID will be automatically generated by Jenkins, and the description is optional.
  1. Back on the project configuration page, select the credentials you just created under the Credentials section.
We mentioned the importance of the question mark icon in an earlier section. You can use this icon at this point to discover more about the rest of the configurations under the Behaviors section.
  1. Under the Build Configuration section, set up the project as shown.

This configuration informs Jenkins where to find the Jenkinsfile we added to our project earlier, which in our case is in the root folder of our project. The rest of the configuration is not relevant for this project, so we can leave it as is.

  1. Select Apply and Save to persist the configuration. Going back to the project dashboard, we can see that our pipeline already ran against the master branch.
  1. On the left-hand panel, select Open Blue Ocean. Under the Branches tab, select the first item, which in our case is master. We will view this project using Blue Ocean as follows:

From the output, we can see that our pipeline ran and added the new stage we defined in our script.

Looking closely at the deployment stage output, we can see that the deployment message that we configured for the master branch, Deploying the master branch, was displayed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.252.8