P M A J E W S K I

Please Wait For Loading

Introduction to Jenkins and Pipelines - Software Developer's Tour

    You Are Currently Here!
  • Home
  • TutorialsIntroduction to Jenkins and Pipelines
Logo Jenkins

Introduction to Jenkins and Pipelines

What is Pipeline?

Pipeline is a solution that automatically build, test and deploy an application. We can split Pipeline to (CI) and (CD) Pipelines.

CI – Continous Integration

CD – Continous Delivery/Deployment

CI – helps maintain code quality by catching errors early in the development process.

CD – streamlines the process of delivering code changes to production environments efficiently and reliably.

Use it in practice!

Today, I’ll show you how to use Jenkins to create CI for your application.

To follow this tutorial, go to https://www.jenkins.io/doc/book/installing/ and install Jenkins.

Create console application in Visual Studio

Console.WriteLine($"It works");

Push it to your remote repository. You can see an example  on https://github.com/MajewskiPawel/app-ci-cd

Use Jenkins!

If your repository is private, you’ll need to add credentials to authorize Jenkins with your Git provider. Navigate to Dashboard -> Manage Jenkins -> Credentials -> System -> Global credentials (unrestricted).

When you click “Add credentials” button choose “SSH Username with private key” in “Kind” field.

Username” – username or email

Private Key” -> click “Enter directly

To add Private Key you need public and private key. 

Generate SSH

After this, you will find both jenkins and jenkins.pub files in your user directory.

Open jenkins.pub file, copy entire value and paste it in your profile configuration on your git provider (example for GitHub is presented below).

Open jenkins file (without pub extension), copy entire value and paste it in Private Key field in Jenkins credentials configurator.

Jenkins credentials added

After saving the form, you should see a list of credentials. Save the ID of the created element because we will need it later.

Create new pipeline

On the main dashboard click “New Item” and add new Pipeline

Create new pipeline

Below is the script I have created to build previously created application.

pipeline {
    agent any
    
    stages {
        stage ('Git') {
            steps {
                git branch: 'main', credentialsId: 'test-application', url: 'https://github.com/MajewskiPawel/app-ci-cd'
            }
        }
        stage('Restore packages') {
            steps {
                powershell "dotnet restore '${workspace}\\App\\App.sln'"
            }
        }
        stage('Build project') {
            steps {
                powershell "dotnet build --configuration Release '${workspace}\\App\\App.csproj'"
            }
        }
    }
}

Run it!

Click “Build Now” button on the left navigation bar and see the magic.

We can configure Jenkins to synchronize with Git events and our builds can be executed when pull request have been created or when we push something to master branch.

Build success

Why are we doing this?

Before I explain, please modify your application code to the presented below.

var z = 
Console.WriteLine($"It works");

and push it to your repository.

Click “Build Now” button again.

Build failed

If we had it connected to Git events, we would know immediately when our code has failed, right after someone pushed it to the main branch.

Everything we just did is called CI. Not so scary, right :)?

leave a comment