0
0
Jenkinsdevops~5 mins

Jenkins in the CI/CD ecosystem - Commands & Configuration

Choose your learning style9 modes available
Introduction
Jenkins helps automate software building, testing, and deployment. It solves the problem of doing these tasks manually, which can be slow and error-prone.
When you want to automatically test your code every time you save changes.
When you need to build your software and create packages without manual steps.
When you want to deploy your app to a server automatically after tests pass.
When multiple developers work on the same project and need consistent builds.
When you want to track the status of your software builds and tests in one place.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
                sh 'echo Build step running'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'echo Test step running'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application...'
                sh 'echo Deploy step running'
            }
        }
    }
}

This Jenkinsfile defines a simple pipeline with three stages: Build, Test, and Deploy.

agent any means the pipeline can run on any available Jenkins worker.

Each stage groups related steps, making the process clear and organized.

The steps section contains commands to run, here simple echo and shell commands to simulate work.

Commands
Starts the Jenkins server locally so you can access the Jenkins web interface to create and manage pipelines.
Terminal
java -jar jenkins.war
Expected OutputExpected
Running from: /home/user/jenkins.war 2024-06-01 12:00:00.000+0000 [id=1] INFO org.eclipse.jetty.server.Server#main: Started @12345ms Jenkins is fully up and running
Triggers a build of the Jenkins pipeline named 'my-pipeline' using the Jenkins REST API with basic authentication.
Terminal
curl -X POST http://localhost:8080/job/my-pipeline/build --user admin:admin123
Expected OutputExpected
HTTP/1.1 201 Created Location: http://localhost:8080/queue/item/123/
-X POST - Specifies the HTTP method to trigger the build
--user admin:admin123 - Provides authentication credentials
Fetches the status and details of the last build of 'my-pipeline' in JSON format to check if it succeeded.
Terminal
curl http://localhost:8080/job/my-pipeline/lastBuild/api/json --user admin:admin123
Expected OutputExpected
{ "result": "SUCCESS", "building": false, "number": 5 }
--user admin:admin123 - Provides authentication credentials
Key Concept

If you remember nothing else from Jenkins pipelines, remember: they automate your build, test, and deploy steps so you don't have to do them by hand.

Common Mistakes
Not starting the Jenkins server before trying to trigger builds.
The Jenkins commands will fail because the server is not running to accept requests.
Always start Jenkins with 'java -jar jenkins.war' before using the web interface or API.
Using incorrect authentication credentials when triggering builds via API.
Jenkins will reject the request with an authentication error, and the build won't start.
Use the correct username and API token or password for the Jenkins user.
Not defining stages in the Jenkinsfile, causing unclear or unorganized pipelines.
Without stages, it's hard to see which part of the pipeline is running or failing.
Always organize your pipeline into clear stages like Build, Test, and Deploy.
Summary
Start Jenkins server to enable pipeline automation.
Use a Jenkinsfile to define build, test, and deploy stages clearly.
Trigger and monitor builds using Jenkins web UI or REST API.