1
votes

I would like to install maven and npm via docker agent using Jenkins declarative pipeline. But When I would like to use below script Jenkins throws an error as below. It might be using agent none but how can I use node with docker agent via declarative pipeline jenkins.

ERROR: Attempted to execute a step that requires a node context while ‘agent none’ was specified. Be sure to specify your own ‘node { ... }’ blocks when using ‘agent none’.

I try to set agent any but this time I received an error "Still waiting to schedule task Waiting for next available executor"

 pipeline {
    agent none
    // environment{

            proxy = https://
    //      stable_revision = sh(script: 'curl -H "Authorization: Basic $base64encoded" 
    // }

    stages {
        stage('Build') {
            agent {
                docker { image 'maven:3-alpine'}
            }
            steps {
                sh 'mvn --version'       
                echo "$apigeeUsername"
                echo "Stable Revision: ${env.stable_revision}"   
            }
        }
        stage('Test') {
            agent { docker { image 'maven:3-alpine' image 'node:8.12.0' } }
            environment {
                 HOME = '.'
                 }
            steps {
                script{
                    try{
                        sh 'npm install'
                        sh 'node --version'
                        //sh 'npm test/unit/*.js'                
                    }catch(e){
                        throw e
                    }        
                }
            }
        }
        // stage('Policy-Code Analysis') {
        //     steps{
        //         sh "npm install -g apigeelint"
        //         sh "apigelint -s wiservice_api_v1/apiproxy/ -f codeframe.js"
        //     }
        // }
         stage('Promotion'){
            steps{
                timeout(time: 2, unit: 'DAYS') {
                input 'Do you want to Approve?'
                }
            }
        }
        stage('Deployment'){
            steps{
                 sh "mvn -f wiservice_api_v1/pom.xml install -Ptest -Dusername=${apigeeUsername} -Dpassword=${apigeePassword} -Dapigee.config.options=update"
                 //sh "mvn apigee-enterprise:install -Ptest -Dusername=${apigeeUsername} -Dpassword=${apigeePassword} " 
            }
        }
    }
}
2

2 Answers

1
votes

Basically your error message tells you everything you need to know:

ERROR: Attempted to execute a step that requires a node context while ‘agent none’ was specified. Be sure to specify your own ‘node { ... }’ blocks when using ‘agent none’.

so what is the issue here? You use agent none for your pipeline which means you do not specify a specific agent for all stages. An agent executes a specific stage. If a stage has no agent it can't be executed and this is your issue here.

The following 2 stage have no agent which means no docker-container / server or whatever where it can be executed.

     stage('Promotion'){
        steps{
            timeout(time: 2, unit: 'DAYS') {
            input 'Do you want to Approve?'
            }
        }
    }
    stage('Deployment'){
        steps{
             sh "mvn -f wiservice_api_v1/pom.xml install -Ptest -Dusername=${apigeeUsername} -Dpassword=${apigeePassword} -Dapigee.config.options=update"
             //sh "mvn apigee-enterprise:install -Ptest -Dusername=${apigeeUsername} -Dpassword=${apigeePassword} " 
        }
    }

so you have to add agent { ... } to both stage seperately or use a global agent like following and remove the agent from your stages:

 pipeline {
    agent {
        docker { image 'maven:3-alpine'}
    } ...

For further information see guide to set up master and agent machines or distributed jenkins builds or the official documentation.

0
votes

I think you meant to add agent any instead of agent none, because each stage requires at least one agent (either declared at the top for the pipeline or per stage).

Also, I see some more issues. Your Test stage specifies two images for the same stage. agent { docker { image 'maven:3-alpine' image 'node:8.12.0' } } although, your stage is executing only npm commands. I believe only one of the image will be downloaded.

To clarify bit more on mkemmerz answer, your Promotion stage is designed correctly. If you plan to have an input step in the pipeline, do not add an agent for the pipeline because input steps block the executor context. See this link https://jenkins.io/blog/2018/04/09/whats-in-declarative/