How can i ignore a particular stage from the compilation tools (maven, java) which are configured globally

20 Views Asked by At

We are using declarative jenkins pipeline, We need to use a default compilation tools and versions (maven and java) for a particular stage in my pipeline instead of using which is defined globally in the pipeline. Which means:- One of my stage in the pipeline should not use the compilation tool which is configured globally.

"pipeline { agent { label "node-1" } tools { maven 'maven3.6.2' jdk 'jdk-17-linux' }"

One of my stage in the pipeline should not use the compilation tool which is configured globally.

1

There are 1 best solutions below

0
Alexander Pletnev On

You can follow the documentation on the Jenkinsfile Syntax and on the tools directive in particular:

A section defining tools to auto-install and put on the PATH. This is ignored if agent none is specified.

...

Allowed

Inside the pipeline block or a stage block.

And try the following example:

// Jenkinsfile
pipeline {
  agent {
    label "node-1"
  }
  tools { // these would be "default"
    maven 'maven3.6.2'
    jdk 'jdk-17-linux'
  }
  stage('Stage one') {
    steps {
      // steps that use the "default" tools
    }
  }
  stage('Stage two') {
    tools {
      maven 'maven3.6.3'
      jdk 'jdk-11-linux' 
    }
    steps {
      // steps that use the "custom" tools
    }
  }
  stage('Stage three') {
    steps {
      // steps that use the "default" tools
    }
  }
}

A better approach, however, would be to apply the same principle but use Docker containers as the build environments (container versions are just for example):

// Jenkinsfile
pipeline {
  agent {
    docker {
      image 'maven:3.9.6-eclipse-temurin-17-alpine'
      args '-v $HOME/.m2:/root/.m2'
    }
  }
  stage('Stage one') {
    steps {
      // steps that use the "default" environment
    }
  }
  stage('Stage two') {
    agent {
      docker {
        image 'maven:3.9.6-eclipse-temurin-11-alpine'
        args '-v $HOME/.m2:/root/.m2'
        reuseNode true // to save time on checkout
      }
    }
    steps {
      // steps that use the "custom" environment
    }
  }
  stage('Stage three') {
    steps {
      // steps that use the "default" environment
    }
  }
}