Search code examples
gitjenkinsjenkins-pluginsjenkins-pipeline

Tagging a git repo using jenkins pipeline script


I am trying to tag a git repo using Jenkins pipeline script. 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'

Solution

  • The git command is a shorthand for the checkout step. It can only clone from the repo.

    If you want to execute generic git commands, then you'll need to set up the connection credentials. In case of SSH, the easiest is to use the SSH Agent Plugin. For some operations you will need to configure the local git user properties too.

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