Skip to main content

Jenkins pipeline for Java based applictaion

 To create a Jenkins pipeline that builds and deploys a Java project, you would typically use a Jenkinsfile, which is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Below, I'll provide an example of a Jenkinsfile using Declarative Pipeline syntax. This example assumes you are using Maven as your build tool and you're deploying the artifact to a simple environment.



Example Jenkinsfile


pipeline {

    agent any  // This specifies that Jenkins can use any available agent to run the pipeline


    environment {

        // Define environment variables here, if necessary

        MAVEN_HOME = '/usr/local/maven'

    }


    tools {

        // Specifies tools to auto-install and put on the PATH

        maven 'Maven 3.6'  // Version is an example; adjust based on your configuration

        jdk 'OpenJDK 11'   // Adjust JDK version as needed

    }


    stages {

        stage('Checkout') {

            steps {

                // Checks out source code from the configured SCM

                checkout scm

            }

        }


        stage('Build') {

            steps {

                // Runs Maven build

                script {

                    sh "${MAVEN_HOME}/bin/mvn clean package"

                }

            }

        }


        stage('Tests') {

            steps {

                // Run tests

                script {

                    sh "${MAVEN_HOME}/bin/mvn test"

                }

            }

            post {

                always {

                    // Archive the test results

                    junit '**/target/surefire-reports/TEST-*.xml'

                }

                failure {

                    // Handle failure

                    echo 'Tests failed'

                }

            }

        }


        stage('Deploy') {

            steps {

                // Deploy to your server or environment

                script {

                    echo "Deploying..."

                    // Example command, replace with your deployment script or command

                    sh "scp target/myapp.jar user@server:/path/to/deployment"

                }

            }

        }

    }


    post {

        always {

            // Actions to perform after pipeline execution, always

            echo 'Cleaning up'

            sh 'rm -rf $WORKSPACE/*'

        }

        success {

            // Actions on success

            echo 'Build and deployment successful!'

        }

        failure {

            // Actions on failure

            echo 'Build or deployment failed.'

        }

    }

}



Key Components

agent: Defines where Jenkins will run the pipeline. any means it can run on any available agent.


environment: Sets environment variables like the Maven home directory.


tools: Specifies the necessary tools (e.g., Maven and JDK) and their versions.


stages: Contains multiple stages such as checkout, build, test, and deploy.


Checkout: Gets the source code from your source control management (SCM).

Build: Runs Maven to package the application.

Tests: Runs unit tests and archives the results.

Deploy: Deploys the built artifact to a server or environment.

post: Defines actions to take based on the outcome of the pipeline (e.g., cleanup, notifications).


Replace placeholders like user@server:/path/to/deployment with actual deployment server details and paths. Also, adjust tool versions and paths according to your Jenkins setup.


This Jenkinsfile should be placed at the root of your source code repository and checked into version control so that Jenkins can access it during the build process.

Comments

Popular posts from this blog

DevOps Vs DevSecOps

   DevOps and DevSecOps are two methodologies that have gained traction in the IT industry for streamlining software development and deployment. However, their approach to security and operations differs, making each suitable for different types of projects and organizational needs. Let's explore DevOps versus DevSecOps with a real-time example, focusing on their distinctions, integration, and practical applications. DevOps: The Foundation DevOps is a cultural and professional movement that emphasizes collaboration and communication between software developers and other IT professionals while automating the process of software delivery and infrastructure changes. It aims to shorten the development life cycle and provide continuous delivery with high software quality. Core Principles: Continuous Integration and Continuous Deployment (CI/CD): Automate building, testing, and deployment of applications. Collaboration: Breaking down silos between teams (developers, IT operations...

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines

Deploying a Node.js project to Azure App Services using Azure DevOps pipelines is a robust way to automate deployment processes and integrate continuous integration and deployment (CI/CD) practices into your workflow. This guide will walk you through the setup of an Azure DevOps pipeline to deploy a Node.js application from GitHub or Azure Repos to Azure App Services. Prerequisites Before you begin, ensure you have the following: An Azure account. You can sign up for a free account here . A GitHub or Azure Repos account with your Node.js project. An Azure DevOps account. Create one here if you don't have it. Step 1: Prepare Your Node.js Application Make sure your Node.js application is ready and includes a package.json file in the root. This file is crucial as it contains dependency information and scripts needed for your application. Step 2: Create an Azure Web App Log into Azure Portal: Visit https://portal.azure.com . Create a Web App: Click on "Create a resource". ...

Git Cheat Sheet

  Git Cheat Sheet Category Command Description Setup git config --global user.name "[name]" Set a name that will be attached to your commits and tags. git config --global user.email "[email]" Set an email that will be attached to your commits and tags. git init Initialize a new git repository in the current directory. git clone [url] Clone a repository into a new directory. Stage & Snapshot git status Show modified files in the working directory, staged for your next commit. git add [file] Add a file as it looks now to your next commit (stage). git reset [file] Unstage a file while retaining the changes in the working directory. git diff Show diff of what is changed but not staged. git diff --staged Diff of what is staged but not yet committed. git commit -m "[message]" Commit your staged content as a new commit snapshot. Branch & Merge git branch List all of the branches in your repo. git branch [name] Create a new branch at the current commit. gi...