Understanding the syntaxes and components of a Jenkinsfile is key to mastering your CI/CD pipelines. A Jenkinsfile is a text-based script written in a Groovy-like language that defines and automates various phases of your software delivery process. In this lesson, we cover declarative pipelines. (A later lesson will detail scripted pipelines.) The Jenkinsfile is typically divided into distinct stages, each corresponding to a phase within your CI/CD process. Common stages include:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Source Code Management: Checking out code from version control systems (e.g., GitHub, Bitbucket, GIT).
- Build: Compiling, building, and packaging your application.
- Test: Running test cases (unit, integration, etc.) to ensure quality.
- Deploy: Pushing build artifacts to staging or production environments.
The
pipeline keyword denotes the start of your pipeline definition. Immediately following is the agent directive, which specifies the execution environment for your pipeline stages. Commonly, you can use any (which will run on any available Jenkins agent, including the controller) or specify a Docker container. For instance, using a Docker image guarantees that your build stage runs in an environment with the required tools.
Within the pipeline, stages structure the workflow. Each stage block contains one or more steps that execute individual commands. These steps may run shell commands or invoke Jenkins Pipeline DSL commands. For more intricate logic beyond the declarative syntax, use the script step to incorporate complex Groovy code.
Below is a comprehensive example to illustrate these concepts:
- Declarative Pipelines: Provide a structured and straightforward syntax.
- Stages and Steps: Define the execution flow of the pipeline.
- Scripted Steps: Allow for complex Groovy code within the declarative pipeline.
1. Environment Variables
Theenvironment directive lets you define key-value pairs for environment variables globally or within a specific stage. For example:
2. Post Actions
Thepost directive specifies actions that run after the pipeline completes, whether it succeeds or fails. This feature is perfect for cleanup tasks or sending notifications.
3. Scripted Steps
Thescript block is used for incorporating Groovy code within a declarative pipeline. It provides the flexibility to loop through files or execute conditional logic:
4. Conditional Execution and Credentials
Thewhen directive conditionally executes a stage based on factors such as branch name or build result:
withCredentials step:
5. Interactive Input and Parameters
During execution, the pipeline can prompt users using theinput directive:
6. Stashing and Unstashing Artifacts
With thestash and unstash directives, you can temporarily store build artifacts or files between stages or nodes. This method is especially useful for caching build results:
7. Parallel Execution
To optimize pipeline run times, stages can be executed concurrently using theparallel directive. Ensure that parallel stages are independent to avoid interfering with shared outputs:
This lesson covered the primary components and directives of a Jenkinsfile. Explore these features in your projects and look forward to upcoming lessons that delve into more advanced Jenkins configurations.