{

How to perform git tasks with build script?


In these tutorials, Learn how to perform the below git operations in the 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 the remote repository.

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

These build scripts are configured as dependent plugins in the life cycle of the Gradle build cycle or can run as an individual.

We have gradle-git plugin that can do multiple git operations using a Gradle build script.

In build.gradle script, Please configure the below steps to write 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 the git clone command.

The same thing can be done using the Gradle task.

  • First import grgit classes
  • create a gradle task
  • Inside it, use Grgit.clone() with dir parameter to clone to the 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 the 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?

It creates a new tag and commits changes to the remote repository using the Gradle build task.

  • First, create Grgit Object using the Grgit.open method
  • add a tag using an object with the new tag name and commit message
  • Finally, push changes using the 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 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 a 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 the 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.

It helps DevOps to automate the git operations using the 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.