Skip to content

detectAgent()

Scans the workspace root for well-known indicator files and returns the Jenkins agent label that matches the project's language. Eliminates the need for every team to hardcode agent { label '...' } in their Jenkinsfile.


Fallback chain

  1. Indicator file scan — checks the repo root for the files below, in order. Returns the label for the first match.
  2. JENKINS_DEFAULT_AGENT env var — if no indicator file is found, checks whether this variable is set on the Jenkins controller and uses it. Lets operators define a platform-wide default without touching pipeline code.
  3. Hard fail — if neither matched, throws an error listing every supported indicator file so the developer knows exactly what to add.

Indicator file → agent label

File Agent label
package.json nodejs
pom.xml maven
build.gradle / build.gradle.kts gradle
go.mod golang
Cargo.toml rust
requirements.txt python
setup.py python
pyproject.toml python
Gemfile ruby
composer.json php

Usage

@Library('shared') _

pipeline {
    agent { label detectAgent() }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
    }
}

Platform-wide default

Set JENKINS_DEFAULT_AGENT as a global environment variable in casc.yml to provide a fallback for repos that don't match any indicator file:

jenkins:
  globalNodeProperties:
    - envVars:
        env:
          - key: JENKINS_DEFAULT_AGENT
            value: nodejs