Search code examples
jenkinsjenkins-pipeline

List all the tags using input (Jenkinsfile)


I'm trying to list all the tags (GitLab) inside an input choise, but I do not know how to do it.

What I want to do is to be able to select the tag and based on that perform the deploy to different environments.

Thank you.


Solution

  • I proposes such a solution working in declarative pipeline with dsl:

    1. stage with download repo
    2. parse tags based on repo
    3. stage with choice parameter

    CODE:

    pipeline {
        agent  any
        stages {
    
            stage('PollSCM') {
                steps {
                    checkout([$class: 'GitSCM', branches: [[name: 'master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxx', url: 'repo']]])
                    script {
                        tags = sh(script: "git tag --sort=v:refname | tail -5 ", returnStdout: true).trim()
                    }
                }
            }
    
            stage('CHOICE TAG') {
                steps {
                    script {
                        def tag_response = input message: 'blah blah tags',
                                parameters: [choice(choices: "${tags}",  description: 'blah', name: '')]
                        env.tag_response = tag_response
                    }
    
                }
            }
    
            stage ('echo choose') {
                steps {
                    echo "I choose: '${tag_response}'"
                }
            }
    
        }
    }