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

时间:2017-12-20 10:41:31

标签: git jenkins jenkins-pipeline git-tag

在成功构建项目源之后,我想在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

出现问题和/或如何在每次成功构建后完成标记项目的目标?

1 个答案:

答案 0 :(得分:6)

我正在使用此代码创建一个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}"
            }
        }
    }