使用 jenkins 管道脚本标记 git 回购

Tagging a git repo using jenkins pipeline script

我正在尝试使用 Jenkins 管道脚本标记 git 存储库。我对詹金斯管道脚本很陌生。

是否有像下面这样的命令来标记分支,用于签出分支

git branch: 'master',
  credentialsId: '12345-1234-4696-af25-123455',
  url: 'ssh://git@bitbucket.org:company/repo.git'

git 命令是一个 shorthand for the checkout 步骤。它只能从 repo 克隆。

如果您想执行通用 git 命令,则需要设置连接凭据。对于 SSH,最简单的方法是使用 SSH Agent Plugin。对于某些操作,您还需要配置本地 git 用户属性。

示例:

# 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}

                """
                }
            }
        }
    }
}