0
0
Jenkinsdevops~5 mins

Build steps execution in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want Jenkins to perform tasks like compiling code, running tests, or packaging software, you use build steps. Build steps are the individual commands or scripts Jenkins runs to build your project.
When you need to compile your source code into executable files automatically.
When you want to run automated tests after code changes to catch errors early.
When you want to package your application into a deployable format like a zip or jar file.
When you want to run shell commands or scripts as part of your build process.
When you want to automate repetitive tasks in your software development pipeline.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Compiling the code...'
                sh 'javac HelloWorld.java'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'java HelloWorld'
            }
        }
        stage('Package') {
            steps {
                echo 'Packaging application...'
                sh 'zip HelloWorld.zip HelloWorld.class'
            }
        }
    }
}

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

agent any means Jenkins can run this pipeline on any available agent.

Each stage contains steps which are the commands Jenkins executes.

In the Build stage, it compiles Java code.

In the Test stage, it runs the compiled program.

In the Package stage, it creates a zip file of the compiled class.

Commands
This command updates the Jenkins job configuration with the Jenkinsfile pipeline script. It tells Jenkins to use the new build steps defined.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline
Expected OutputExpected
Job 'my-pipeline' updated successfully
Check that Java is installed on the Jenkins agent so the build steps can compile and run Java code.
Terminal
java -version
Expected OutputExpected
openjdk version "17.0.5" 2022-10-18 OpenJDK Runtime Environment (build 17.0.5+8) OpenJDK 64-Bit Server VM (build 17.0.5+8, mixed mode, sharing)
Trigger the Jenkins pipeline job to start executing the build steps defined in the Jenkinsfile.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1
View the console output of build number 1 to see the results of each build step execution.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Compiling the code... [Pipeline] sh + javac HelloWorld.java [Pipeline] echo Running tests... [Pipeline] sh + java HelloWorld Hello, World! [Pipeline] echo Packaging application... [Pipeline] sh + zip HelloWorld.zip HelloWorld.class adding: HelloWorld.class (deflated 50%) [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

Build steps are the commands Jenkins runs in order to compile, test, and package your software automatically.

Common Mistakes
Not defining build steps inside the 'steps' block of a stage.
Jenkins will not know what commands to run and the build will do nothing or fail.
Always put shell commands or scripts inside the 'steps' section of each stage.
Using commands that are not installed on the Jenkins agent.
Build steps will fail because the system cannot find the command to run.
Verify required tools like Java or zip are installed on the Jenkins agent before running the pipeline.
Triggering the build without updating the Jenkins job configuration after changing the Jenkinsfile.
Jenkins will run the old pipeline without your new build steps.
Update the Jenkins job configuration or reload the Jenkinsfile before triggering the build.
Summary
Define build steps inside the 'steps' block of each stage in the Jenkinsfile.
Use shell commands to compile, test, and package your application automatically.
Trigger the Jenkins job to run the build steps and check console output for results.