About CI tools ...
Now a days Gitlab is becoming a very popular in software development . Because it provides both Source code management and Continuous integration itself. Anyhow Jenkins is still the most popular CI tool in the industry as per the stats in Developer Ecosystem Stats in 2023.
Moving to GitLab
When the industries move their SCM tool to Gitlab , there is a requirement to integrate Git-lab repository with the Jenkins.How the Jenkins SCM change to Gitlab repository ?? That need to be explored. Before everything we would need a token from the Gitlab repository.
Gitlab Provides different types of tokens so that you can communicate with .
- The GitLab API.
- GitLab repositories.
- The GitLab registry.
You can find more details on the different types of the Gitlab tokens in GitLab Token overview. In this article we are going to focus on the Project access tokens assuming that you are a owner or a maintainer of the project . Because only owner and maintainers are allowed to manage the Gitlab tokens.
Here I am selecting one of my Gitlab Projects for the demonstration. In here my role is Owner .
How to create a token for GitLab project?
Go inside your project , click on the Settings on the left sidebar and once it expanded click on the Access Tokens.
Once you go inside the Access Tokens , you can see the available access token . At the moment there are no any access tokens created for this project . Now I will add a new access token by clicking on the Add new token .
In here , you need give the below details >
- Token name : Token name is a descriptive name for the token you are creating.
- Expiration Date :Expiration Date specifies the date when the token will expire and no longer be valid.
- Select Role : role the token will assume in the context of its permissions (e.g., Developer, Maintainer). These roles dictate the level of access the token has within the projects.
- Select Scope : Scopes define the permissions granted to the token. Each scope .allows the token to perform certain actions within GitLab.
Once all filled , click on Create Project access token . Make sure you copy the token because you cant access it again .
How can we add this credentials to Jenkins?
Go to your Jenkins dashboard > Manage Jenkins > Security > Credentials
Now select the domain where you want to add credentials . For the demonstration I will add this in the global domain .
On your domain , click on Add Credentials.
In here add the below details,
- Kind : Username With passsword
- Scope : your scope domain
- username : Gitlab Username
- Password : Gitlab Token
- ID : desired ID for the jenkins credentials
- Description : Credential description
After adding these details you can save the credentials.
Now go to your Jenkins project , and go to the configuration . Under the Pipeline configuration section give your Gitlab repository URL . You will see that it will fail to connect to the repository. This is because access is denied.
To resolve this you can click the credentials drop down and select the previously added credentials.
Now Jenkins will use this repository to checkout .
Further , within the Jenkins pipeline you can use the credentials to checkout from the git repo .See the below example >
stage('Checkout') {
steps {
// Checkout the code from GitLab using the token
git branch: 'main',
url: 'https://gitlab.com/username/repository.git',
credentialsId: 'project-1-token'
}
}
0 Comments