Is there a way to get all stage names in a declarative pipeline?

I want to set GitHub Checks for each stage on the pipeline to “Pending” when a pipeline starts.
I use the checks-api plugin (GitHub - jenkinsci/github-checks-plugin: Jenkins Plugin for GitHub Checks API)

Now I repeat the publishChecks (Checks API plugin) for each stage on the 1st stage.
But I would like to have a loop to go over all stage names and create the checks, so in the future if stages are added, it will automatically do the work.

TL;DR

I want to get a list of stages names in the pipeline.

Hello @stavros-k and welcome to this community. :wave:

Would that work for you?

def getStageNames() {
    def stageNames = []
    node {
        stage('Get Stage Names') {
            // Define your pipeline stages here
            stage('Stage 1') {
                // ...
            }
            stage('Stage 2') {
                // ...
            }
            stage('Stage 3') {
                // ...
            }

            // Retrieve stage names
            stageNames = stage('Get Stage Names').parent.children.collect { it.name }
        }
    }
    return stageNames
}

def stageNames = getStageNames()
println(stageNames)