Getting to Know Jenkins: Your First CI/CD Pipeline

2021-06-22 · 2 min read

Jenkins is an open-source automation server written in Java, and it's one of the most common tools for running CI/CD. In practice, it automates three things teams usually do by hand: testing, building, and deploying, every time new code lands in the repository.

Installing Jenkins

The quickest path is a small install script you run directly on the server:

git clone https://github.com/hiibnu/jenkins-run.git
cd jenkins-run
./test.sh

The script shows a menu to pick your Linux distro (CentOS 7, Ubuntu/Debian, or a sample Jenkinsfile option). Once it finishes, Jenkins is reachable at http://ip-server:8080, since 8080 is the default port.

On first run, Jenkins asks to be unlocked with an auto-generated password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

From there, install the "suggested plugins" set, create an admin account, and configure the Jenkins URL.

Connecting a GitHub Repository

Create a new job of type Pipeline. Under the General tab, check "GitHub project" and paste the repo URL. Under Build Triggers, check "GitHub hook trigger for GITScm polling" so pushes can kick off a build.

In the Pipeline tab, pick Git as the SCM (the same steps work for Bitbucket). Public repos can leave credentials blank; private ones need a username/password or SSH key. Set the branch to build, and the Script Path — which defaults to looking for a file literally named Jenkinsfile.

For pushes to actually trigger builds, add a webhook in the GitHub repo's Settings pointing to .

NextLearning Bun Basics: A Faster JavaScript Runtime
http://ip-jenkins:8080/github-webhook/

Writing the Jenkinsfile

A basic declarative Jenkinsfile usually has an environment block, an agent, and a few stages:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
        sh "ls -lisa"
      }
    }
    stage('Test') {
      steps { echo 'Testing...' }
    }
    stage('Deploy') {
      steps { echo 'Deploying...' }
    }
  }
}

The Build stage is often just there to sanity-check the workspace before moving on to testing and deployment.

This only scratches the surface. Jenkins can do a lot more — like firing off a Telegram or email notification once a build finishes — but that's a story for another post.