3
votes

Ihave noticed that Jenkins pipeline file -- Jenkinsfile which have two syntax

  • Declarative
  • Scripted

I have made Declarative Script work to specify node to run my task. However I don't know how to modify my script to Scripted syntax.

My Declarative Script

pipeline {
    agent none

    stages {
        stage('Build') {
            agent { label 'my-label​' }
            steps {
                echo 'Building..'
                sh '''

                '''
            }
        }
        stage('Test') {
            agent { label 'my-label​' }
            steps {
                echo 'Testing..'
                sh '''

                '''
            }
        }
        stage('Deploy') {
            agent { label 'my-label​' }
            steps {
                echo 'Deploying....'
                sh '''

                '''
            }
        }
    }
}

I have tried to use in this way:

node('my-label') {
  stage 'SCM'
  git xxxx

  stage 'Build'
  sh ''' '''
}

But it seems Jenkins cannot find my node to run.

1

1 Answers

4
votes

How about this simple example?

stage("one") {
    node("linux") {
        echo "One"
    }
}
stage("two") {
    node("linux") {
        echo "two"
    }
}
stage("three") {
    node("linux") {
        echo "three"
    }
}

Or the below answer, this way you are guaranteed to have the stages run on the same node if there are multiple nodes with the same label and run interrupted by another job. The above example will release the node after every stage, the below example will hold the node for all three stages.

node("linux") {
    stage("one") {
        echo "One"
    }
    stage("two") {
        echo "two"
    }
    stage("three") {
        echo "three"
    }
}