Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Jenkinsfile Basics

1. Introduction

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. It is typically written in Groovy and is stored in the source control repository of the project, allowing for versioning and collaboration.

2. What is a Jenkinsfile?

Jenkinsfile is a way to define a Jenkins Pipeline as code. It allows you to specify how your CI/CD process should be executed, including build, test, and deployment stages.

Note: A Jenkinsfile can be stored in the root of your repository or in a subdirectory.

3. Pipeline Structure

A Jenkins pipeline can be defined in two ways: declarative and scripted. The declarative syntax is simpler and recommended for most users.

3.1 Declarative Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                // Build steps go here
            }
        }
        stage('Test') {
            steps {
                // Test steps go here
            }
        }
        stage('Deploy') {
            steps {
                // Deployment steps go here
            }
        }
    }
}

3.2 Scripted Pipeline

node {
    stage('Build') {
        // Build steps go here
    }
    stage('Test') {
        // Test steps go here
    }
    stage('Deploy') {
        // Deployment steps go here
    }
}

4. Stages and Steps

Each pipeline consists of stages that group related steps. Steps are the individual tasks that Jenkins performs, such as executing shell commands or running scripts.

Stages

  • Define a sequence of operations.
  • Can be parallelized.

Steps

  • Individual commands or actions within a stage.
  • Can include scripts, shell commands, or other plugins.

5. Example Jenkinsfile

Here’s a simple example of a Jenkinsfile that builds, tests, and deploys a Node.js application:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
        stage('Deploy') {
            steps {
                sh 'npm run deploy'
            }
        }
    }
}

6. Best Practices

  • Keep Jenkinsfile simple and readable.
  • Use descriptive stage names.
  • Leverage environment variables for configuration.
  • Version control your Jenkinsfile alongside your code.

7. FAQ

What is the difference between declarative and scripted pipelines?

Declarative pipelines provide a simplified syntax and structure, while scripted pipelines offer more flexibility and control over the pipeline execution.

Can I use multiple Jenkinsfiles in one repository?

Yes, you can have multiple Jenkinsfiles in different directories or with different names, but you will need to specify which one to use in the pipeline job configuration.

How do I run a Jenkins pipeline?

A Jenkins pipeline can be triggered by various means, such as a commit to the repository, a manual trigger, or a scheduled event.