How to create a Jenkinsfile through a Jenkins Pipeline

Jenkins—an open-source automation server— is a popular option for automating numerous operations during the software development process. Its primary advantage is its ability to construct development and deployment pipelines as code using a Jenkinsfile. This Answer will show how to create a Jenkinsfile using the Jenkins Pipeline, enabling us to automate and manage our CI/CD workflows effectively.

What is a Jenkinsfile?

A text file called Jenkinsfile describes the whole Pipeline for a given job in Jenkins. Developers can express their build and deployment workflows in a human-readable style because of Jenkins’ Pipeline as Code methodology. By doing away with the requirement for manual job configuration through the Jenkins web interface, the CI/CD process becomes more standardized and repeatable.

Creating a Jenkinsfile

  1. Installing Jenkins: We install and configure Jenkins first, then set it up. If Jenkins is not installed, we can download it from the official website, install it, and ensure it is operational.

  2. Installing plugins: We need to install a few plugins to enable particular functionality in our Jenkins Pipeline. For example, we must install the Python plugin to execute Python codes. We can accomplish this by going to “Manage Jenkins” > “Plugins” and looking for and installing the necessary plugins.

  3. New “Pipeline” job creation: In Jenkins, we start a new “Pipeline” job. We choose “Pipeline script from SCM” and give it a name. Using a Jenkinsfile kept in our version control system enables us to define our pipeline this way. 

  4. Source code management (SCM) options: We select the preferred SCM program (such as Git or Subversion). Jenkins retrieves the Jenkinsfile from our repository.

  5. Repo link: We provide the URL of the SCM repository where our Jenkinsfile is stored in the “Specify Repository URL” field. We ensure that Jenkins can access this repository.

  6. Setting the “Script Path” section: We enter the location of the Jenkinsfile in the repository in the “Script Path” section. We can use “Jenkinsfile” because it is typically in the root directory.

  7. Saving and building: We save the settings for our job and start a build. Following the instructions in the file, Jenkins executes the Pipeline after retrieving the Jenkinsfile from our SCM repository.

Start page of Jenkins
Start page of Jenkins
1 of 10

Writing the Jenkinsfile

Typically, a Jenkinsfile is written in the simple-to-read and write programming language Groovy. We can generate the Jenkinsfile using the Jenkins web-based editor or any preferred code editor.

pipeline {
agent any
stages {
stage('Version') {
steps {
// check your language version
sh 'python3 --version'
}
}
stage('Build and Run') {
steps {
// Build your code here
sh 'python3 code.py'
}
}
}
}
Boilerplate code

In this straightforward example, the two key stages are Version and Build and Run. Each stage includes particular steps associated with a particular CI/CD workflow. These phases and processes are adaptable to the needs of our project.

Understanding the Jenkinsfile

  • pipeline {: This is the start of the pipeline block, which defines the entire pipeline. It sets the Pipeline configuration.

  • agent any: This specifies the type of agent to use for running the Pipeline. The any parameter ensures that the Pipeline can run on any available agent or executor in the Jenkins environment.

  • stages {: This opens the stages block, which contains a list of stages to be executed in the Pipeline.

  • stage {: This defines the stage with our defined name. Stages are the building blocks of a Pipeline and represent distinct steps in our workflow.

  • steps {: This opens the steps block, which contains a list of commands or steps to be executed inside the stage.

  • sh 'python3 --version': This is a step within the Version stage. It uses the sh step to execute a shell command. In this case, it runs the python3 --version command to check the version of Python 3 installed on the agent.

  • sh 'python3 code.py': This is a step within the Build and Run stage. It uses the sh step to execute the python3 code.py command, which builds and runs the Python code located in the code.py file.

Conclusion

The Jenkins Pipeline is a potent tool for automating software development operations and may be used to create a Jenkinsfile. The whole CI/CD workflow can be defined as code, which makes it easier to manage, version control, and reproduce. It is possible to fully utilize Jenkins and streamline our software development and deployment process by following the procedures described in this Answer. To ensure a more seamless and effective CI/CD process, keep in mind to regularly enhance and modify the Jenkinsfile as the project develops.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved