zot24
7/19/2016 - 3:12 AM

Jenkinsfile to tag the sources used by current build

Jenkinsfile to tag the sources used by current build

node {
  repositoryAccess = 'https://'
  repositoryAccessSeparator = '/'
  
  echo "repository host: ${repositoryHost}" // github.com
  echo "repository path: ${repositoryPath}" // <user>/<repository>.git
  echo "repository jenkins credentials id: ${credentialsId}"  // jenkins credentials for the jenkins git account who have commit access
  echo "repository branch: ${branch}" // master or another branch
  echo "repository commiter username: ${repositoryCommiterUsername}" // Jenkins account email 
  echo "repository commiter name: ${repositoryCommiterEmail}" // Jenkins
  
  repositoryUrl = "${repositoryHost}${repositoryAccessSeparator}${repositoryPath}"
  repositoryUrlFull = "${repositoryAccess}${repositoryUrl}"
  echo "repository url: ${repositoryUrl}" // github.com/<user>/<repository>.git
  echo "repository url full: ${repositoryUrlFull}" // https://github.com/<user>/<repository>.git
  
  echo "download sources from repository branch"
  git credentialsId: credentialsId, url: repositoryUrlFull, branch: branch
  
  echo "tag the sources with this build tag and push the tag to origin repository"
  withCredentials([[$class: 'UsernamePasswordMultiBinding', 
                  credentialsId: credentialsId, 
                  usernameVariable: 'GIT_USERNAME', 
                  passwordVariable: 'GIT_PASSWORD']]) {

    sh("git config user.email ${repositoryCommiterEmail}")
    sh("git config user.name '${repositoryCommiterUsername}'")
    sh("git tag -a ${env.BUILD_TAG} -m '${repositoryCommiterMessage}'")
    sh("git push ${repositoryAccess}${env.GIT_USERNAME}:${env.GIT_PASSWORD}@${repositoryUrl} --tags")
  }
}