This article will teach you how to use Git in pipelines. Using Git is a most important topic in CI/CD pipelines as we store everything in Git, our source codes and pipeline codes themselves. To work with Git, you need the Jenkins Git plugin.
Follow our social media:
https://www.linkedin.com/in/ssbostan
https://www.linkedin.com/company/kubedemy
https://www.youtube.com/@kubedemy
Jenkins Pipeline in Git repository:
When you’re about to create a new Jenkins pipeline, you can connect Jenkins to a specific Git repository to read your pipeline code, Jenkinsfile. In the case of CI pipelines, your Jenkinsfile is along with your source code, and Jenkins will clone your Git repository, which consists of your application source code and Jenkinsfile. You need your source code in the Jenkins CI pipeline to be able to build it, test it, and release it. You may have multiple things in CD pipelines like Kubernetes manifests, GitOps manifests, Ansible playbooks, Docker-compose files, Terraform codes, Pulumi codes, etc., along with your Jenkinsfile.
In the Pipeline section, you can configure:
- Git repository URL address
- Credentials to clone the private repository (User/Pass or SSH)
- Target branch or Ref name
- Script path, Jenkins pipeline file (Default: Jenkinsfile)
It’s super easy, but what if you want to clone the Git repository inside your pipeline or commit and push something into the Git repo using the pipeline? Jenkins provides several ways to clone a Git repository inside the pipeline.
Clone Git repository in Jenkins Pipeline:
git(
url: "REPO_URL_OR_SSH_URI_ADDRESS",
branch: "REPO_BRANCH_NAME",
credentialsId: "REPO_CREDENTIAL_ID_IN_JENKINS",
changelog: true,
poll: true
)
For public repositories, you don’t need to specify credentialsId
option.
If poll
option is set to true, the repo will be polled for changes.
If changelog
option is set to true, the changelog will be computed.
Here is a complete example of how to clone a Git repository:
Attention: If you read pipeline code from Git using Jenkinsfile “We always use this method”, you should use dir
to go to another directory and clone the other repository to prevent conflict between the main repo and the newly cloned repo.
Commit and Push to Git using Jenkins Pipeline:
Now it’s time to commit something new and push it back to the Git repository. Why do we need it? In many cases, you may need to add, commit and push from your pipeline. For example, updating GitOps manifests after building the image of the new application version or pushing terraform.tfplan file into the artefact repo.
First, you need to configure the Git plugin:
Dashboard > Manage Jenkins > Configure System > Git plugin
To push to the GitHub repository, you need to have a token. To create a token, go to Developer Settings in your GitHub account.
Then back to Jenkins and create a new Username/Password credential.
Manage Jenkins > Credentials > System > Global credentials
For GitHub, write TOKEN in the Password field.
Now go to your pipeline code, commit and push something to the Git repository. It doesn’t matter if you’ve cloned the Git repository in the pipeline or Jenkins has cloned it because of Jenkinsfile from SCM. To do so, you need a Git repository with .git
directory and credentials to push to the origin.
withCredentials
is a pipeline step to inject credentials in its block. As you can see, I injected a gitUsernamePassword to be able to run the git push
command.
Sometimes Jenkins configuration does not take place, and you may face the following error. To solve it, run git config
command inside the Jenkins container.
git config --global user.name "Jenkins Pipeline"
git config --global user.email "jenkins@localhost"
Run the pipeline and check your Git repository. You should be able to see the new commit created and pushed by the Jenkins pipeline.
Conclusion:
Using Git repositories in CI/CD pipelines is a daily task you should learn well. In the following articles, we will work more with Git repositories as everything we have is stored in Git repositories, and we use it as the source of truth.
You can find all tutorial materials in the following GitHub repository:
https://github.com/ssbostan/jenkins-tutorial
If you like this series of articles, please share them and write your thoughts as comments here. Your feedback encourages me to complete this massively planned program.
Follow my LinkedIn https://www.linkedin.com/in/ssbostan
Follow Kubedemy LinkedIn https://www.linkedin.com/company/kubedemy
Follow Kubedemy Telegram https://telegram.me/kubedemy