Setting up the Build

Now that we've set up a freestyle project, let's set up our build environment. Follow these steps: 

  1. On the project configuration page, select Apply and then Save. This should take you to the following page.
  1. Select Build Now on the left navigation menu to run the build.

Notice that a number pops up on the Build History. Hover over the build number and select the drop-down list. This presents a quick menu for your builds. Open the Console Output to view the logs of our build, as shown here: 

  1. Hover over the number to get a drop-down, and select Console Output to view the build logs. The logs will tell us what happened when our code was being pulled, and if the build has an issue, this is the quickest way to identify it. Your window should have an output that's similar to the following:

From the logs, we can identify the following:

    • Who triggered the build
    • The commands that were run to achieve the build's purpose
    • Any changes made
    • Whether the build was a Success or a Fail

We haven't run any code or tests, so let's go ahead and complete our build by adding this.

  1. Select Back to Project and then Configure, on the left navigation menu, which should take us back to the project configuration page.
In our case, we want Jenkins to run our tests from the tests.py file. In an ideal local environment, we would run the tests on the terminal or by using Git Bash:
 
  1. Under Build, select Add build step. This will enable us to add a step for Jenkins to run our tests.
We will choose Shell, even though we are running Jenkins on Windows. Why? Because Jenkins is running on a Docker container, which has a Linux operating system by default.

To achieve the same result, we will need to tell Jenkins to run the script, as we saw on the preceding terminal. This can be achieved by selecting Execute shell from the Add build step drop-down. With this in mind, we can execute any kind of script, provided the necessary languages or tools are available on the Docker container. We are executing a shell, even though we are on a Windows operating system, because Jenkins is running in a Docker container:

  1. Select Execute shell and the following window should pop up.
You can add as many build steps as required. For post-build items, it's preferred to use the Post-build Actions resource.
Since this is the first time we are running Python scripts on Jenkins, despite having the Python plugin, we also need to install Python. Why? Because the Docker Container doesn't come with Python pre-installed.
  1. Add the commands on the prompt, as follows:
  1. Select Apply, then Save. This will take you back to the project window. 
  2. On the left navigation menu, select Build Now to build the project.

Jenkins will automatically schedule the build and display a new build number on the Build History pane, as shown:

Now, go ahead and open the Console Output and view the logs. Since Python is already set up on this container, the output shown here won't display the logs; however, the test results will be displayed as follows:

Congratulations! We now have a complete build. Let's quickly recap what our build entails:

  1. Jenkins will first pull the code from GitHub after we add the repository details.
  2. Through the build step, Jenkins will run the Python script.
  3. The logs on the console output tell us whether our build has failed or passed.

As we progress, we will get to understand how to set up more complex builds that can be triggered automatically and even send notifications.

Back on the main dashboard, notice how Jenkins has updated to factor in our build. We can now easily view and access our build(s), and on the left navigation menu, we can also view any build that is queued.

We will discuss views in the next section.

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

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