Simple Jenkins pipeline for running and automating Gradle tasks

Jenkins is a great tool for automating running different Gradle tasks. In this post, you'll find a very simple starting point for creating a Jenkins pipeline that can run different Gradle tasks.

Jenkins is a popular and widely-used open-source tool for Continuous Integration and Continuous Delivery/Deployment (CI/CD) automation. Jenkins is easy to install and configure, and comes with a web-based graphical user interface (GUI) that makes it easy to manage and use.

Create a new Jenkins job

Before you can create a new pipeline for Jenkins, you will need to have a job for it. To create a Jenkins pipeline job, you can follow these steps:

  1. Log in to your Jenkins server and navigate to the Jenkins dashboard.

  2. Click on “New Item” from the left-hand menu.

  3. Enter a name for your pipeline job and select “Pipeline” from the list of job types.

  4. Click “OK” to create your pipeline job.

Install necessary plugins

To run our example pipeline from a Jenkinsfile, you will need a couple of Jenkins plugins. Jenkins plugins can be installed by navigating from Jenkins Dashboard to -> “Manage Jenkins” -> “Manage plugins”.

Configuring the pipeline

You can add our example pipeline script directly from your job configuration page to a textarea, or you can add a new script file directly to your source code repository. Jenkins pipelines are usually named as Jenkinsfile to the root of your repository without any file extensions.

pipeline {
    agent any
    
    stages {
        stage('Clean') {
            steps {
                sh './gradlew clean'
            }
        }
        stage('Test') {
            steps {
                sh './gradlew test'
            }
            post {
                always {
                    junit '**/build/test-results/test/TEST-*.xml'
                }
            }
        }
        stage('Build') {
            steps {
                sh './gradlew build -x test'
            }
        }
        stage('Deploy') {
            steps {
                sh './gradlew deploy'
            }
        }
    }

    post {
        failure {
            echo 'Pipeline failed!'
        }
    }
}

This pipeline script includes the following stages with their corresponding build steps inside:

  1. Clean

You want to make sure that you are working with a clean build before running new builds on top of existing ones.

  1. Test

Next, we’ll run our tests, such as unit tests and integration tests. Whether our tests are failing or not, we’ll always generate a complete test report using the JUnit Jenkins plugin.

  1. Build

If none of the tests were broken, we are ready to build our application.

  1. Deploy

Once our build is successful, we’ll use the generated artifacts to deploy to our specified environment.

  1. Post-build actions

In post-build actions, we could do things like automated emails or Slack messages to notify of succeeding or failing pipeline runs.

Software development DevOps Programming

Related contents

  1. Why Tina CMS might be the best tool for bloggers
  2. 15 ways to manage technical debt as a software developer
  3. Making money with ChatGPT plugins
  4. 7 jobs that are in danger of being replaced by an AI
  5. Is AI going to replace software developers in the future