简体   繁体   中英

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://git@bitbucket.org:company/repo.git'

The git command is a shorthand for the checkout step. It can only clone from the repo.

If you want to execute generic git commands, then you'll need to set up the connection credentials. In case of SSH, the easiest is to use the SSH Agent Plugin . For some operations you will need to configure the local git user properties too.

Example:

# configure git
sh '''
  git config --global user.email 'my@company.org'
  git config --global user.name 'This Is Me'
  git tag this-very-version
'''

# enable remote connection
sshagent (credentials: ['your-credentials-to-bitbucket']) {
  sh 'git push origin this-very-version'
}
#!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 "shikhas@ayraa.io"')
                        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}

                """
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM