如何从 jenkins 声明性管道中标记 git 中的项目

How can I tag a project in git from a jenkins declarative pipeline

成功构建项目源后,我想在 git 中设置一个标签并推送它。 以下是我的管道定义的相关部分:

pipeline {
    agent {
        docker {
            label 'docker'
            image 'somerepo/maven:3.5.2-jdk-8-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }

    stages {
        stage('Prepare') {
            environment {
                BITBUCKET = credentials('jenkins-creds')
            }
            steps {
                sh 'git clean -ffdx'
                sh("git config user.name 'jenkins'")
                sh("git config user.email 'x@y.z'")
                // Use .netrc for Git credentials
                sh '''
                    cat >~/.netrc <<.
                    machine bitbucket.server
                    login $BITBUCKET_USR
                    password $BITBUCKET_PSW
                    .
                    '''.stripIndent()
            }
        }
        stage('Validate pom') {
            steps {
                sh 'mvn -B validate'
            }
        }
        stage('Tag the version') {
            steps {
                sh 'printf "Tag the sources %s", "1.2.3"'
                sh 'mvn -B versions:set -DnewVersion=5.1.1'
                sh 'mvn -B scm:tag'
            }
        }
    }
}

这会导致以下错误日志:

[INFO] Building PR02 Parent POM 5.1.2
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-scm-plugin:1.9.5:tag (default-cli) @ pr02-parent-pom ---
[INFO] Final Tag Name: '5.1.2'
[INFO] Executing: /bin/sh -c cd /var/jenkins_home/workspace/ParentPOM-Pipeline_master-GOYL657LPCAICEX72SN6WFZQV55BYMCLX7KLK63B6MKGMFYWD3IA && git tag -F /tmp/maven-scm-523050667.commit 5.1.2
[INFO] Working directory: /var/jenkins_home/workspace/ParentPOM-Pipeline_master-GOYL657LPCAICEX72SN6WFZQV55BYMCLX7KLK63B6MKGMFYWD3IA
[INFO] Executing: /bin/sh -c cd /var/jenkins_home/workspace/ParentPOM-Pipeline_master-GOYL657LPCAICEX72SN6WFZQV55BYMCLX7KLK63B6MKGMFYWD3IA && git push https://jira.team-dso.nl/bitbucket/scm/pr02/pr02-parent-pom.git refs/tags/5.1.2
[INFO] Working directory: /var/jenkins_home/workspace/ParentPOM-Pipeline_master-GOYL657LPCAICEX72SN6WFZQV55BYMCLX7KLK63B6MKGMFYWD3IA
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] fatal: could not read Username for 'https://jira.team-dso.nl': No such device or address

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

出了什么问题and/or如何在每次成功构建后完成标记项目的目标?

我正在使用此代码创建一个 git 标签并将其推送到服务器。您需要在构建阶段之后添加此阶段

stage('Git Push To Origin') {

        steps {

            withCredentials([usernamePassword(credentialsId: your_credentials, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {


                sh "git tag ${your_tag}"
                sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${repository} ${your_tag}"
            }
        }
    }