Search code examples
jenkinsgroovyjenkins-pipeline

Creating a Named step in groovy


I'm trying to learn groovy for my pipeline set-up and I'm stuck on something realy basic, but I don't know where to start looking to solve my issue.

What I'm trying to do is basicly create a stage with multiple Named steps. Underneath I've posted a basic example of what I'm trying to do, what would be the 'go to way' to do this.(this just creates a folder with a zipped file inside it).

Sidenote: This code currently throws the error

WorkflowScript: 23: Expecting "interface jenkins.tasks.SimpleBuildStep" but got Mkdir

pipeline {
    agent any
    
    stages {
        stage('Zipfile'){
            steps{
                step('Mkdir'){
                    sh 'mkdir LocalDir'
                }
                step('Touch File'){
                    sh '''cd LocalDir
                    touch File
                    cd  ..'''
                }
                step('Zip'){
                    sh '''cd LocalDir
                    zip File.zip File
                    cd  ..'''
                }
            }
        }
    }
}

Solution

  • I think i found what the question was expecting... I should have added named stages with steps in them

    pipeline {
        agent any
        
        stages {
            stage('Zipfile'){
                stages{
                    stage('mkdir'){
                        steps{
                           sh 'mkdir LocalDir'
                        }
                    }
                    stage('Touch File'){
                        steps{
                             sh '''cd LocalDir
                             touch File
                             cd  ..'''
                        }
                    }
                    stage('Zip'){
                        steps{ 
                              sh '''cd LocalDir
                              zip File.zip File
                              cd  ..'''
                        }
                    }
                }
            }
        }
    }