Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a tag to a repository with Jenkinsfile pipeline with credentials

I'm new on Jenkinsfile and I'm trying to pass a job I have to a Jenkinsfile. I've successfully created all steps I needed but I can't add a tag to my git repository like I use to with the interface and the "git publisher" plugin.

Here is what I've got now :

stage('Tag') {
 steps {
     script {
         env.POM_VERSION = readMavenPom().getVersion()
     }
     // creating the name of the tag
     sh '''#!/bin/bash -xe
       currentDate=$(date +"%Y-%m-%d_%Hh%Mm%Ss")
       customTagName="${CUSTOMER_NAME}--${POM_VERSION}--${currentDate}"
       echo CUSTOM_TAG_NAME=${customTagName} >> ${PROPERTIES_FILE_NAME}
     '''
     script {
         def PROPERTIES = readProperties file: "${PROPERTIES_FILE_NAME}"
         env.CUSTOM_TAG_NAME = PROPERTIES.CUSTOM_TAG_NAME
     }
     withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS_ID}", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
         sh "git tag -a ${CUSTOM_TAG_NAME} -m 'Jenkinsfile push tag'"
         sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@git.repohere.com/scm/reponamehere.git ${CUSTOM_TAG_NAME}"
     }
  }

}

but then I've got this issue in logs:

[Pipeline] sh + git push 'https://****:****@git.repohere.com/scm/reponamehere.git' name--4.36.0--2020-04-10_22h00m50s

fatal: unable to access 'https://****:****@git.repohere.com/scm/reponamehere.git/': Could not resolve host: ****; Unknown error [Pipeline] } [Pipeline] // withCredentials

I'm stuck on this for hours now, how can I add and push a tag with credentials added in jenkins config. Thank you!

like image 782
Antoine Avatar asked Oct 23 '25 02:10

Antoine


1 Answers

Do you have this variable ${GIT_CREDENTIALS_ID} declared already in the environment directive in your pipeline first? If not declare it first or use the value of the variable directly as listed below:

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'MyID', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
    sh("git tag -a some_tag -m 'Jenkins'")
    sh("git push https://${env.GIT_USERNAME}:${env.GIT_PASSWORD}@<REPO> --tags")
}

OR

You don't need to pass your password with your GIT repo url in jenkinsfile if you're using the sshagent to create tag and push.

pipeline {
    agent any
    stages {
        stage("Tag and Push") {
            when { branch 'master' }
            environment { 
                GIT_TAG = "jenkins-$BUILD_NUMBER"
            }
            steps {
                sh('''
                    git config user.name 'my-ci-user'
                    git config user.email '[email protected]'
                    git tag -a \$GIT_TAG -m "[Jenkins CI] New Tag"
                ''')
                
                sshagent(['my-ssh-credentials-id']) {
                    sh("""
                        #!/usr/bin/env bash
                        set +x
                        export GIT_SSH_COMMAND="ssh -oStrictHostKeyChecking=no"
                        git push origin \$GIT_TAG
                     """)
                }
            }
        }
    }
}
like image 83
vijay Avatar answered Oct 25 '25 08:10

vijay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!