Jenkins: Fundamentals

Welcome back! Now that you’ve survived the Jenkins introduction, it’s time to get serious. You can install Jenkins and admire its web UI, but can you actually make it do something useful? In this chapter, we’ll dive into the fundamentals: creating jobs, defining pipelines, managing artifacts, and ensuring your workspaces don’t turn into a digital junkyard. Let’s roll up our sleeves and get Jenkins doing the hard work for you.

Creating Jenkins Jobs and Pipelines

Understanding Freestyle Jobs vs. Pipeline Jobs

Freestyle jobs are the “plug-and-play” option—point, click, and execute. Great for simple builds, but as your CI/CD gets more complex, you’ll want pipelines. Pipelines are Jenkins’ way of letting you script your build processes, making them more reusable, scalable, and automation-friendly.

Creating and Configuring Jobs in Jenkins

  1. Navigate to New Item in Jenkins.
  2. Choose Freestyle Project for simple automation or Pipeline for more flexibility.
  3. Configure source code management (Git, SVN, or whatever you fancy).
  4. Define build steps (execute shell, invoke Gradle, run tests, etc.).
  5. Save and build. Jenkins will either reward you with success or drown you in logs.

Defining Build Triggers and Scheduling Jobs

Jenkins doesn’t wake up and run jobs on its own—you need to tell it when. Some options:

  • SCM Polling: Jenkins periodically checks for repo changes.
  • Webhooks: Jenkins reacts instantly when your repo updates.
  • Cron Jobs: Define schedules like H 3 * * * (which makes sense to someone, probably).
  • Manual Triggering: Click “Build Now” when you feel like it.

Managing Build Histories and Logs

  • View past builds under the job dashboard.
  • Check logs when things inevitably break.
  • Use console output to debug (or panic) when you see a wall of red text.

Using Jenkinsfile and Declarative Pipeline Syntax

Introduction to Jenkinsfile and Pipeline as Code

Jenkinsfiles are the magic scripts that define your pipeline. Keep them in version control to make your builds repeatable and less reliant on human memory (which, let’s be honest, is unreliable).

Writing and Storing Jenkinsfile in Source Control

Place a Jenkinsfile in your repository and tell Jenkins to use it. Example:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application...'
            }
        }
    }
}

Declarative Pipeline Structure

  • pipeline {}: Defines the pipeline.
  • agent any: Runs on any available node.
  • stages {}: Contains multiple stage {} blocks.
  • steps {}: The actual commands that Jenkins runs.

Scripted Pipeline Basics

For those who like full control (and complexity), there’s the scripted pipeline:

node {
    stage('Build') {
        sh 'make build'
    }
    stage('Test') {
        sh 'make test'
    }
    stage('Deploy') {
        sh 'make deploy'
    }
}

Managing Builds and Artifacts

Understanding Build Artifacts and Workspace Management

Artifacts are the outputs of a build—binaries, reports, logs, or anything else you need to keep.

Storing and Archiving Artifacts in Jenkins

Use the archiveArtifacts step to keep important files:

post {
    success {
        archiveArtifacts artifacts: 'build/*.jar', fingerprint: true
    }
}

Uploading Artifacts to External Storage

If your CI/CD needs to push artifacts elsewhere, use plugins for:

  • Nexus/Artifactory: To store dependencies.
  • AWS S3: For cloud storage.
  • FTP Servers: If you’re feeling nostalgic.

Cleaning Up Old Builds and Artifacts Automatically

Jenkins doesn’t clean up after itself. Use Build Discarder settings or pipeline script:

properties([
    buildDiscarder(logRotator(numToKeepStr: '10'))
])

Understanding Job Management and Workspaces

How Jenkins Manages Workspaces Per Job

Each job gets its own workspace directory where builds happen. Multiple builds running? Multiple workspaces.

Sharing Workspaces Between Jobs

If jobs need to share data, use:

  • Copy Artifacts Plugin: Moves files between jobs.
  • Shared Storage (like NFS): Good for large teams.
  • Parameterized Builds: Pass variables across jobs.

Using Custom Workspace Directories

Want builds to happen in a specific directory? Define a custom workspace under Advanced Project Options.

Best Practices for Efficient Workspace Management

  • Use clean workspaces: Avoid leftover junk between builds.
  • Archive artifacts smartly: Keep only what’s needed.
  • Limit build history: Don’t hoard old logs.

Hands-on Exercises

Enough talk—time to get your hands dirty! Try these tasks:

  • Create and configure freestyle and pipeline jobs.
  • Write a Jenkinsfile and run a basic CI/CD pipeline.
  • Archive and manage build artifacts in Jenkins.
  • Implement automated workspace cleanup strategies.

References

For when Jenkins inevitably confuses you:

By the end of this module, you should have a solid understanding of jobs, pipelines, and artifacts. Now go automate like a pro! 🚀