简体   繁体   English

使用 jenkins 管道脚本标记 git repo

[英]Tagging a git repo using jenkins pipeline script

I am trying to tag a git repo using Jenkins pipeline script.我正在尝试使用 Jenkins 管道脚本标记 git repo。 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. git命令是checkout步骤的简写 It can only clone from the repo.它只能从 repo 克隆。

If you want to execute generic git commands, then you'll need to set up the connection credentials.如果要执行通用 git 命令,则需要设置连接凭据。 In case of SSH, the easiest is to use the SSH Agent Plugin .对于 SSH,最简单的方法是使用SSH 代理插件 For some operations you will need to configure the local git user properties too.对于某些操作,您还需要配置本地 git 用户属性。

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}

                """
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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