How to perform git tasks with build script?
In these tutorials, Learn how to perform the below git operations in the build script.
- Clone git repository in gradle build
- git pull changes from the 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 the 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 the build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.ajoberstar:gradle-git:1.6.0'
}
}
import org.ajoberstar.grgit into the build scrip
t
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 the dir parameter to clone to the local folder
- and URI points to the git repository url
task cloneRepository << {
Grgit.clone(dir: file('build/localreponame'), uri: '[email protected]: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 the message
- Finally, push changes using the push method
task createGitTag <<{
def grGit = Grgit.open(dir: 'location_folder')
// create a tag with the new name and commit 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 a 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.