Setting up Jenkins pipeline for Maven project

·

1 min read

I'll be sharing my experience setting up Jenkins pipeline and solutions to the problems I faced.

Below is the general pipeline script:

pipeline {
    agent any 
    environment {
        GIT_URL="ssh://your_github_repo.git"
    }
    stages {
        stage('Checkout') { 
            steps {
                sh('echo "checking out from ${GIT_URL}"')
                git branch: 'main',
                    credentialsId:'credential_id',
                    url: "${GIT_URL}"
            }
        }
        stage('Build') { 
            steps {
                // Building maven project
                sh('echo "Build Started"')
                dir('code/nss-web') {
                    sh('pwd')
                    sh('mvn clean package -P PROD')
                }
            }
        }
        stage('Deploy') { 
            steps {
                // Deploying
                sh('echo "Deploy Started"')
                sh('eb deploy')
            }
        }
    }

}

Checking out from git with credentials, you can specify branch, credentialsId, and git URL. I came across a below error initially:

hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- https://github.com/WPMedia/NewsService.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://your_github_repo.git/'

Make sure your repository URL is ssh:// instead of https://

Shell command cd will not move you to different directory. To execute build in a different folder, you can use dir('path'){} and run the command inside the curly brackets.

Setting Up Maven

You can set the name for your Maven as well as set the version associated with the name.