Nani
Nani

Reputation: 117

how to convert jenkins scripted pipeline to declarative pipeline

can someone help me to convert the below Jenkins scripted pipeline to declarative pipeline

node('agent') {

if ( ! "${GIT_BRANCH}".isEmpty()) {
    branch="${GIT_BRANCH}"
} else {
    echo 'The git branch is not provided, exiting..'
    sh 'exit 1'
}

version = extract_version("${GIT_BRANCH}")

if ( "${GIT_BRANCH}".contains("feature")) {
    currentBuild.displayName = "${version}-SNAPSHOT-${env.BUILD_ID}"
}
else {
    currentBuild.displayName = "${version}-${env.BUILD_ID}"
 }
}

I am trying to check if git branch has been provided and setup jenkins build id dynamically based on git branch

Upvotes: 4

Views: 5155

Answers (2)

JKC
JKC

Reputation: 47

pipeline {
agent any

stages {
    stage('Validate Git Branch') {
        steps {
            script {
                if (env.GIT_BRANCH.isEmpty()) {
                    echo 'The git branch is not provided, exiting..'
                    sh 'exit 1'
                }
                env.BRANCH = env.GIT_BRANCH
                env.VERSION = extract_version(env.GIT_BRANCH)
            }
        }
    }

    stage('Set Display Name') {
        steps {
            script {
                if (env.BRANCH.contains("feature")) {
                    currentBuild.displayName = "${env.VERSION}-SNAPSHOT-${env.BUILD_ID}"
                } else {
                    currentBuild.displayName = "${env.VERSION}-${env.BUILD_ID}"
                }
            }
        }
    }
}}

Upvotes: 0

ben5556
ben5556

Reputation: 3018

pipeline {
agent {
        label 'agent'
    }
    stages{
        stage('stag1'){
            steps {
                script {
                    if ( ! "${GIT_BRANCH}".isEmpty()) {
                        branch="${GIT_BRANCH}"
                    } else {
                        echo 'The git branch is not provided, exiting..'
                        sh 'exit 1'
                    }

                    version = extract_version("${GIT_BRANCH}")

                    if ( "${GIT_BRANCH}".contains("feature")) {
                        currentBuild.displayName = "${version}-SNAPSHOT-${env.BUILD_ID}"
                    }
                    else {
                        currentBuild.displayName = "${version}-${env.BUILD_ID}"
                    }
                }
            }           
        }
    }
}

Upvotes: 6

Related Questions