ASP.NET Core,Jenkins • Posted 2 months ago
So far we have seen how we can setup a jenkins automation server in a server node, and see how we can setup a git repository using github to maintain our codebase we are looking at to setup for automation. Next, we shall look at how we can use the maintained repository codebase to be fetched and configured to be built for a specific trigger.
Jenkins comes up with support for git based builds as a built-in feature and hence we shall not require any additional plugins to be added for us to configure the repository.
Our flow goes like this:
First, we shall add new credentials to the credential store. Jenkins maintains all of the credentials which are used in various job under a secure credential store. This is in a way useful and secure so that we don't need to think about all the places where the credentials have been entered if at all they're to be changed or lost. I go to the jenkins homepage and click on the Credentials link in the left menu.
Next, I click on Jenkins under the Credentials stores and then click on Global Credentials. This way, the credentials we store shall be available across all the jobs and menus in the jenkins server. Next, I click on Add Credentials in the left hand menu and select the Kind "Username and Password". Since github issues a username and password for its private repositories, this one is the simplest.
I type in the username and password and leave the ID (which shall be auto generated by the system) and Description empty. I have created a credentials entry and thus the step one is complete.
Second, we shall modify the job we created previously to fetch and use the git repository for processing. I click on configure on the Job home page in the left menu, and then in the Source Code Management section of the project, I chose git under the options. Now we get three blanks to be filled, first one being the Repository URL and the Credentials dropdown. I give my Github repository url (same as what I use for cloning: https://github.com/xxxxx/xxxxx.git) and for Credentials, I select the credentials I created just a bit ago in the credential store. Next, under the branches to build I leave it unchanged for the codebase I'm interested in automating is in the master branch which is the default entry. If any other branch needs to be checkedout for build, we can specify it here.
Third, I shall configure the jenkins job to build the project codebase that has been fetched from the git repository so far. And since the codebase is an aspnetcore project (ReadersApi.csproj) I shall use the dotnet CLI commands to build the project. As a prerequisite, it is mandatory to have the dotnet core sdk installed on the machine where the Jenkins is installed. Obvious right?
Under the Build section within the command box, I give the below commands:
dotnet clean && dotnet build
And where does this commands run? By default, all the instructions that are executed under a jenkins job run under the job directory under workspaces within the jenkins installation directory. And since each job runs under its own directory, each job is isolated and runs under its own environment. When the git repository is invoked for to fetch the codebase under the master branch, the code is fetched and placed under the root of the job directory and hence these commands run directly on the fetched codebase without any issues.
Now that we are done with the three steps, we shall save the changes and hit on Build Now to see how the build happens. All the things happenning can be seen under the build log which we can see by clicking on the currently executing build number shown under the build history section in the left side.
From the logs we can see that the job has fetched the codebase from the git repository using the credentials we mapped under the credential store and the build has been successful. Since there were no errors that happened within the build or any step within the instructions, the build is marked as a success.
This way we can configure a jenkins job to build a project by fetching from the git repository. In the next article, we shall see how we can add test script execution for the existing build pipeline and then trigger mails upon issues.
Published 2 months ago