'Tagging a git repo using jenkins pipeline script

I am trying to tag a git repo using Jenkins pipeline script. I am very new to jenkins pipeline script.

Is there a command like the one below, to tag the branch, which is used to checkout the branch

git branch: 'master',
  credentialsId: '12345-1234-4696-af25-123455',
  url: 'ssh://[email protected]:company/repo.git'


Solution 1:[1]

#!groovy
import java.text.SimpleDateFormat
def gitRepo = "${env.REPO_NAME}"
def gitBranch = "${env.BRANCH_NAME}"
def gitCredentialsId = "b5aeddba-e2d2-4415-aab3-9cb3ec62fd65"
def snapshotVersion = ''
pipeline {
    agent { label "Jenkins-Node-1" }
    stages {
        stage('Pull code from GIT') {
            steps {
                script {
                    cleanWs()
                    steps.git branch: gitBranch, url:gitRepo, credentialsId: gitCredentialsId
                    stash includes: '**', name: 'workspace'
                }
            }
        }
        stage('Build Code') {
            steps {
                script {
                    unstash 'workspace'
                    sh '''
                        export JAVA_HOME="/usr/lib/jvm/java-11-amazon-corretto.x86_64"
                        mvn clean install
                    '''
                }
            }
        }
        stage('Initialize GIT') {
            steps {
                script {
                def remoteOrigin =gitRepo.replace('https://','')
                snapshotVersion = sh(script:"""xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml""", returnStdout:true).trim()
                withCredentials([usernamePassword(credentialsId: gitCredentialsId, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
                        def uri ="""'https://${GIT_USERNAME}:${GIT_PASSWORD}@${remoteOrigin}'"""
                        sh('git config --global user.email "[email protected]"')
                        sh('git config --global user.name "shikhasingh"')
                        sh("git remote -v")
                    }
                }
            }
        }
        stage('Create release tag') {
            steps {
                script {
                def date = new Date()
                sdf = new SimpleDateFormat("dd-MM-yyyy")
                //snapshotVersion = sh(script:"""xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml""", returnStdout:true).trim()
                println("Date is: "+sdf.format(date))
                def TAG="tag-${sdf.format(date)}"
                echo "TAG is : ${TAG}"
                sh """
                     echo "TAG is : ${TAG}"
                     git tag -a ${TAG} -m "tag: ${TAG} is created"
                     echo "*** Created tag ${TAG} in ${gitBranch}"
                     git push origin ${TAG}

                """
                }
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shikha singh