Creating a Basic Jenkinsfile
To get started, create a new file in VS Code named “Jenkinsfile”. VS Code automatically recognizes this file extension, enabling syntax highlighting and other helpful features. Let’s start with a simple “Hello World” pipeline:- The
agent anydirective allows Jenkins to run the pipeline on any available build agent or on the Jenkins server. - The
stagesblock contains a single stage labeled “Hello”. - Under the
stepssection, the commandecho 'Hello World'prints the message to the console.
Introducing a Pipeline that Pulls Code from GitHub
Next, we examine a Jenkinsfile that interacts with GitHub. In this example, the pipeline checks out code from a GitHub repository during the build stage:- The
agent anydirective indicates that any available build agent can be used. - A single stage, “Build”, is defined.
- Within the “Build” stage, the
gitstep retrieves the code from the specified GitHub repository, making it available for further processes such as testing or deployment.
Constructing a Multi-Stage Pipeline
Multi-stage pipelines enable you to separate your build, test, and deployment processes into distinct stages within a single Jenkinsfile. Below is an example demonstrating two stages, “Dev” and “UAT”, that both pull code from GitHub:- The
stagesblock is divided into two stages: “Dev” and “UAT”. - Both stages use the
gitstep to pull code from the GitHub repository. - This structure allows you to extend each stage with additional tasks such as running tests, performing quality checks, or deploying to different environments.
This basic multi-stage design can be easily extended to fit more complex deployment workflows, ensuring that each environment receives the necessary configuration and testing procedures.
Now that we’ve covered the basics of creating and structuring Jenkinsfiles—from a single-stage “Hello World” pipeline to a more advanced multi-stage pipeline—it’s time to put this knowledge into practice. Head over to the Jenkins portal to start building and experimenting with your own code pipelines. That’s it for this lesson. We’ll see you in the next one!