How to perform git tasks with build script?


In this tutorials, Learn how to perform below git operations in build script.

Gradle git operations
  • Clone git repository in gradle build
  • git Pull changes from gradle task
  • create a git tag and commit changes to remote repository.

Some times, We have to write a build script to automate git tasks for build process in continuous integration and build deployment,

This build scripts are configured as dependent plugins in life cycle of gradle build cycle or can run as individual.

We have gradle-git plugin which can we use to do multiple git operations using gradle build script.

In build.gradle script, Please configure below steps to write a any task for git operations

  • First, Download this plugin using buildScript.
  • let’s configure this plugin in build.gradle
buildscript {
  repositories { 
      mavenCentral()
       }
  dependencies { 
      classpath 'org.ajoberstar:gradle-git:1.6.0'
       }
}
  • import org.ajoberstar.grgit into build script
import org.ajoberstar.grgit.*

How to clone git repository in build grade

git cloning is done with git clone command.

The same thing can be done using gradle task

  • First import grgit classes
  • create a gradle task
  • Inside it, use Grgit.clone() with dir parameter to clone to local folder
  • and uri points to git repository url

task cloneRepository << {
  Grgit.clone(dir: file('build/localreponame'), uri: 'git@github.com:username/reponame.git')
}

The same thing can be run from command line as follows

gradle  cloneRepository
or
gradlew cloneRepository

How to do git pull with gradle tasks

task pullRepo << {
   def grGit = Grgit.open(dir: project.file('.'))
   grGit.pull(rebase: false)
}

The same thing can be run with gradle or wrapper command

gradlew pullRepo
 (or)
gradle pullRepo

How to create a git tag with gradle build task?

This creates a new tag and commit changes to remote repository using gradle build task.

  • First create Grgit Object using Grgit.open method
  • add tag using object with new tag name and commit message
  • Finally push changes using push method
task createGitTag <<{                                                      
def grGit = Grgit.open(dir: 'location_folder')    
// create a tag with new  name and commt message    
grGit.tag.add(name: 'release-tag-1..0', message: 'Create a new tag using gradle tag')           
// push
grGit.push(tags: true)     
//clean resource
grGit.close()                                         
}    

The same thing can be run with gradle or wrapper command

gradlew createGitTag
 (or)
gradle createGitTag

How to commit and push file changes to git remote repository in build.grade script

In build script

  • first create a Grgit object with local directory path
  • Next, add patterns object to grGit which tells to include folder or single file to commit
  • Provide commit message using commit method
  • Finally, push changes using push method.
task pushtoGitRepo <<{ 
    def grGit = Grgit.open(dir: '.')
    grGit.add(patterns: ['src'])
    grGit.commit(message: 'Committing final Changes')
    grGit.push(force: true) 
}

The same thing can be run with gradle or wrapper command

gradlew pushtoGitRepo
 (or)
gradle pushtoGitRepo

Conclusion

You learned how to write a build task for git commit push pull and create a branch.

This helps devops to automate the git operations using build script

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.