no such DSL method `stages`

后端 未结 2 1982
无人及你
无人及你 2020-12-23 10:10

I\'m trying to create my first Groovy script for Jenkins:

After looking here https://jenkins.io/doc/book/pipeline/, I created this:

node {
  stages {         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 10:26

    A Jenkinsfile can be written using two types of syntax - Declarative and Scripted.

    Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which:

    • provides richer syntactical features over Scripted Pipeline syntax, and

    • is designed to make writing and reading Pipeline code easier.

    Many of the individual syntactical components (or "steps") written into a Jenkinsfile, however, are common to both Declarative and Scripted Pipeline. Example:

    Declarative Pipeline fundamentals

    In Declarative Pipeline syntax, the pipeline block defines all the work done throughout your entire Pipeline.

    Jenkinsfile (Declarative Pipeline):

    pipeline {
        agent any 1
        stages { 
            stage('Build') { 2
                steps { 
                    // 3
                }
            }
            stage('Test') { 4
                steps { 
                    // 5
                }
            }
            stage('Deploy') { 6
                steps {
                    // 7
                }
            }
        }
    }
    
    1. Execute this Pipeline or any of its stages, on any available agent.
    2. Defines the "Build" stage.
    3. Perform some steps related to the "Build" stage.
    4. Defines the "Test" stage.
    5. Perform some steps related to the "Test" stage.
    6. Defines the "Deploy" stage.
    7. Perform some steps related to the "Deploy" stage.

    Scripted Pipeline fundamentals

    In Scripted Pipeline syntax, one or more node blocks do the core work throughout the entire Pipeline. Although this is not a mandatory requirement of Scripted Pipeline syntax, confining your Pipeline's work inside of a node block does two things:

    1. Schedules the steps contained within the block to run by adding an item to the Jenkins queue. As soon as an executor is free on a node, the steps will run.

    2. Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from source control.
      Caution: Depending on your Jenkins configuration, some workspaces may not get automatically cleaned up after a period of inactivity. See tickets and discussion linked from JENKINS-2111 for more information.

    Jenkinsfile (Scripted Pipeline):

    node { 1
        stage('Build') { 2
            // 3
        }
        stage('Test') { 4
            // 5
        }
        stage('Deploy') { 6
            // 7
        }
    }
    
    1. Execute this Pipeline or any of its stages, on any available agent.
    2. Defines the "Build" stage. stage blocks are optional in Scripted Pipeline syntax. However, implementing stage blocks in a Scripted Pipeline provides clearer visualization of each `stage's subset of tasks/steps in the Jenkins UI.
    3. Perform some steps related to the "Build" stage.
    4. Defines the "Test" stage. 5
    5. Perform some steps related to the "Test" stage.
    6. Defines the "Deploy" stage.
    7. Perform some steps related to the "Deploy" stage.

    Pipeline example

    Here is an example of a Jenkinsfile using Declarative and it's equivalent scriptive Pipeline syntax:

    Jenkinsfile (Declarative Pipeline):

    pipeline {
        agent any
        options {
            skipStagesAfterUnstable()
        }
        stages {
            stage('Build') {
                steps {
                    sh 'make'
                }
            }
            stage('Test'){
                steps {
                    sh 'make check'
                    junit 'reports/**/*.xml'
                }
            }
            stage('Deploy') {
                steps {
                    sh 'make publish'
                }
            }
        }
    }
    

    Jenkinsfile (Scripted Pipeline):

    node {
        stage('Build') {
            sh 'make'
        }
        stage('Test') {
            sh 'make check'
            junit 'reports/**/*.xml'
        }
        if (currentBuild.currentResult == 'SUCCESS') {
            stage('Deploy') {
                sh 'make publish'
            }
        }
    }
    

提交回复
热议问题