0
0
Jenkinsdevops~5 mins

Jenkinsfile concept - Commands & Configuration

Choose your learning style9 modes available
Introduction
A Jenkinsfile is a text file that defines the steps Jenkins will run to build, test, and deploy your software. It helps automate these tasks so you don't have to do them manually every time.
When you want to automate running tests every time you save code changes.
When you need to build your app and create a package automatically.
When you want to deploy your app to a server without manual steps.
When you want to keep your build and deployment steps in the same place as your code.
When you want to share your automation steps with your team easily.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying the application...'
            }
        }
    }
}

pipeline: Defines the entire automation process.

agent any: Runs the pipeline on any available Jenkins worker.

stages: Groups the steps into phases like Build, Test, and Deploy.

steps: The actual commands or actions Jenkins will run in each stage.

Commands
Check if Jenkins job builder is installed and its version to ensure Jenkins can read Jenkinsfiles.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-job-builder 3.10.0
Initialize a git repository to track your Jenkinsfile and code changes.
Terminal
git init
Expected OutputExpected
Initialized empty Git repository in /home/user/my-app/.git/
Add the Jenkinsfile to the git repository so it can be version controlled.
Terminal
git add Jenkinsfile
Expected OutputExpected
No output (command runs silently)
Save the Jenkinsfile in git with a message describing the change.
Terminal
git commit -m "Add Jenkinsfile with build, test, deploy stages"
Expected OutputExpected
[master (root-commit) abcdef1] Add Jenkinsfile with build, test, deploy stages 1 file changed, 15 insertions(+) create mode 100644 Jenkinsfile
-m - Add a commit message inline
Trigger the Jenkins pipeline named 'my-pipeline' which uses the Jenkinsfile to run the automation.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 Waiting for build to complete... Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: a Jenkinsfile stores your automation steps as code so Jenkins can run them automatically.

Common Mistakes
Writing Jenkinsfile syntax incorrectly without proper indentation or missing braces.
Jenkinsfile uses Groovy syntax and requires correct structure; errors cause pipeline failures.
Use a text editor with Groovy support and validate the Jenkinsfile syntax before committing.
Not committing the Jenkinsfile to the git repository.
Jenkins cannot find the Jenkinsfile to run the pipeline if it is not in the repository.
Always add and commit the Jenkinsfile to your source control along with your code.
Running pipeline commands without configuring Jenkins to use the Jenkinsfile.
Jenkins needs to be set up to look for the Jenkinsfile in the repository to run the pipeline.
Configure your Jenkins job to use Pipeline script from SCM and point to your repository.
Summary
Create a Jenkinsfile to define build, test, and deploy steps as code.
Add and commit the Jenkinsfile to your git repository for version control.
Trigger the Jenkins pipeline to run the automation steps defined in the Jenkinsfile.