N
N
Nikolay Baranenko2021-06-25 21:51:57
Conveyor
Nikolay Baranenko, 2021-06-25 21:51:57

How to change stage name dynamically in Jenkins declarative pipeline?

We decided to try to move away from the Scripted Pipeline towards the Declarative Pipeline
and faced the question of how to dynamically redefine the name of the stage?

def template1 = "spread_sshkeys"

pipeline {
    agent any

    stages {
        stage ("Checkout") {
            steps {
                deleteDir()
                checkout scm
                sh "git submodule foreach --recursive git pull origin master"
            }
        }
        stage('test') {
            steps {
                    script {
                        sh "ls -las; cd jenkins-ci-examples; ls -las";
                    }
            }
        }
        stage('run template ${template1}') {
            steps {
                sh "ls -las; cd jenkins-ci-examples; ls -las";
            }
        }

    }
}


as in scripted
stage('run template ${template1}')
does not work now

Is there such a possibility at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baranenko, 2021-06-28
@drno-reg

the answer to the question is here
https://stackoverflow.com/questions/68136020/how-m...
in short, you need to write something like this

def template1 ="spread_sshkeys"
pipeline {
    agent any

    stages {
        stage('Dynamic Stages') {
            
            steps {
                script {
                        stage("import template ${template1}"){
                            println("${env.STAGE_NAME}")
                        }
                         stage("run template ${template1}"){
                            println("${env.STAGE_NAME}")
                        }
                }
            }
        }
        
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question