Search code examples
gitjenkinsjenkins-pipelinegit-tag

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


After a successful build of the projects sources I want to set a tag in git and push it. Here are the relevant parts of my pipeline definition:

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

This results in following error log:

[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

What is going wrong and/or how can I accomplish my goal tagging the project after each successful build?


Solution

  • I am using the following code to create a git tag and push it to the server. You need to add this stage after the build stage.

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